File tree Expand file tree Collapse file tree 4 files changed +58
-6
lines changed Expand file tree Collapse file tree 4 files changed +58
-6
lines changed Original file line number Diff line number Diff line change 20
20
21
21
from datetime import datetime
22
22
from datetime import timedelta
23
+ import cgi
23
24
import functools
24
25
import requests
25
26
import json
@@ -257,10 +258,15 @@ def req(self):
257
258
@staticmethod
258
259
def _get_body (response ):
259
260
"""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 )
264
270
265
271
# error = body.get('error')
266
272
status_code = response .status_code
Original file line number Diff line number Diff line change @@ -71,7 +71,7 @@ def directions(
71
71
:param format: Specifies the response format. One of ['json', 'geojson', 'gpx']. Default "json".
72
72
Geometry format for "json" is Google's encodedpolyline. The GPX schema the response is validated
73
73
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.
75
75
:type format: str
76
76
77
77
:param format_out: DEPRECATED.
Original file line number Diff line number Diff line change 25
25
from copy import deepcopy
26
26
27
27
from openrouteservice import exceptions
28
- from test .test_helper import ENDPOINT_DICT
28
+ from test .test_helper import ENDPOINT_DICT , GPX_RESPONSE
29
29
30
30
31
31
class DirectionsTest (_test .TestCase ):
@@ -48,6 +48,26 @@ def test_directions(self):
48
48
self .assertEqual (resp , self .valid_query )
49
49
self .assertIn ("sample_key" , responses .calls [0 ].request .headers .values ())
50
50
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
+
51
71
@responses .activate
52
72
def test_directions_incompatible_parameters (self ):
53
73
self .valid_query ["optimized" ] = True
Original file line number Diff line number Diff line change 225
225
"matrix" : PARAM_LIST_TWO ,
226
226
},
227
227
}
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
+ """
You can’t perform that action at this time.
0 commit comments