-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpclient.py
73 lines (64 loc) · 2.59 KB
/
httpclient.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import httpx
default_header: dict = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/80.0.3987.163 "
"Safari/537.36",
"Accept-Language": "en-GB,en;q=0.5",
}
class HttpClient:
def __init__(self, headers: dict = None, cookies: dict = None):
if headers is None:
headers = default_header
self.session = httpx.AsyncClient(timeout=10.0, headers=headers, cookies=cookies)
async def get(self, page: str, redirects: bool = False) -> httpx.Response:
print(page)
try:
req = await self.session.get(page, follow_redirects=redirects)
self.session.headers["Referer"] = page
except Exception as e:
print(
f"Error: {e}",
"\n Please open an issue if this is not due due to your internet connection",
)
return req
async def post(self, page: str, data: dict = None, json=None) -> httpx.Response:
print(page)
if json is None:
try:
req = await self.session.post(page, data=data)
self.session.headers["Referer"] = page
except Exception as e:
print(
f"Error: {e}",
"\n Please open an issue if this is not due due to your internet connection",
)
return req
else:
try:
req = await self.session.post(page, json=json)
self.session.headers["Referer"] = page
except Exception as e:
print(
f"Error: {e}",
"\n Please open an issue if this is not due due to your internet connection",
)
return req
async def head(self, page: str, redirects: False) -> httpx.Response:
print(page)
try:
req = await self.session.head(page, follow_redirects=redirects)
self.session.headers["Referer"] = page
except Exception as e:
print(
f"Error: {e}",
"\n Please open an issue if this is not due due to your internet connection",
)
return req
def set_headers(self, header: dict) -> None:
self.session.headers = header
# do not use this!w
def set_cookies(self, cookies: dict) -> None:
self.session.cookies = cookies
def add_elem(self, elements: dict) -> None:
for i in elements.items():
self.session.headers[i[0]] = i[1]