Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7ee3002c6f | ||
|
|
d1bc9135c7 | ||
|
|
111813296c | ||
|
|
31acda73a3 | ||
|
|
888457387b | ||
|
|
fe45ff2ab0 | ||
|
|
221d7f09f3 |
@@ -12,3 +12,5 @@ env
|
||||
flake8.out
|
||||
pylint.out
|
||||
posthog-analytics
|
||||
.idea
|
||||
|
||||
|
||||
+15
-8
@@ -32,13 +32,15 @@ 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):
|
||||
require('api_key', api_key, string_types)
|
||||
sync_mode=False, timeout=15, thread=1, poll_interval=30, personal_api_key=None, project_api_key=None):
|
||||
|
||||
self.queue = queue.Queue(max_queue_size)
|
||||
|
||||
# api_key: This should be the Team API Key (token), public
|
||||
self.api_key = api_key
|
||||
self.api_key = api_key or project_api_key
|
||||
|
||||
require('api_key', self.api_key, string_types)
|
||||
|
||||
self.on_error = on_error
|
||||
self.debug = debug
|
||||
self.send = send
|
||||
@@ -228,11 +230,6 @@ 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:
|
||||
@@ -243,6 +240,11 @@ 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)
|
||||
@@ -250,6 +252,11 @@ 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()
|
||||
|
||||
+13
-27
@@ -46,13 +46,13 @@ def post(api_key, host=None, path=None, gzip=False, timeout=15, **kwargs):
|
||||
return res
|
||||
|
||||
|
||||
def decide(api_key, host=None, gzip=False, timeout=15, **kwargs):
|
||||
"""Post the `kwargs to the decide API endpoint"""
|
||||
def _process_response(res, success_message, return_json=True):
|
||||
log = logging.getLogger('posthog')
|
||||
res = post(api_key, host, '/decide/', gzip, timeout, **kwargs)
|
||||
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.')
|
||||
if res.status_code == 200:
|
||||
log.debug('Feature flags decided successfully')
|
||||
return res.json()
|
||||
log.debug(success_message)
|
||||
return res.json() if return_json else res
|
||||
try:
|
||||
payload = res.json()
|
||||
log.debug('received response: %s', payload)
|
||||
@@ -60,27 +60,21 @@ def decide(api_key, host=None, gzip=False, timeout=15, **kwargs):
|
||||
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)
|
||||
|
||||
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)
|
||||
return _process_response(res, success_message='data uploaded successfully', return_json=False)
|
||||
|
||||
|
||||
def get(api_key, url, host=None, timeout=None):
|
||||
log = logging.getLogger('posthog')
|
||||
url = remove_trailing_slash(host or DEFAULT_HOST) + url
|
||||
response = requests.get(
|
||||
res = requests.get(
|
||||
url,
|
||||
headers={
|
||||
'Authorization': 'Bearer %s' % api_key,
|
||||
@@ -88,15 +82,7 @@ def get(api_key, url, host=None, timeout=None):
|
||||
},
|
||||
timeout=timeout
|
||||
)
|
||||
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)
|
||||
|
||||
return _process_response(res, success_message=f'GET {url} completed successfully')
|
||||
|
||||
class APIError(Exception):
|
||||
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
VERSION = '1.1.2'
|
||||
VERSION = '1.1.3'
|
||||
|
||||
Reference in New Issue
Block a user