11from datetime import date , datetime
22from dateutil .tz import tzutc
3+ from typing import Any , Optional , Union
34import logging
45import json
56from gzip import GzipFile
6- from requests .auth import HTTPBasicAuth
77import requests
88from io import BytesIO
99
1616USER_AGENT = 'posthog-python/' + VERSION
1717
1818
19- def post (api_key , host = None , path = None , gzip = False , timeout = 15 , ** kwargs ):
19+ def post (api_key : str , host : Optional [ str ] = None , path = None , gzip : bool = False , timeout : int = 15 , ** kwargs ) -> requests . Response :
2020 """Post the `kwargs` to the API"""
2121 log = logging .getLogger ('posthog' )
2222 body = kwargs
@@ -43,10 +43,11 @@ def post(api_key, host=None, path=None, gzip=False, timeout=15, **kwargs):
4343
4444 if res .status_code == 200 :
4545 log .debug ('data uploaded successfully' )
46- return res
46+
47+ return res
4748
4849
49- def _process_response (res , success_message , return_json = True ):
50+ def _process_response (res : requests . Response , success_message : str , * , return_json : bool = True ) -> Union [ requests . Response , Any ] :
5051 log = logging .getLogger ('posthog' )
5152 if not res :
5253 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.' )
@@ -60,19 +61,19 @@ def _process_response(res, success_message, return_json=True):
6061 except ValueError :
6162 raise APIError (res .status_code , res .text )
6263
63- def decide (api_key , host = None , gzip = False , timeout = 15 , ** kwargs ):
64+ def decide (api_key : str , host : Optional [ str ] = None , gzip : bool = False , timeout : int = 15 , ** kwargs ) -> Any :
6465 """Post the `kwargs to the decide API endpoint"""
6566 res = post (api_key , host , '/decide/' , gzip , timeout , ** kwargs )
6667 return _process_response (res , success_message = 'Feature flags decided successfully' )
6768
6869
69- def batch_post (api_key , host = None , gzip = False , timeout = 15 , ** kwargs ):
70+ def batch_post (api_key : str , host : Optional [ str ] = None , gzip : bool = False , timeout : int = 15 , ** kwargs ) -> requests . Response :
7071 """Post the `kwargs` to the batch API endpoint for events"""
7172 res = post (api_key , host , '/batch/' , gzip , timeout , ** kwargs )
7273 return _process_response (res , success_message = 'data uploaded successfully' , return_json = False )
7374
7475
75- def get (api_key , url , host = None , timeout = None ):
76+ def get (api_key : str , url : str , host : Optional [ str ] = None , timeout : Optional [ int ] = None ) -> requests . Response :
7677 url = remove_trailing_slash (host or DEFAULT_HOST ) + url
7778 res = requests .get (
7879 url ,
@@ -86,7 +87,7 @@ def get(api_key, url, host=None, timeout=None):
8687
8788class APIError (Exception ):
8889
89- def __init__ (self , status , message ):
90+ def __init__ (self , status : Union [ int , str ], message : str ):
9091 self .message = message
9192 self .status = status
9293
@@ -96,7 +97,7 @@ def __str__(self):
9697
9798
9899class DatetimeSerializer (json .JSONEncoder ):
99- def default (self , obj ):
100+ def default (self , obj : Any ):
100101 if isinstance (obj , (date , datetime )):
101102 return obj .isoformat ()
102103
0 commit comments