Skip to content

Commit 9bf61cc

Browse files
committed
Add handling for 429 status code in AsyncHTTPClient and HTTPClient
1 parent 6cd020a commit 9bf61cc

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

recallrai/utils/async_http_client.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
Async HTTP client for making requests to the RecallrAI API.
33
"""
44

5-
import asyncio
65
from json import JSONDecodeError
7-
from httpx import Response, AsyncClient, TimeoutException, ConnectError, Limits
86
from typing import Any, Dict, Optional
7+
from httpx import Response, AsyncClient, TimeoutException, ConnectError, Limits
98
from ..exceptions import (
109
TimeoutError,
1110
ConnectionError,
@@ -132,23 +131,29 @@ async def request(
132131
return response # No content to parse
133132

134133
elif response.status_code == 422:
135-
detail = response.json().get("detail", "Validation error")
134+
detail = "Validation error"
136135
raise ValidationError(
137136
message=detail,
138137
http_status=response.status_code
139138
)
140139
elif response.status_code == 500:
141-
detail = response.json().get("detail", "Internal server error")
140+
detail = "Internal server error"
142141
raise InternalServerError(
143142
message=detail,
144143
http_status=response.status_code
145144
)
146145
elif response.status_code == 401:
147-
detail = response.json().get("detail", "Authentication failed")
146+
detail = "Authentication failed"
148147
raise AuthenticationError(
149148
message=detail,
150149
http_status=response.status_code
151150
)
151+
elif response.status_code == 429:
152+
detail = "Too many requests"
153+
raise ConnectionError(
154+
message=detail,
155+
http_status=response.status_code
156+
)
152157

153158
# Try to parse to JSON to catch JSON errors early
154159
_ = response.json()

recallrai/utils/http_client.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"""
44

55
from json import JSONDecodeError
6-
from httpx import Response, Client, TimeoutException, ConnectError, Limits
76
from typing import Any, Dict, Optional
7+
from httpx import Response, Client, TimeoutException, ConnectError, Limits
88
from ..exceptions import (
99
TimeoutError,
1010
ConnectionError,
@@ -96,24 +96,30 @@ def request(
9696
return response # No content to parse
9797

9898
elif response.status_code == 422:
99-
detail = response.json().get("detail", "Validation error")
99+
detail = "Validation error"
100100
raise ValidationError(
101101
message=detail,
102102
http_status=response.status_code
103103
)
104104
elif response.status_code == 500:
105-
detail = response.json().get("detail", "Internal server error")
105+
detail = "Internal server error"
106106
raise InternalServerError(
107107
message=detail,
108108
http_status=response.status_code
109109
)
110110
elif response.status_code == 401:
111-
detail = response.json().get("detail", "Authentication failed")
111+
detail = "Authentication failed"
112112
raise AuthenticationError(
113113
message=detail,
114114
http_status=response.status_code
115115
)
116-
116+
elif response.status_code == 429:
117+
detail = "Too many requests"
118+
raise ConnectionError(
119+
message=detail,
120+
http_status=response.status_code
121+
)
122+
117123
# Try to parse to JSON to catch JSON errors early
118124
_ = response.json()
119125

0 commit comments

Comments
 (0)