main
py 90 lines 3.21 KB
Raw
1 import asyncio
2 from typing import Any
3 from typing import Dict
4 from typing import Optional
5
6 import httpx
7 from loguru import logger
8
9
10 async def send_get_request(
11 endpoint: str,
12 headers: Optional[Dict[str, Any]] = None,
13 params: Optional[Dict[str, Any]] = None,
14 ) -> Dict[str, Any]:
15 """Send a GET request to the given endpoint.
16
17 Args:
18 endpoint (str): The endpoint to send the request to.
19 params (Optional[Dict[str, Any]], optional): The parameters to send with the request. Defaults to None.
20
21 Returns:
22 Dict[str, Any]: The response from the request.
23 """
24 async with httpx.AsyncClient() as client:
25 try:
26 response = await client.get(endpoint, params=params, headers=headers)
27 response.raise_for_status()
28 return {
29 "data": response.json(),
30 "success": True,
31 "message": "Successfully retrieved data",
32 }
33 except httpx.HTTPError as e:
34 return {"success": False, "message": f"Failed to retrieve data: {e}"}
35
36
37 async def send_post_request(
38 endpoint: str,
39 data: Dict[str, Any],
40 headers: Optional[Dict[str, Any]] = None,
41 ) -> Dict[str, Any]:
42 """
43 Send a POST request to the given endpoint.
44 """
45 async with httpx.AsyncClient() as client:
46 try:
47 logger.info(
48 f"Sending POST request to {endpoint} with data: {data} and headers: {headers}",
49 )
50 response = await client.post(endpoint, json=data, headers=headers)
51
52 if response.status_code == 429:
53 retry_after = int(response.headers.get("X-RateLimit-Reset", 1))
54 logger.warning(
55 f"Rate limit exceeded. Retrying after {retry_after} seconds.",
56 )
57 await asyncio.sleep(retry_after)
58 response = await client.post(endpoint, json=data, headers=headers)
59
60 if response.status_code != 200:
61 error_message = f"Request failed with status code {response.status_code}: {response.text}"
62 logger.error(error_message)
63 return {"success": False, "message": error_message}
64
65 content_type = response.headers.get("Content-Type", "")
66 logger.info(f"Content-Type: {content_type}")
67
68 if "application/json" in content_type:
69 logger.info(
70 f"Successfully retrieved data from {endpoint} with data: {data} and headers: {headers}",
71 )
72 return {
73 "data": response.json(),
74 "success": True,
75 "message": "Successfully retrieved data",
76 }
77 else:
78 logger.info(
79 f"Successfully retrieved data from {endpoint} with data: {data} and headers: {headers}",
80 )
81 return {
82 "data": response.content,
83 "success": True,
84 "message": "Successfully retrieved data",
85 }
86
87 except Exception as e:
88 error_message = f"Failed to send POST request: {str(e)}"
89 logger.error(error_message)
90 return {"success": False, "message": error_message}