-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_client.py
More file actions
83 lines (54 loc) · 2.22 KB
/
http_client.py
File metadata and controls
83 lines (54 loc) · 2.22 KB
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
import asyncio
import httpx
_sync_client: httpx.Client | None = None
_async_client: httpx.AsyncClient | None = None
_async_client_loop: asyncio.AbstractEventLoop | None = None
def _get_sync_client() -> httpx.Client:
global _sync_client
if _sync_client is None:
_sync_client = httpx.Client(timeout=None)
return _sync_client
def _get_async_client() -> httpx.AsyncClient:
global _async_client, _async_client_loop
current_loop = asyncio.get_running_loop()
if _async_client is None or _async_client_loop is not current_loop:
_async_client = httpx.AsyncClient(timeout=None)
_async_client_loop = current_loop
return _async_client
def post(url, **kwargs) -> httpx.Response:
return _get_sync_client().post(url, **kwargs)
async def async_post(url, **kwargs) -> httpx.Response:
return await _get_async_client().post(url, **kwargs)
def get(url, **kwargs) -> httpx.Response:
return _get_sync_client().get(url, **kwargs)
async def async_get(url, **kwargs) -> httpx.Response:
return await _get_async_client().get(url, **kwargs)
def patch(url, **kwargs) -> httpx.Response:
return _get_sync_client().patch(url, **kwargs)
async def async_patch(url, **kwargs) -> httpx.Response:
return await _get_async_client().patch(url, **kwargs)
def delete(url, **kwargs) -> httpx.Response:
return _get_sync_client().delete(url, **kwargs)
async def async_delete(url, **kwargs) -> httpx.Response:
return await _get_async_client().delete(url, **kwargs)
def request(method, url, **kwargs) -> httpx.Response:
return _get_sync_client().request(method, url, **kwargs)
async def async_request(method, url, **kwargs) -> httpx.Response:
return await _get_async_client().request(method, url, **kwargs)
def close():
global _sync_client
if _sync_client is not None:
_sync_client.close()
_sync_client = None
async def close_async():
global _sync_client, _async_client, _async_client_loop
close()
client = _async_client
client_loop = _async_client_loop
_async_client = None
_async_client_loop = None
if client is None:
return
current_loop = asyncio.get_running_loop()
if client_loop is current_loop:
await client.aclose()