-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.py
67 lines (47 loc) · 1.86 KB
/
async.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
class AsyncClient(BaseClient):
_client: httpx.AsyncClient
def __init__(self) -> None:
super().__init__(httpx.AsyncClient)
self._repository_manager: RepositoryManager = RepositoryManager(self)
async def _request(self, endpoint: str, return_type: str) -> Resource | Response | str:
# print("hello from async")
# print(self._client)
"""Send a HTTP GET request to the specified endpoint.
Args:
endpoint: The endpoint to send the request to.
return_type: The type of response to expect.
Returns:
TODO A string representation of the response (if `return_type` is "dataclass") or the full response object.
Raises:
TODO httpx.RequestError: If the request fails or times out.
ValueError: If an invalid `return_type` is provided.
"""
if return_type not in ALLOWED_RETURN_TYPES:
raise ValueError(f"Invalid `return_type`: {return_type}. Expected one of: {ALLOWED_RETURN_TYPES}")
http_response = await self._client.get(endpoint)
# print(http_response)
response = _build_response(http_response)
if return_type == "dataclass":
return response.parsed
if return_type == "xml":
return http_response.text
return response
@property
def repositories(self) -> RepositoryManager:
"""Get the repository manager for this client.
Returns:
The repository manager.
"""
return self._repository_manager
# ---
import asyncio
import re3data
from re3data import AsyncClient
repos = re3data.repositories.list()
async def main():
client = AsyncClient()
for repo in repos:
endpoint = repo.link.split("beta/")[-1]
response = await client._request(endpoint, "response")
print(response)
asyncio.run(main())