-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepositories.py
More file actions
58 lines (50 loc) · 2.01 KB
/
Copy pathrepositories.py
File metadata and controls
58 lines (50 loc) · 2.01 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
from datetime import timedelta
from fastapi import Depends
from httpx import AsyncClient
from httpx._models import Response
from pydantic.error_wrappers import ValidationError
from tenacity import retry
from tenacity.stop import stop_after_attempt
from tenacity.wait import wait_fixed
from wacruit.src.apps.hodu.schemas import HoduSubmitErrorResponse
from wacruit.src.apps.hodu.schemas import HoduSubmitRequest
from wacruit.src.apps.hodu.schemas import HoduSubmitResponse
from wacruit.src.utils.mixins import LoggingMixin
from .connections import get_hodu_api_client
HTTP_CLIENT_ERROR_STATUS_CODE = 400
class HoduApiRepository(LoggingMixin):
def __init__(self, client: AsyncClient = Depends(get_hodu_api_client)):
self.client = client
@retry(stop=stop_after_attempt(5), wait=wait_fixed(timedelta(seconds=1)))
async def submit(
self, request: HoduSubmitRequest
) -> HoduSubmitResponse | HoduSubmitErrorResponse:
res = await self.client.post(
url="/api/v1/submit",
json=request.dict(),
timeout=60,
)
return self._parse_response(res)
def _parse_response(
self, response: Response
) -> HoduSubmitResponse | HoduSubmitErrorResponse:
try:
if response.status_code >= HTTP_CLIENT_ERROR_STATUS_CODE:
self.logger.error(
"HODU API ERROR for sending %s / status code: %d / response: %s",
response.url,
response.status_code,
response.json(),
)
return HoduSubmitErrorResponse(**response.json())
return HoduSubmitResponse(**response.json())
except ValidationError as e:
self.logger.error(
"HODU API RESPONSE PARSING ERROR for sending %s / status code: %d / "
"response: %s / error: %s",
response.url,
response.status_code,
response.json(),
e,
)
raise e