-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathadapters.py
95 lines (73 loc) · 2.61 KB
/
adapters.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
from urllib.parse import urlparse
from random import randint
import requests
from requests.adapters import BaseAdapter, HTTPAdapter
class FileAdapter(BaseAdapter):
def send(
self,
request: requests.PreparedRequest,
stream: bool = False,
timeout: None | float | tuple[float, float] | tuple[float, None] = None,
verify: bool | str = True,
cert: None | bytes | str | tuple[bytes | str, bytes | str] = None,
proxies: dict[str, str] | None = None,
) -> requests.Response:
file_name = urlparse(request.url).path
rs = requests.Response()
rs.status_code = 200
rs.raw = open(file_name, "rb")
rs.request = request
return rs
def close(self) -> None:
pass
class DuckDuckGoAdapter(HTTPAdapter):
def send(
self,
request: requests.PreparedRequest,
stream: bool = False,
timeout: None | float | tuple[float, float] | tuple[float, None] = None,
verify: bool | str = True,
cert: None | bytes | str | tuple[bytes | str, bytes | str] = None,
proxies: dict[str, str] | None = None,
) -> requests.Response:
query = urlparse(request.path_url).path
url = f"https://api.duckduckgo.com/?q={query}&format=json"
return requests.get(url) # TODO:
class RandomColorAdapter(BaseAdapter):
def send(
self,
request: requests.PreparedRequest,
stream: bool = False,
timeout: None | float | tuple[float, float] | tuple[float, None] = None,
verify: bool | str = True,
cert: None | bytes | str | tuple[bytes | str, bytes | str] = None,
proxies: dict[str, str] | None = None,
) -> requests.Response:
r, g, b = map(
lambda x: hex(x)[2:].zfill(2),
(randint(0, 255), randint(0, 255), randint(0, 255)),
)
rs = requests.Response()
rs.status_code = 200
rs._content = f"#{r}{g}{b}".encode()
rs.request = request
return rs
def close(self) -> None:
pass
s = requests.session()
s.mount("file:", FileAdapter())
s.mount("duckduckgo:", DuckDuckGoAdapter())
s.mount("randomcolor:", RandomColorAdapter())
file_name = __file__
rs = s.get("file:" + file_name)
print(rs.content)
# b"#!/usr/bin/env python3\r\n# -*- coding: utf-8 -*-\r\n\r\n__author__ = 'ipetrash' ...
rs = s.get("duckduckgo:" + "Dark Souls")
print(rs.json())
# {'Abstract': "Dark Souls is a series of action role-playing games created by Hidet ...
rs = s.get("randomcolor:")
print(rs.content)
# b'#535ebd'