-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathhttp_utils.py
71 lines (58 loc) · 2.12 KB
/
http_utils.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
from typing import Any, List, Union, Dict, Optional
import aiohttp
async def post_async_request(url: str, data: Any = None) -> List[Union[int, Any]]:
"""Post request with the data provided to the url provided.
Parameters
----------
url: str
url to make the post to
data: Any
object to post
Returns
-------
[int, Any]
Tuple with the Response status code and the data returned from the request
"""
async with aiohttp.ClientSession() as session:
async with session.post(url,
json=data) as response:
# We disable aiohttp's input type validation
# as the server may respond with alternative
# data encodings. This is potentially unsafe.
# More here: https://docs.aiohttp.org/en/stable/client_advanced.html
data = await response.json(content_type=None)
return [response.status, data]
async def get_async_request(url: str, headers: Dict[Any, Any] = None) -> List[Any]:
"""Get the data from the url provided.
Parameters
----------
url: str
url to get the data from
headers: Dict[str, str]
headers to send with the request
Returns
-------
[int, Any]
Tuple with the Response status code and the data returned from the request
"""
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(url) as response:
data = await response.json(content_type=None)
if data is None:
data = ""
return [response.status, data]
async def delete_async_request(url: str) -> List[Union[int, Any]]:
"""Delete the data from the url provided.
Parameters
----------
url: str
url to delete the data from
Returns
-------
[int, Any]
Tuple with the Response status code and the data returned from the request
"""
async with aiohttp.ClientSession() as session:
async with session.delete(url) as response:
data = await response.json(content_type=None)
return [response.status, data]