Skip to content

Commit e6f71e4

Browse files
authored
Merge pull request #20 from Hungsiro506/patch-1
Fix post() returning None on non-200 response
2 parents 05fde2a + 17874cb commit e6f71e4

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

.gitignore

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
**sublime**
22
*.pyc
3-
dist
3+
dist/
44
*.egg-info
5-
dist
65
MANIFEST
7-
build
8-
.eggs
9-
.coverage
6+
build/
7+
.eggs/
8+
.coverage/
109
.vscode/
11-
env
10+
env/
11+
venv/
1212
flake8.out
1313
pylint.out
1414
posthog-analytics
1515
.idea
16-

posthog/request.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from datetime import date, datetime
22
from dateutil.tz import tzutc
3+
from typing import Any, Optional, Union
34
import logging
45
import json
56
from gzip import GzipFile
6-
from requests.auth import HTTPBasicAuth
77
import requests
88
from io import BytesIO
99

@@ -16,7 +16,7 @@
1616
USER_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

8788
class 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

9899
class 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

Comments
 (0)