def post(api_key: str, host: Optional[str] = None, path=None, gzip: bool = False, timeout: int = 15, **kwargs) -> requests.Response: """Post the `kwargs` to the API""" log = logging.getLogger("posthog") body = kwargs body["sentAt"] = datetime.utcnow().replace(tzinfo=tzutc()).isoformat() url = remove_trailing_slash(host or DEFAULT_HOST) + path body["api_key"] = api_key data = json.dumps(body, cls=DatetimeSerializer) log.debug("making request: %s", data) headers = {"Content-Type": "application/json", "User-Agent": USER_AGENT} if gzip: headers["Content-Encoding"] = "gzip" buf = BytesIO() with GzipFile(fileobj=buf, mode="w") as gz: # 'data' was produced by json.dumps(), # whose default encoding is utf-8. gz.write(data.encode("utf-8")) data = buf.getvalue() res = _session.post(url, data=data, headers=headers, timeout=timeout) if res.status_code == 200: log.debug("data uploaded successfully") return res
def post(api_key, host=None, gzip=False, timeout=15, **kwargs): """Post the `kwargs` to the API""" log = logging.getLogger('posthog') body = kwargs body["sentAt"] = datetime.utcnow().replace(tzinfo=tzutc()).isoformat() url = remove_trailing_slash(host or 'https://t.posthog.com') + '/batch/' body['api_key'] = api_key data = json.dumps(body, cls=DatetimeSerializer) log.debug('making request: %s', data) headers = { 'Content-Type': 'application/json', 'User-Agent': 'analytics-python/' + VERSION } if gzip: headers['Content-Encoding'] = 'gzip' buf = BytesIO() with GzipFile(fileobj=buf, mode='w') as gz: # 'data' was produced by json.dumps(), # whose default encoding is utf-8. gz.write(data.encode('utf-8')) data = buf.getvalue() res = _session.post(url, data=data, headers=headers, timeout=timeout) 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['code'], payload['message']) except ValueError: raise APIError(res.status_code, 'unknown', res.text)
def get(api_key, url, host=None, timeout=None): url = remove_trailing_slash(host or DEFAULT_HOST) + url res = requests.get(url, headers={ 'Authorization': 'Bearer %s' % api_key, 'User-Agent': USER_AGENT }, timeout=timeout) return _process_response( res, success_message=f'GET {url} completed successfully')
def get(api_key: str, url: str, host: Optional[str] = None, timeout: Optional[int] = None) -> requests.Response: url = remove_trailing_slash(host or DEFAULT_HOST) + url res = requests.get(url, headers={ "Authorization": "Bearer %s" % api_key, "User-Agent": USER_AGENT }, timeout=timeout) return _process_response( res, success_message=f"GET {url} completed successfully")
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(url, headers={ 'Authorization': 'Bearer %s' % api_key, 'User-Agent': USER_AGENT }, 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)
def post(api_key, host=None, path=None, gzip=False, timeout=15, **kwargs): """Post the `kwargs` to the API""" log = logging.getLogger('posthog') body = kwargs body["sentAt"] = datetime.utcnow().replace(tzinfo=tzutc()).isoformat() url = remove_trailing_slash(host or DEFAULT_HOST) + path body['api_key'] = api_key data = json.dumps(body, cls=DatetimeSerializer) log.debug('making request: %s', data) headers = {'Content-Type': 'application/json', 'User-Agent': USER_AGENT} if gzip: headers['Content-Encoding'] = 'gzip' buf = BytesIO() with GzipFile(fileobj=buf, mode='w') as gz: # 'data' was produced by json.dumps(), # whose default encoding is utf-8. gz.write(data.encode('utf-8')) data = buf.getvalue() res = _session.post(url, data=data, headers=headers, timeout=timeout) if res.status_code == 200: log.debug('data uploaded successfully') return res
def test_remove_slash(self): self.assertEqual('http://posthog.io', utils.remove_trailing_slash('http://posthog.io/')) self.assertEqual('http://posthog.io', utils.remove_trailing_slash('http://posthog.io'))
def test_remove_slash(self): self.assertEqual("http://posthog.io", utils.remove_trailing_slash("http://posthog.io/")) self.assertEqual("http://posthog.io", utils.remove_trailing_slash("http://posthog.io"))