|
7 | 7 | from oauthlib.oauth2 import LegacyApplicationClient
|
8 | 8 | from oauthlib.oauth2 import TokenExpiredError, is_secure_transport
|
9 | 9 | import requests
|
| 10 | +from requests.exceptions import HTTPError |
10 | 11 |
|
11 | 12 | log = logging.getLogger(__name__)
|
12 | 13 |
|
@@ -363,6 +364,8 @@ def fetch_token(
|
363 | 364 | log.debug("Invoking hook %s.", hook)
|
364 | 365 | r = hook(r)
|
365 | 366 |
|
| 367 | + self._raise_for_5xx(response=r) |
| 368 | + |
366 | 369 | self._client.parse_request_body_response(r.text, scope=self.scope)
|
367 | 370 | self.token = self._client.token
|
368 | 371 | log.debug("Obtained token %s.", self.token)
|
@@ -449,6 +452,8 @@ def refresh_token(
|
449 | 452 | log.debug("Invoking hook %s.", hook)
|
450 | 453 | r = hook(r)
|
451 | 454 |
|
| 455 | + self._raise_for_5xx(response=r) |
| 456 | + |
452 | 457 | self.token = self._client.parse_request_body_response(r.text, scope=self.scope)
|
453 | 458 | if not "refresh_token" in self.token:
|
454 | 459 | log.debug("No new refresh token given. Re-using old.")
|
@@ -538,3 +543,37 @@ def register_compliance_hook(self, hook_type, hook):
|
538 | 543 | "Hook type %s is not in %s.", hook_type, self.compliance_hook
|
539 | 544 | )
|
540 | 545 | self.compliance_hook[hook_type].add(hook)
|
| 546 | + |
| 547 | + def _raise_for_5xx(self, response: requests.models.Response) -> None: |
| 548 | + """ |
| 549 | + Raise requests.HTTPError if response is an HTTP 5XX error. |
| 550 | +
|
| 551 | + Just like the existing Response.raise_for_status() but ignores 4XX |
| 552 | + errors. |
| 553 | +
|
| 554 | + :param response: HTTP response object from requests |
| 555 | + Raises :class:`requests.exceptions.HTTPError`, if a 5XX error occurred. |
| 556 | + """ |
| 557 | + http_error_msg = '' |
| 558 | + if isinstance(response.reason, bytes): |
| 559 | + # We attempt to decode utf-8 first because some servers |
| 560 | + # choose to localize their reason strings. If the string |
| 561 | + # isn't utf-8, we fall back to iso-8859-1 for all other |
| 562 | + # encodings. (See psf/requests PR #3538) |
| 563 | + try: |
| 564 | + reason = response.reason.decode('utf-8') |
| 565 | + except UnicodeDecodeError: |
| 566 | + reason = response.reason.decode('iso-8859-1') |
| 567 | + else: |
| 568 | + reason = response.reason |
| 569 | + |
| 570 | + if 400 <= response.status_code < 500: |
| 571 | + pass # ignored |
| 572 | + |
| 573 | + elif 500 <= response.status_code < 600: |
| 574 | + http_error_msg = u'%s Server Error: %s for url: %s' % ( |
| 575 | + response.status_code, reason, response.url |
| 576 | + ) |
| 577 | + |
| 578 | + if http_error_msg: |
| 579 | + raise HTTPError(http_error_msg, response=response) |
0 commit comments