2016-07-20 19:00:34 +02:00
|
|
|
import functools
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from requests import auth
|
2017-03-23 23:25:31 +01:00
|
|
|
from txrequests import Session
|
2016-07-20 19:00:34 +02:00
|
|
|
|
2016-12-21 20:55:43 +01:00
|
|
|
from lbrynet import conf
|
2016-07-20 19:00:34 +02:00
|
|
|
from lbrynet.analytics import utils
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def log_response(fn):
|
2017-03-23 23:25:31 +01:00
|
|
|
def _log_error(failure):
|
|
|
|
log.warning('Failed to send an analytics event. %s', failure.getTraceback())
|
2016-07-20 19:00:34 +02:00
|
|
|
|
|
|
|
@functools.wraps(fn)
|
|
|
|
def wrapper(*args, **kwargs):
|
2017-03-23 23:25:31 +01:00
|
|
|
d = fn(*args, **kwargs)
|
|
|
|
d.addErrback(_log_error)
|
|
|
|
return d
|
|
|
|
|
2016-07-20 19:00:34 +02:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2016-10-01 17:47:37 +02:00
|
|
|
class Api(object):
|
2016-07-20 19:00:34 +02:00
|
|
|
def __init__(self, session, url, write_key):
|
|
|
|
self.session = session
|
|
|
|
self.url = url
|
|
|
|
self.write_key = write_key
|
|
|
|
|
2016-12-01 19:13:12 +01:00
|
|
|
def post(self, endpoint, data):
|
|
|
|
# there is an issue with a timing condition with keep-alive
|
|
|
|
# that is best explained here: https://github.com/mikem23/keepalive-race
|
|
|
|
#
|
|
|
|
# If you make a request, wait just the right amount of time,
|
|
|
|
# then make another request, the requests module may opt to
|
|
|
|
# reuse the connection, but by the time the server gets it the
|
|
|
|
# timeout will have expired.
|
|
|
|
#
|
|
|
|
# by forcing the connection to close, we will disable the keep-alive.
|
|
|
|
assert endpoint[0] == '/'
|
|
|
|
headers = {"Connection": "close"}
|
|
|
|
return self.session.post(
|
|
|
|
self.url + endpoint, json=data, auth=self.auth, headers=headers)
|
|
|
|
|
2016-07-20 19:00:34 +02:00
|
|
|
@property
|
|
|
|
def auth(self):
|
|
|
|
return auth.HTTPBasicAuth(self.write_key, '')
|
|
|
|
|
|
|
|
@log_response
|
|
|
|
def batch(self, events):
|
|
|
|
"""Send multiple events in one request.
|
|
|
|
|
|
|
|
Each event needs to have its type specified.
|
|
|
|
"""
|
|
|
|
data = json.dumps({
|
|
|
|
'batch': events,
|
|
|
|
'sentAt': utils.now(),
|
|
|
|
})
|
|
|
|
log.debug('sending %s events', len(events))
|
|
|
|
log.debug('Data: %s', data)
|
2016-12-01 19:13:12 +01:00
|
|
|
return self.post('/batch', data)
|
2016-07-20 19:00:34 +02:00
|
|
|
|
|
|
|
@log_response
|
|
|
|
def track(self, event):
|
|
|
|
"""Send a single tracking event"""
|
|
|
|
log.debug('Sending track event: %s', event)
|
2016-12-01 19:13:12 +01:00
|
|
|
return self.post('/track', event)
|
2016-07-20 19:00:34 +02:00
|
|
|
|
|
|
|
@classmethod
|
2016-11-10 21:49:51 +01:00
|
|
|
def new_instance(cls, session=None):
|
2016-09-29 23:51:48 +02:00
|
|
|
"""Initialize an instance using values from the configuration"""
|
2016-07-20 19:00:34 +02:00
|
|
|
if not session:
|
2017-03-23 23:25:31 +01:00
|
|
|
session = Session()
|
2016-07-20 19:00:34 +02:00
|
|
|
return cls(
|
|
|
|
session,
|
2017-01-17 04:23:20 +01:00
|
|
|
conf.settings['ANALYTICS_ENDPOINT'],
|
|
|
|
utils.deobfuscate(conf.settings['ANALYTICS_TOKEN'])
|
2016-07-20 19:00:34 +02:00
|
|
|
)
|