-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HTTP Requests response status code handlings #42
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,12 +22,22 @@ def __init__(self, request, log=None, message="", msg_args=()): | |
message = f"Request %s: %s + %s {message}" | ||
log(message, request.method, request.url, request.cookies) | ||
|
||
class RLRequestFailed(RateLimiterError): | ||
def __init__(self, request, *args, **kwargs): | ||
super().__init__(request, *args, **kwargs) | ||
|
||
class RLRequestClientError(RLRequestFailed): | ||
def __init__(self, request, *args, **kwargs): | ||
super().__init__(request, *args, **kwargs) | ||
Comment on lines
+25
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe
Comment on lines
+29
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also the name |
||
|
||
class RLErrorWithPause(RateLimiterError): | ||
def __init__(self, request, time_to_wait, *args, **kwargs): | ||
super().__init__(request, *args, **kwargs) | ||
self.time_to_wait = time_to_wait | ||
|
||
class RLRequestServerError(RLErrorWithPause): | ||
def __init__(self, request, time_to_wait, *args, **kwargs): | ||
super().__init__(request, time_to_wait, *args, **kwargs) | ||
|
||
# pylint: disable=too-few-public-methods | ||
class RequestEntry: | ||
|
@@ -101,6 +111,21 @@ def handle_get_request(self, request): | |
match resp.status_code: | ||
case 200: | ||
return resp.json() | ||
case 400, 401, 403, 404, 405: | ||
# HTTP failure client side | ||
raise RLRequestClientError( | ||
request, | ||
self.logger.error, | ||
f"40X error code, client side error, useless to retry." | ||
) | ||
case 500, 501, 502, 503, 504: | ||
# HTTP failure server side | ||
raise RLRequestServerError( | ||
request, | ||
300, | ||
self.logger.error, | ||
f"50X error code, server side error, retry in 5 min ..." | ||
) | ||
case 429: | ||
try: | ||
time_to_wait = int(resp.headers["Retry-After"]) | ||
|
@@ -163,6 +188,14 @@ async def handle_requests(self): | |
|
||
try: | ||
self.requests[request.key]["result"] = self.handle_get_request(request) | ||
except RLRequestFailed as exc: | ||
# Request failed and should not be sent again | ||
self.requests[request.key]["result"] = None | ||
# set the exception | ||
self.requests[request.key]["exception"] = ( | ||
RLRequestFailed(request, self.logger.error, "Request Failed"), | ||
exc, | ||
) | ||
except RateLimiterError as exc: | ||
if isinstance(exc, RLErrorWithPause): | ||
await self.wait(exc.time_to_wait) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm not mistaken the whole point of this exception is to allow future subclassing (because today only
RLRequestClientError
inherits from it)However it made the code a bit more obscure (especially because you
except
the parent class not this one)So in the interest of readability maybe we could get rid of this one and instead except
RLClientError
later?