Skip to content

Commit aace39a

Browse files
committed
http client test
1 parent 3435a72 commit aace39a

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

http_client.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
1+
import json
2+
13
from proxy_py import settings
24
from fake_useragent import UserAgent
35
from aiosocks.connector import ProxyConnector, ProxyClientRequest
46

57
import aiohttp
68

79

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+
829
# TODO: complete cookies saving
930
class HttpClient:
1031
"""
@@ -31,7 +52,7 @@ async def get(self, url):
3152
:param url:
3253
:return:
3354
"""
34-
pass
55+
return await self.request('GET', url, None)
3556

3657
async def post(self, url, data):
3758
"""
@@ -41,12 +62,11 @@ async def post(self, url, data):
4162
:param data:
4263
:return:
4364
"""
44-
raise NotImplementedError()
65+
return await self.request('POST', url, data)
4566

4667
async def request(self, method, url, data) -> HttpClientResult:
4768
headers = {
4869
'User-Agent': self.user_agent,
49-
5070
}
5171

5272
async with aiohttp.ClientSession(connector=HttpClient._aiohttp_connector,
@@ -55,37 +75,26 @@ async def request(self, method, url, data) -> HttpClientResult:
5575
) as session:
5676
async with session.request(method,
5777
url=url,
78+
data=data,
5879
proxy=self.proxy_address,
5980
timeout=self.timeout,
6081
headers=headers) as response:
61-
return HttpClientResult.make(response)
82+
return await HttpClientResult.make(response)
6283

6384
@staticmethod
6485
async def clean():
6586
HttpClient._aiohttp_connector.close()
6687

6788

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-
8489
async def get_text(url):
8590
"""
86-
fast method for getting get response without creating extra objects
91+
fast method for sending get response without creating extra objects
8792
8893
:param url:
8994
:return:
9095
"""
9196
return (await HttpClient().get(url)).as_text()
97+
98+
99+
async def get_json(url):
100+
return (await HttpClient().get(url)).as_json()

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ recommonmark
1010
sphinx_rtd_theme
1111
py_mini_racer
1212
pytest
13+
pytest-asyncio

tests/test_http_client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import http_client
2+
import pytest
3+
4+
5+
@pytest.mark.asyncio
6+
async def test_fast_methods():
7+
resp = await http_client.get_json('https://ipinfo.io/json')
8+
assert 'ip' in resp
9+
10+
11+
def test_saving_state():
12+
# TODO: implement
13+
pass

0 commit comments

Comments
 (0)