| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import http.client |
| 4 |
import json |
| 5 |
import typing as t |
| 6 |
|
| 7 |
import requests |
| 8 |
import responses |
| 9 |
|
| 10 |
from globus_sdk.exc import GlobusAPIError |
| 11 |
|
| 12 |
E = t.TypeVar("E", bound=GlobusAPIError) |
| 13 |
|
| 14 |
|
| 15 |
def get_last_request( |
| 16 |
*, requests_mock: responses.RequestsMock | None = None |
| 17 |
) -> requests.PreparedRequest | None: |
| 18 |
""" |
| 19 |
Get the last request which was received, or None if there were no requests. |
| 20 |
|
| 21 |
:param requests_mock: A non-default ``RequestsMock`` object to use. |
| 22 |
""" |
| 23 |
calls = requests_mock.calls if requests_mock is not None else responses.calls |
| 24 |
try: |
| 25 |
last_call = calls[-1] |
| 26 |
except IndexError: |
| 27 |
return None |
| 28 |
return last_call.request |
| 29 |
|
| 30 |
|
| 31 |
@t.overload |
| 32 |
def construct_error( |
| 33 |
*, |
| 34 |
http_status: int, |
| 35 |
body: bytes | str | t.Dict[str, t.Any], |
| 36 |
method: str = "GET", |
| 37 |
response_headers: t.Dict[str, str] | None = None, |
| 38 |
request_headers: t.Dict[str, str] | None = None, |
| 39 |
response_encoding: str = "utf-8", |
| 40 |
url: str = "https://bogus-url/", |
| 41 |
) -> GlobusAPIError: ... |
| 42 |
|
| 43 |
|
| 44 |
@t.overload |
| 45 |
def construct_error( |
| 46 |
*, |
| 47 |
http_status: int, |
| 48 |
error_class: type[E], |
| 49 |
body: bytes | str | t.Dict[str, t.Any], |
| 50 |
method: str = "GET", |
| 51 |
response_headers: t.Dict[str, str] | None = None, |
| 52 |
request_headers: t.Dict[str, str] | None = None, |
| 53 |
response_encoding: str = "utf-8", |
| 54 |
url: str = "https://bogus-url/", |
| 55 |
) -> E: ... |
| 56 |
|
| 57 |
|
| 58 |
def construct_error( |
| 59 |
*, |
| 60 |
http_status: int, |
| 61 |
body: bytes | str | t.Dict[str, t.Any], |
| 62 |
error_class: type[E] | type[GlobusAPIError] = GlobusAPIError, |
| 63 |
method: str = "GET", |
| 64 |
response_headers: t.Dict[str, str] | None = None, |
| 65 |
request_headers: t.Dict[str, str] | None = None, |
| 66 |
response_encoding: str = "utf-8", |
| 67 |
url: str = "https://bogus-url/", |
| 68 |
) -> E | GlobusAPIError: |
| 69 |
""" |
| 70 |
Given parameters for an HTTP response, construct a GlobusAPIError and return it. |
| 71 |
|
| 72 |
:param error_class: The class of the error to construct. Defaults to |
| 73 |
GlobusAPIError. |
| 74 |
:param http_status: The HTTP status code to use in the response. |
| 75 |
:param body: The body of the response. If a dict, will be JSON-encoded. |
| 76 |
:param method: The HTTP method to set on the underlying request. |
| 77 |
:param response_headers: The headers of the response. |
| 78 |
:param request_headers: The headers of the request. |
| 79 |
:param response_encoding: The encoding to use for the response body. |
| 80 |
:param url: The URL to set on the underlying request. |
| 81 |
""" |
| 82 |
raw_response = requests.Response() |
| 83 |
raw_response.status_code = http_status |
| 84 |
raw_response.reason = http.client.responses.get(http_status, "Unknown") |
| 85 |
raw_response.url = url |
| 86 |
raw_response.encoding = response_encoding |
| 87 |
raw_response.request = requests.Request( |
| 88 |
method=method, url=url, headers=request_headers or {} |
| 89 |
).prepare() |
| 90 |
raw_response.headers.update(response_headers or {}) |
| 91 |
if isinstance(body, dict) and "Content-Type" not in raw_response.headers: |
| 92 |
raw_response.headers["Content-Type"] = "application/json" |
| 93 |
|
| 94 |
raw_response._content = _encode_body(body, response_encoding) |
| 95 |
|
| 96 |
return error_class(raw_response) |
| 97 |
|
| 98 |
|
| 99 |
def _encode_body(body: bytes | str | t.Dict[str, t.Any], encoding: str) -> bytes: |
| 100 |
if isinstance(body, bytes): |
| 101 |
return body |
| 102 |
elif isinstance(body, str): |
| 103 |
return body.encode(encoding) |
| 104 |
else: |
| 105 |
return json.dumps(body).encode(encoding) |