|
10 | 10 | ColorTextureRecorderJson, |
11 | 11 | EnvironmentJson, |
12 | 12 | EquirectCameraJson, |
13 | | - GenericError, |
| 13 | + ProblemJson, |
14 | 14 | GetCameraJson, |
15 | 15 | GroundBodyJson, |
16 | 16 | ObjectJson, |
@@ -43,23 +43,26 @@ def from_context(context: Context) -> "APIClient": |
43 | 43 | def get_api_version(self) -> Result[ApiVersionJson, str]: |
44 | 44 | return self._get("api/version", assert_compat=False).bind(self._parse_json_response) |
45 | 45 |
|
46 | | - def is_api_supported(self) -> bool: |
47 | | - if self.api_version is None: |
48 | | - api_version = self.get_api_version().unwrap_or(None) |
49 | | - if api_version: |
50 | | - self.api_version = (api_version["major"], api_version["minor"]) |
| 46 | + def assert_compatability(self): |
| 47 | + get_version_error: str | None = None |
51 | 48 |
|
52 | 49 | if self.api_version is None: |
53 | | - return False |
| 50 | + get_version_result = self.get_api_version() |
| 51 | + if get_version_result.is_ok: |
| 52 | + api_version = get_version_result.unwrap() |
| 53 | + self.api_version = (api_version["major"], api_version["minor"]) |
| 54 | + else: |
| 55 | + get_version_error = get_version_result.unwrap_error() |
54 | 56 |
|
55 | | - return self.api_version[0] == ACCEPTED_API_VERSION[0] and self.api_version[1] >= ACCEPTED_API_VERSION[1] |
| 57 | + is_api_supported = self.api_version is not None and ( |
| 58 | + self.api_version[0] == ACCEPTED_API_VERSION[0] and self.api_version[1] >= ACCEPTED_API_VERSION[1] |
| 59 | + ) |
56 | 60 |
|
57 | | - def assert_compatability(self): |
58 | | - if not self.is_api_supported(): |
| 61 | + if not is_api_supported: |
59 | 62 | raise AssertionError( |
60 | | - f'versions of Outer Scout mod and addon are incompatible. Please, update the addon to support version {ACCEPTED_API_VERSION[0]}.{ACCEPTED_API_VERSION[1]}.x' |
| 63 | + f"versions of Outer Scout mod and addon are incompatible. Please, update the addon to support version {ACCEPTED_API_VERSION[0]}.{ACCEPTED_API_VERSION[1]}.x" |
61 | 64 | if self.api_version is not None |
62 | | - else "failed to connect to the Outer Scout API" |
| 65 | + else get_version_error |
63 | 66 | ) |
64 | 67 |
|
65 | 68 | def get_environment(self) -> Result[EnvironmentJson, str]: |
@@ -151,12 +154,20 @@ def _get_response( |
151 | 154 | request = Request(url=self.base_url + route, method=method, data=data, query_params=query) |
152 | 155 |
|
153 | 156 | response = request.send() |
| 157 | + if response is None: |
| 158 | + Result.do_error( |
| 159 | + f"couldn't get a response from the Outer Scout API. Make sure that it's available at {self.base_url}" |
| 160 | + ) |
154 | 161 |
|
155 | | - error_prefix = "Outer Scout API error: " |
156 | | - |
157 | | - if not response.is_success: |
158 | | - generic_error = response.json(GenericError).map_error(lambda _: error_prefix + response.body).then() |
159 | | - Result.do_error(error_prefix + generic_error["error"] if "error" in generic_error else response.body) |
| 162 | + if response.is_error: |
| 163 | + Result.do_error( |
| 164 | + response.json(ProblemJson) |
| 165 | + .map( |
| 166 | + lambda p: (f"[{p['type']}]" if "title" not in p else p["title"]) |
| 167 | + + (f": {p['detail']}" if "detail" in p else "") |
| 168 | + ) |
| 169 | + .unwrap_or_else(lambda _: f"Outer Scout API error: {response.body}") |
| 170 | + ) |
160 | 171 |
|
161 | 172 | return response |
162 | 173 |
|
|
0 commit comments