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
@@ -47,7 +47,7 @@ def post(api_key, host=None, path=None, gzip=False, timeout=15, **kwargs):
4747 return res
4848
4949
50- 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 ] :
5151 log = logging .getLogger ('posthog' )
5252 if not res :
5353 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.' )
@@ -61,19 +61,19 @@ def _process_response(res, success_message, return_json=True):
6161 except ValueError :
6262 raise APIError (res .status_code , res .text )
6363
64- 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 :
6565 """Post the `kwargs to the decide API endpoint"""
6666 res = post (api_key , host , '/decide/' , gzip , timeout , ** kwargs )
6767 return _process_response (res , success_message = 'Feature flags decided successfully' )
6868
6969
70- 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 :
7171 """Post the `kwargs` to the batch API endpoint for events"""
7272 res = post (api_key , host , '/batch/' , gzip , timeout , ** kwargs )
7373 return _process_response (res , success_message = 'data uploaded successfully' , return_json = False )
7474
7575
76- 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 :
7777 url = remove_trailing_slash (host or DEFAULT_HOST ) + url
7878 res = requests .get (
7979 url ,
@@ -87,7 +87,7 @@ def get(api_key, url, host=None, timeout=None):
8787
8888class APIError (Exception ):
8989
90- def __init__ (self , status , message ):
90+ def __init__ (self , status : Union [ int , str ], message : str ):
9191 self .message = message
9292 self .status = status
9393
@@ -97,7 +97,7 @@ def __str__(self):
9797
9898
9999class DatetimeSerializer (json .JSONEncoder ):
100- def default (self , obj ):
100+ def default (self , obj : Any ):
101101 if isinstance (obj , (date , datetime )):
102102 return obj .isoformat ()
103103
0 commit comments