make analytics use treq

This commit is contained in:
Victor Shyba 2018-05-05 00:50:42 -03:00
parent 1077d8f937
commit d03fc80eac

View file

@ -1,8 +1,8 @@
import collections import collections
import logging import logging
import treq
from twisted.internet import defer, task from twisted.internet import defer, task
from requests import auth
from txrequests import Session
from lbrynet import conf from lbrynet import conf
from lbrynet.core import looping_call_manager, utils, system_info from lbrynet.core import looping_call_manager, utils, system_info
@ -216,8 +216,8 @@ class Manager(object):
class Api(object): class Api(object):
def __init__(self, session, url, write_key, enabled): def __init__(self, cookies, url, write_key, enabled):
self.session = session self.cookies = cookies
self.url = url self.url = url
self._write_key = write_key self._write_key = write_key
self._enabled = enabled self._enabled = enabled
@ -232,14 +232,17 @@ class Api(object):
# timeout will have expired. # timeout will have expired.
# #
# by forcing the connection to close, we will disable the keep-alive. # by forcing the connection to close, we will disable the keep-alive.
def update_cookies(response):
self.cookies.update(response.cookies())
return response
assert endpoint[0] == '/' assert endpoint[0] == '/'
headers = {"Connection": "close"} headers = {b"Connection": b"close"}
return self.session.post( d = treq.post(self.url + endpoint, auth=(self._write_key, ''), json=data,
self.url + endpoint, headers=headers, cookies=self.cookies)
json=data, d.addCallback(update_cookies)
auth=auth.HTTPBasicAuth(self._write_key, ''), return d
headers=headers
)
def track(self, event): def track(self, event):
"""Send a single tracking event""" """Send a single tracking event"""
@ -257,11 +260,10 @@ class Api(object):
@classmethod @classmethod
def new_instance(cls, enabled=None): def new_instance(cls, enabled=None):
"""Initialize an instance using values from the configuration""" """Initialize an instance using values from the configuration"""
session = Session()
if enabled is None: if enabled is None:
enabled = conf.settings['share_usage_data'] enabled = conf.settings['share_usage_data']
return cls( return cls(
session, {},
conf.settings['ANALYTICS_ENDPOINT'], conf.settings['ANALYTICS_ENDPOINT'],
utils.deobfuscate(conf.settings['ANALYTICS_TOKEN']), utils.deobfuscate(conf.settings['ANALYTICS_TOKEN']),
enabled, enabled,