Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d52b605742 | ||
|
|
a5f2e030b5 |
@@ -12,5 +12,3 @@ env
|
||||
flake8.out
|
||||
pylint.out
|
||||
posthog-analytics
|
||||
.idea
|
||||
|
||||
|
||||
+8
-15
@@ -32,15 +32,13 @@ class Client(object):
|
||||
def __init__(self, api_key=None, host=None, debug=False,
|
||||
max_queue_size=10000, send=True, on_error=None, flush_at=100,
|
||||
flush_interval=0.5, gzip=False, max_retries=3,
|
||||
sync_mode=False, timeout=15, thread=1, poll_interval=30, personal_api_key=None, project_api_key=None):
|
||||
sync_mode=False, timeout=15, thread=1, poll_interval=30, personal_api_key=None):
|
||||
require('api_key', api_key, string_types)
|
||||
|
||||
self.queue = queue.Queue(max_queue_size)
|
||||
|
||||
# api_key: This should be the Team API Key (token), public
|
||||
self.api_key = api_key or project_api_key
|
||||
|
||||
require('api_key', self.api_key, string_types)
|
||||
|
||||
self.api_key = api_key
|
||||
self.on_error = on_error
|
||||
self.debug = debug
|
||||
self.send = send
|
||||
@@ -230,6 +228,11 @@ class Client(object):
|
||||
self.join()
|
||||
|
||||
def _load_feature_flags(self):
|
||||
if not self.personal_api_key:
|
||||
self.log.warning('[FEATURE FLAGS] You have to specify a personal_api_key to use feature flags.')
|
||||
self.feature_flags = []
|
||||
return
|
||||
|
||||
try:
|
||||
self.feature_flags = get(self.personal_api_key, '/api/feature_flag/', self.host)['results']
|
||||
except APIError as e:
|
||||
@@ -240,11 +243,6 @@ class Client(object):
|
||||
'To use feature flags, please set a personal_api_key ' \
|
||||
'More information: https://posthog.com/docs/api/overview'
|
||||
)
|
||||
else:
|
||||
raise APIError(
|
||||
status=e.status,
|
||||
message=e.message
|
||||
)
|
||||
except Exception as e:
|
||||
self.log.warning('[FEATURE FLAGS] Fetching feature flags failed with following error. We will retry in %s seconds.' % self.poll_interval)
|
||||
self.log.warning(e)
|
||||
@@ -252,11 +250,6 @@ class Client(object):
|
||||
self._last_feature_flag_poll = datetime.utcnow().replace(tzinfo=tzutc())
|
||||
|
||||
def load_feature_flags(self):
|
||||
if not self.personal_api_key:
|
||||
self.log.warning('[FEATURE FLAGS] You have to specify a personal_api_key to use feature flags.')
|
||||
self.feature_flags = []
|
||||
return
|
||||
|
||||
self._load_feature_flags()
|
||||
poller = Poller(interval=timedelta(seconds=self.poll_interval), execute=self._load_feature_flags)
|
||||
poller.start()
|
||||
|
||||
+27
-13
@@ -46,13 +46,13 @@ def post(api_key, host=None, path=None, gzip=False, timeout=15, **kwargs):
|
||||
return res
|
||||
|
||||
|
||||
def _process_response(res, success_message, return_json=True):
|
||||
def decide(api_key, host=None, gzip=False, timeout=15, **kwargs):
|
||||
"""Post the `kwargs to the decide API endpoint"""
|
||||
log = logging.getLogger('posthog')
|
||||
if not res:
|
||||
raise APIError('N/A', 'Error when fetching PostHog API, please make sure you are using your public project token/key and not a private API key.')
|
||||
res = post(api_key, host, '/decide/', gzip, timeout, **kwargs)
|
||||
if res.status_code == 200:
|
||||
log.debug(success_message)
|
||||
return res.json() if return_json else res
|
||||
log.debug('Feature flags decided successfully')
|
||||
return res.json()
|
||||
try:
|
||||
payload = res.json()
|
||||
log.debug('received response: %s', payload)
|
||||
@@ -60,21 +60,27 @@ def _process_response(res, success_message, return_json=True):
|
||||
except ValueError:
|
||||
raise APIError(res.status_code, res.text)
|
||||
|
||||
def decide(api_key, host=None, gzip=False, timeout=15, **kwargs):
|
||||
"""Post the `kwargs to the decide API endpoint"""
|
||||
res = post(api_key, host, '/decide/', gzip, timeout, **kwargs)
|
||||
return _process_response(res, success_message='Feature flags decided successfully')
|
||||
|
||||
|
||||
def batch_post(api_key, host=None, gzip=False, timeout=15, **kwargs):
|
||||
"""Post the `kwargs` to the batch API endpoint for events"""
|
||||
log = logging.getLogger('posthog')
|
||||
res = post(api_key, host, '/batch/', gzip, timeout, **kwargs)
|
||||
return _process_response(res, success_message='data uploaded successfully', return_json=False)
|
||||
|
||||
if res.status_code == 200:
|
||||
log.debug('data uploaded successfully')
|
||||
return res
|
||||
try:
|
||||
payload = res.json()
|
||||
log.debug('received response: %s', payload)
|
||||
raise APIError(res.status_code, payload['detail'])
|
||||
except ValueError:
|
||||
raise APIError(res.status_code, res.text)
|
||||
|
||||
|
||||
def get(api_key, url, host=None, timeout=None):
|
||||
log = logging.getLogger('posthog')
|
||||
url = remove_trailing_slash(host or DEFAULT_HOST) + url
|
||||
res = requests.get(
|
||||
response = requests.get(
|
||||
url,
|
||||
headers={
|
||||
'Authorization': 'Bearer %s' % api_key,
|
||||
@@ -82,7 +88,15 @@ def get(api_key, url, host=None, timeout=None):
|
||||
},
|
||||
timeout=timeout
|
||||
)
|
||||
return _process_response(res, success_message=f'GET {url} completed successfully')
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
try:
|
||||
payload = response.json()
|
||||
log.debug('received response: %s', payload)
|
||||
raise APIError(response.status_code, payload['detail'])
|
||||
except ValueError:
|
||||
raise APIError(response.status_code, response.text)
|
||||
|
||||
|
||||
class APIError(Exception):
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from datetime import date, datetime, timedelta
|
||||
from decimal import Decimal
|
||||
from uuid import UUID
|
||||
import unittest
|
||||
|
||||
from dateutil.tz import tzutc
|
||||
@@ -50,6 +51,9 @@ class TestUtils(unittest.TestCase):
|
||||
utils.clean(combined)
|
||||
self.assertEqual(combined.keys(), pre_clean_keys)
|
||||
|
||||
# test UUID separately, as the UUID object doesn't equal its string representation according to Python
|
||||
self.assertEqual(utils.clean(UUID('12345678123456781234567812345678')), '12345678-1234-5678-1234-567812345678')
|
||||
|
||||
def test_clean_with_dates(self):
|
||||
dict_with_dates = {
|
||||
'birthdate': date(1980, 1, 1),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from dateutil.tz import tzlocal, tzutc
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
from uuid import UUID
|
||||
import logging
|
||||
import numbers
|
||||
|
||||
@@ -47,6 +48,8 @@ def remove_trailing_slash(host):
|
||||
def clean(item):
|
||||
if isinstance(item, Decimal):
|
||||
return float(item)
|
||||
if isinstance(item, UUID):
|
||||
return str(item)
|
||||
elif isinstance(item, (six.string_types, bool, numbers.Number, datetime,
|
||||
date, type(None))):
|
||||
return item
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
VERSION = '1.1.3'
|
||||
VERSION = '1.1.2'
|
||||
|
||||
Reference in New Issue
Block a user