|
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | 4 | """OhsomeClient classes to build and handle requests to ohsome API""" |
| 5 | +import json |
5 | 6 | import urllib |
6 | 7 |
|
7 | 8 | import requests |
@@ -38,6 +39,7 @@ def __init__( |
38 | 39 | :param log: Log failed queries, default:True |
39 | 40 | :param log_dir: Directory for log files, default: ./ohsome_log |
40 | 41 | :param cache: Cache for endpoint components |
| 42 | + :param user_agent: User agent passed with the request to the ohsome API |
41 | 43 | """ |
42 | 44 | self.log = log |
43 | 45 | self.log_dir = log_dir |
@@ -95,6 +97,7 @@ def __init__( |
95 | 97 | :param log: Log failed queries, default:True |
96 | 98 | :param log_dir: Directory for log files, default: ./ohsome_log |
97 | 99 | :param cache: Cache for endpoint components |
| 100 | + :param user_agent: User agent passed with the request to the ohsome API |
98 | 101 | """ |
99 | 102 | super(_OhsomeInfoClient, self).__init__( |
100 | 103 | base_api_url, log, log_dir, cache, user_agent |
@@ -190,6 +193,7 @@ def __init__( |
190 | 193 | :param log: Log failed queries, default:True |
191 | 194 | :param log_dir: Directory for log files, default: ./ohsome_log |
192 | 195 | :param cache: Cache for endpoint components |
| 196 | + :param user_agent: User agent passed with the request to the ohsome API |
193 | 197 | """ |
194 | 198 | super(_OhsomePostClient, self).__init__( |
195 | 199 | base_api_url, log, log_dir, cache, user_agent |
@@ -286,13 +290,18 @@ def _handle_request(self): |
286 | 290 | response.raise_for_status() |
287 | 291 | response.json() |
288 | 292 | except requests.exceptions.HTTPError as e: |
289 | | - ohsome_exception = OhsomeException( |
290 | | - message=e.response.json()["message"], |
291 | | - url=self._url, |
292 | | - params=self._parameters, |
293 | | - error_code=e.response.status_code, |
294 | | - response=e.response, |
295 | | - ) |
| 293 | + try: |
| 294 | + error_message = e.response.json()["message"] |
| 295 | + except json.decoder.JSONDecodeError: |
| 296 | + error_message = f"Invalid URL: Is {self._url} valid?" |
| 297 | + finally: |
| 298 | + ohsome_exception = OhsomeException( |
| 299 | + message=error_message, |
| 300 | + url=self._url, |
| 301 | + params=self._parameters, |
| 302 | + error_code=e.response.status_code, |
| 303 | + response=e.response, |
| 304 | + ) |
296 | 305 | except requests.exceptions.ConnectionError as e: |
297 | 306 | ohsome_exception = OhsomeException( |
298 | 307 | message="Connection Error: Query could not be sent. Make sure there are no network " |
|
0 commit comments