Skip to content

Commit 49023a4

Browse files
SlowMo24redfrexx
andauthored
feat: add curl command to logging
closes: #42 Co-authored-by: Christina Ludwig <christina.ludwig@uni-heidelberg.de>
1 parent c70358c commit 49023a4

File tree

6 files changed

+115
-2
lines changed

6 files changed

+115
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- support for python 3.12
66
- custom [retry](https://urllib3.readthedocs.io/en/latest/reference/urllib3.util.html#urllib3.util.Retry) configuration
77
- start and end timestamp meta information of the client are now datetime objects
8+
- if a request fails a bash script containing the respective `curl` command is logged (if possible). This allows for easier debugging and sharing of failed requests.
89

910
### Removed
1011

ohsome/exceptions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import json
88
from pathlib import Path
99

10+
from curlify2 import Curlify
11+
1012

1113
class OhsomeException(Exception):
1214
"""Exception to handle ohsome API errors"""
@@ -33,8 +35,17 @@ def log(self, log_dir: Path):
3335
self.log_bpolys(log_dir, log_file_name)
3436
self.log_parameter(log_dir, log_file_name)
3537
if self.response is not None:
38+
self.log_curl(log_dir, log_file_name)
3639
self.log_response(log_dir, log_file_name)
3740

41+
def log_curl(self, log_dir: Path, log_file_name: str) -> None:
42+
"""Log the respective curl command for the request for easy debugging and sharing."""
43+
log_file = log_dir / f"{log_file_name}_curl.sh"
44+
curl = Curlify(self.response.request)
45+
curl_command = curl.to_curl()
46+
with log_file.open(mode="w") as dst:
47+
dst.write(curl_command)
48+
3849
def log_response(self, log_dir: Path, log_file_name: str):
3950
"""
4051
Log raw response. This may duplicate much data but is helpful for debugging to know the exact raw answer by the
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
interactions:
2+
- request:
3+
body: bboxes=8.67555%2C49.39885%2C8.69637%2C49.41122&timeout=0.001
4+
headers:
5+
Accept:
6+
- '*/*'
7+
Accept-Encoding:
8+
- gzip, deflate
9+
Connection:
10+
- keep-alive
11+
Content-Length:
12+
- '60'
13+
Content-Type:
14+
- application/x-www-form-urlencoded
15+
user-agent:
16+
- ohsome-py/0.2.0
17+
method: POST
18+
uri: https://api.ohsome.org/v1/elements/count
19+
response:
20+
body:
21+
string: "{\n \"timestamp\" : \"2023-11-17T12:28:28.06766606\",\n \"status\"
22+
: 413,\n \"message\" : \"The given query is too large in respect to the given
23+
timeout. Please use a smaller region and/or coarser time period.\",\n \"requestUrl\"
24+
: \"https://api.ohsome.org/v1/elements/count\"\n}"
25+
headers:
26+
Access-Control-Allow-Credentials:
27+
- 'true'
28+
Access-Control-Allow-Headers:
29+
- Origin,Accept,X-Requested-With,Content-Type,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization
30+
Access-Control-Allow-Methods:
31+
- POST, GET
32+
Access-Control-Allow-Origin:
33+
- '*'
34+
Access-Control-Max-Age:
35+
- '3600'
36+
Cache-Control:
37+
- no-cache, no-store, must-revalidate
38+
Connection:
39+
- close
40+
Content-Encoding:
41+
- gzip
42+
Content-Type:
43+
- application/json
44+
Date:
45+
- Fri, 17 Nov 2023 12:28:27 GMT
46+
Server:
47+
- Apache
48+
Strict-Transport-Security:
49+
- max-age=63072000; includeSubdomains;
50+
Transfer-Encoding:
51+
- chunked
52+
vary:
53+
- accept-encoding
54+
status:
55+
code: 413
56+
message: ''
57+
version: 1

ohsome/test/test_exceptions.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,46 @@ def test_log_bpolys(base_client_without_log, tmpdir):
118118
base_client_without_log.elements.count.post(
119119
bpolys=bpolys, time=time, filter=fltr, timeout=timeout
120120
)
121-
log_file_patterns = ["ohsome_*_bpolys.geojson", "ohsome_*.json", "ohsome_*raw.txt"]
121+
log_file_patterns = [
122+
"ohsome_*_bpolys.geojson",
123+
"ohsome_*_curl.sh",
124+
"ohsome_*.json",
125+
"ohsome_*raw.txt",
126+
]
122127
for p in log_file_patterns:
123128
log_file = list(Path(base_client_without_log.log_dir).glob(p))
124129
assert len(log_file) == 1, f"Log file {p} not found"
125130
logger.info(f"Found log file: {log_file[0].name}")
126131
log_file[0].unlink()
127132

128133

134+
@pytest.mark.vcr
135+
def test_log_curl(base_client_without_log, tmpdir):
136+
"""
137+
Test whether log file containing curl command is created when request fails
138+
:return:
139+
"""
140+
141+
base_client_without_log.log = True
142+
base_client_without_log.log_dir = tmpdir.mkdir("logs").strpath
143+
144+
bboxes = [8.67555, 49.39885, 8.69637, 49.41122]
145+
timeout = 0.001
146+
147+
with pytest.raises(ohsome.OhsomeException):
148+
base_client_without_log.elements.count.post(bboxes=bboxes, timeout=timeout)
149+
150+
log_file = list(Path(base_client_without_log.log_dir).glob("ohsome_*_curl.sh"))
151+
with open(log_file[0]) as file:
152+
assert file.read() == (
153+
'curl -X POST -H "user-agent: ohsome-py/0.2.0" -H "Accept-Encoding: gzip, '
154+
'deflate" -H "Accept: */*" -H "Connection: keep-alive" -H "Content-Length: 60" '
155+
'-H "Content-Type: application/x-www-form-urlencoded" '
156+
"-d 'bboxes=8.67555%2C49.39885%2C8.69637%2C49.41122&timeout=0.001' "
157+
"https://api.ohsome.org/v1/elements/count"
158+
)
159+
160+
129161
def test_metadata_invalid_baseurl(custom_client_with_wrong_url):
130162
"""
131163
Throw exception if the ohsome API is not available

poetry.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pandas = "^2.1.3"
1414
numpy = "^1.20.0"
1515
geopandas = "^0.14.1"
1616
urllib3 = "^2.1.0"
17+
curlify2 = "^2.0.0"
1718

1819
[tool.poetry.group.test.dependencies]
1920
pytest = "^7.4.3"

0 commit comments

Comments
 (0)