|
19 | 19 | from requests.models import Response |
20 | 20 | from typing_extensions import TypedDict |
21 | 21 |
|
22 | | -from .problem import api_error |
| 22 | + |
| 23 | +class ProblemDecoder(json.JSONDecoder): |
| 24 | + def decode(self, s): |
| 25 | + data = super().decode(s) |
| 26 | + if isinstance(data, dict): |
| 27 | + return self._make_problem(data) |
| 28 | + return data |
| 29 | + |
| 30 | + def _make_problem(self, data): |
| 31 | + type_ = data.pop("type", None) |
| 32 | + status = data.pop("status", None) |
| 33 | + title = data.pop("title", None) |
| 34 | + detail = data.pop("detail", None) |
| 35 | + instance = data.pop("instance", None) |
| 36 | + return Problem(type_, status, title, detail, instance, **data) |
| 37 | + |
| 38 | + |
| 39 | +class Problem(Exception): |
| 40 | + def __init__(self, type_, status, title, detail, instance, **kwargs): |
| 41 | + self._type = type_ or "about:blank" |
| 42 | + self.status = status |
| 43 | + self.title = title |
| 44 | + self.detail = detail |
| 45 | + self.instance = instance |
| 46 | + self.extras = kwargs |
| 47 | + |
| 48 | + for k in self.extras: |
| 49 | + if k in ["type", "status", "title", "detail", "instance"]: |
| 50 | + raise ValueError(f"Reserved key '{k}' used in Problem extra arguments.") |
| 51 | + |
| 52 | + def __str__(self): |
| 53 | + return self.title |
| 54 | + |
| 55 | + def __repr__(self): |
| 56 | + return f"{self.__class__.__name__}<type={self._type}; status={self.status}; title={self.title}>" |
| 57 | + |
| 58 | + def msg(self): |
| 59 | + msg = ( |
| 60 | + f"type = {self._type}, " |
| 61 | + f"status = {self.status}, " |
| 62 | + f"title = {self.title}, " |
| 63 | + f"detail = {self.detail}, " |
| 64 | + f"instance = {self.instance}, " |
| 65 | + f"extras = {self.extras}" |
| 66 | + ) |
| 67 | + return msg |
| 68 | + |
| 69 | + @property |
| 70 | + def type(self): |
| 71 | + return self._type |
| 72 | + |
| 73 | + |
| 74 | +class LegacyProblemDecoder(json.JSONDecoder): |
| 75 | + def decode(self, s): |
| 76 | + data = super().decode(s) |
| 77 | + if isinstance(data, dict): |
| 78 | + return self._make_legacy_problem(data) |
| 79 | + return data |
| 80 | + |
| 81 | + def _make_legacy_problem(self, data): |
| 82 | + request_id = None |
| 83 | + error_code = None |
| 84 | + code_type = None |
| 85 | + |
| 86 | + if "__type" in data: |
| 87 | + error_code = data.get("__type") |
| 88 | + else: |
| 89 | + request_id = (data.get("ResponseContext") or {}).get("RequestId") |
| 90 | + errors = data.get("Errors") |
| 91 | + if errors: |
| 92 | + error = errors[0] |
| 93 | + error_code = error.get("Code") |
| 94 | + reason = error.get("Type") |
| 95 | + if error.get("Details"): |
| 96 | + code_type = reason |
| 97 | + else: |
| 98 | + code_type = None |
| 99 | + return LegacyProblem(None, error_code, code_type, request_id, None) |
| 100 | + |
| 101 | + |
| 102 | +class LegacyProblem(Exception): |
| 103 | + def __init__(self, status, error_code, code_type, request_id, url): |
| 104 | + self.status = status |
| 105 | + self.error_code = error_code |
| 106 | + self.code_type = code_type |
| 107 | + self.request_id = request_id |
| 108 | + self.url = url |
| 109 | + |
| 110 | + def msg(self): |
| 111 | + msg = ( |
| 112 | + f"status = {self.status}, " |
| 113 | + f"code = {self.error_code}, " |
| 114 | + f"{'code_type = ' if self.code_type is not None else ''}" |
| 115 | + f"{self.code_type + ', ' if self.code_type is not None else ''}" |
| 116 | + f"request_id = {self.request_id}, " |
| 117 | + f"url = {self.url}" |
| 118 | + ) |
| 119 | + return msg |
| 120 | + |
| 121 | + |
| 122 | +def api_error(response): |
| 123 | + try: |
| 124 | + problem = None |
| 125 | + ct = response.headers.get("content-type") or "" |
| 126 | + if "application/json" in ct: |
| 127 | + problem: LegacyProblem = response.json(cls=LegacyProblemDecoder) |
| 128 | + problem.status = problem.status or str(response.status_code) |
| 129 | + problem.url = response.url |
| 130 | + elif "application/problem+json" in ct: |
| 131 | + problem: Problem = response.json(cls=ProblemDecoder) |
| 132 | + problem.status = problem.status or str(response.status_code) |
| 133 | + |
| 134 | + if problem: |
| 135 | + return problem |
| 136 | + except json.JSONDecodeError: |
| 137 | + # If it is not JSON, pass |
| 138 | + pass |
| 139 | + |
| 140 | + try: |
| 141 | + error = ET.fromstring(response.text) |
| 142 | + |
| 143 | + err = dict() |
| 144 | + for key, attr in [ |
| 145 | + ("Code", "error_code"), |
| 146 | + ("Message", "status"), |
| 147 | + ("RequestId", "request_id"), |
| 148 | + ("RequestID", "request_id"), |
| 149 | + ]: |
| 150 | + value = next((x.text for x in error.iter() if x.tag.endswith(key)), None) |
| 151 | + if value: |
| 152 | + err[attr] = value |
| 153 | + |
| 154 | + return LegacyProblem(**err) |
| 155 | + except: |
| 156 | + raise Exception( |
| 157 | + f"Could not decode error response from {response.url} with status code {response.status_code}" |
| 158 | + ) |
| 159 | + |
23 | 160 |
|
24 | 161 | CANONICAL_URI = "/" |
25 | 162 | CONFIGURATION_FILE = "config.json" |
|
0 commit comments