-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathrest_handler.py
44 lines (40 loc) · 1.32 KB
/
rest_handler.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import requests
from typing import List, Dict, Union, Optional
class Request:
proxies = None
@staticmethod
def post(url: str, data: List[Dict], params: Dict[str, Union[str, float]] = None) -> \
Optional[requests.Response]:
"""
Post request
:param params:
:param url:
:param data:
:return:
"""
try:
headers = {'content-type': 'application/json'}
r = requests.post(url, verify=False, params=params, json=data, headers=headers,
proxies=Request.proxies)
if r.status_code == 200:
return r
else:
raise ValueError(f"Http Request had non-200 Status: {r.status_code}", r.status_code)
except Exception as e:
print("Post Error\n", e)
raise
@staticmethod
def get(url: str, params: Dict[str, Union[str, float]], timeout: float = 1) -> Optional[requests.Response]:
"""
Get request
:param url:
:param params:
:param timeout:
:return:
"""
try:
data = requests.get(url=url, verify=False, params=params, timeout=timeout, proxies=Request.proxies)
return data
except Exception as e:
print("Get Error\n", e)
raise