Skip to content

Commit c764db8

Browse files
authored
Provide response text passthrough for GPX-formatted requests (#67)
1 parent ed5e5cd commit c764db8

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

openrouteservice/client.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from datetime import datetime
2222
from datetime import timedelta
23+
import cgi
2324
import functools
2425
import requests
2526
import json
@@ -257,10 +258,15 @@ def req(self):
257258
@staticmethod
258259
def _get_body(response):
259260
"""Returns the body of a response object, raises status code exceptions if necessary."""
260-
try:
261-
body = response.json()
262-
except json.JSONDecodeError: # pragma: no cover
263-
raise exceptions.HTTPError(response.status_code)
261+
content_type = response.headers["Content-Type"]
262+
mime_type, _ = cgi.parse_header(content_type)
263+
if mime_type == "application/gpx+xml":
264+
body = response.text
265+
else:
266+
try:
267+
body = response.json()
268+
except json.JSONDecodeError: # pragma: no cover
269+
raise exceptions.HTTPError(response.status_code)
264270

265271
# error = body.get('error')
266272
status_code = response.status_code

openrouteservice/directions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def directions(
7171
:param format: Specifies the response format. One of ['json', 'geojson', 'gpx']. Default "json".
7272
Geometry format for "json" is Google's encodedpolyline. The GPX schema the response is validated
7373
against can be found here:
74-
https://raw.githubusercontent.com/GIScience/openrouteservice-schema/master/gpx/v1/ors-gpx.xsd.
74+
https://raw.githubusercontent.com/GIScience/openrouteservice-schema/master/gpx/v2/ors-gpx.xsd.
7575
:type format: str
7676
7777
:param format_out: DEPRECATED.

test/test_directions.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from copy import deepcopy
2626

2727
from openrouteservice import exceptions
28-
from test.test_helper import ENDPOINT_DICT
28+
from test.test_helper import ENDPOINT_DICT, GPX_RESPONSE
2929

3030

3131
class DirectionsTest(_test.TestCase):
@@ -48,6 +48,26 @@ def test_directions(self):
4848
self.assertEqual(resp, self.valid_query)
4949
self.assertIn("sample_key", responses.calls[0].request.headers.values())
5050

51+
@responses.activate
52+
def test_directions_gpx(self):
53+
query = deepcopy(self.valid_query)
54+
query["format"] = "gpx"
55+
56+
responses.add(
57+
responses.POST,
58+
"https://api.openrouteservice.org/v2/directions/{}/gpx".format(
59+
self.valid_query["profile"]
60+
),
61+
body=GPX_RESPONSE,
62+
status=200,
63+
content_type="application/gpx+xml;charset=UTF-8",
64+
)
65+
66+
resp = self.client.directions(**query)
67+
68+
self.assertEqual(resp, GPX_RESPONSE)
69+
self.assertIn("sample_key", responses.calls[0].request.headers.values())
70+
5171
@responses.activate
5272
def test_directions_incompatible_parameters(self):
5373
self.valid_query["optimized"] = True

test/test_helper.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,29 @@
225225
"matrix": PARAM_LIST_TWO,
226226
},
227227
}
228+
229+
GPX_RESPONSE = """
230+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
231+
<gpx version="1.0" creator="openrouteservice" xmlns="https://raw.githubusercontent.com/GIScience/openrouteservice-schema/master/gpx/v2/ors-gpx.xsd">
232+
<metadata>
233+
<name>openrouteservice directions</name>
234+
<desc>This is a directions instructions file as GPX, generated from openrouteservice</desc>
235+
<author><name>openrouteservice</name>
236+
<email id="support" domain="openrouteservice.heigit.org"/>
237+
<link href="https://openrouteservice.org/">
238+
<text>https://openrouteservice.org/</text>
239+
<type>text/html</type>
240+
</link>
241+
</author>
242+
<copyright author="openrouteservice.org | OpenStreetMap contributors">
243+
<year>2021</year>
244+
<license>LGPL 3.0</license>
245+
</copyright>
246+
<time>2021-07-25T17:26:59.023Z</time>
247+
<bounds maxLat="51.501602" maxLon="0.147508" minLat="51.185303" minLon="-0.076743"/>
248+
<extensions>
249+
<system-message></system-message>
250+
</extensions>
251+
</metadata>
252+
</gpx>
253+
"""

0 commit comments

Comments
 (0)