1
+ import json
2
+
1
3
from proxy_py import settings
2
4
from fake_useragent import UserAgent
3
5
from aiosocks .connector import ProxyConnector , ProxyClientRequest
4
6
5
7
import aiohttp
6
8
7
9
10
+ class HttpClientResult :
11
+ text = None
12
+ aiohttp_response = None
13
+
14
+ @staticmethod
15
+ async def make (aiohttp_response ):
16
+ obj = HttpClientResult ()
17
+ obj .aiohttp_response = aiohttp_response
18
+ obj .text = await obj .aiohttp_response .text ()
19
+
20
+ return obj
21
+
22
+ def as_text (self ):
23
+ return self .text
24
+
25
+ def as_json (self ):
26
+ return json .loads (self .text )
27
+
28
+
8
29
# TODO: complete cookies saving
9
30
class HttpClient :
10
31
"""
@@ -31,7 +52,7 @@ async def get(self, url):
31
52
:param url:
32
53
:return:
33
54
"""
34
- pass
55
+ return await self . request ( 'GET' , url , None )
35
56
36
57
async def post (self , url , data ):
37
58
"""
@@ -41,12 +62,11 @@ async def post(self, url, data):
41
62
:param data:
42
63
:return:
43
64
"""
44
- raise NotImplementedError ( )
65
+ return await self . request ( 'POST' , url , data )
45
66
46
67
async def request (self , method , url , data ) -> HttpClientResult :
47
68
headers = {
48
69
'User-Agent' : self .user_agent ,
49
-
50
70
}
51
71
52
72
async with aiohttp .ClientSession (connector = HttpClient ._aiohttp_connector ,
@@ -55,37 +75,26 @@ async def request(self, method, url, data) -> HttpClientResult:
55
75
) as session :
56
76
async with session .request (method ,
57
77
url = url ,
78
+ data = data ,
58
79
proxy = self .proxy_address ,
59
80
timeout = self .timeout ,
60
81
headers = headers ) as response :
61
- return HttpClientResult .make (response )
82
+ return await HttpClientResult .make (response )
62
83
63
84
@staticmethod
64
85
async def clean ():
65
86
HttpClient ._aiohttp_connector .close ()
66
87
67
88
68
- class HttpClientResult :
69
- text = None
70
- aiohttp_response = None
71
-
72
- @staticmethod
73
- async def make (aiohttp_response ):
74
- obj = HttpClientResult ()
75
- obj .aiohttp_response = aiohttp_response
76
- obj .text = await obj .aiohttp_response .text ()
77
-
78
- return obj
79
-
80
- def as_text (self ):
81
- return self .text
82
-
83
-
84
89
async def get_text (url ):
85
90
"""
86
- fast method for getting get response without creating extra objects
91
+ fast method for sending get response without creating extra objects
87
92
88
93
:param url:
89
94
:return:
90
95
"""
91
96
return (await HttpClient ().get (url )).as_text ()
97
+
98
+
99
+ async def get_json (url ):
100
+ return (await HttpClient ().get (url )).as_json ()
0 commit comments