-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequestHandler.py
28 lines (22 loc) · 961 Bytes
/
RequestHandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import json
from requests import Response
from requests.exceptions import ConnectionError
class RequestHandler:
def __init__(self, requester):
self.requester = requester
def get_jsondict(self, url):
response = self.perform_get_request(url)
decoded_string = response.content.decode("utf-8")
return json.loads(decoded_string)
def perform_get_request(self, url) -> Response:
response = self.requester.get(url=url)
if str(response.status_code)[:1] != '2':
raise ConnectionError(f'status {response.status_code}')
return response
def perform_post_request(self, url, json_data=None, **kwargs) -> Response:
if json_data is not None:
kwargs['json'] = json_data
response = self.requester.post(url=url, **kwargs)
if str(response.status_code)[:1] != '2':
raise ConnectionError(f'status {response.status_code}')
return response