Repository: mapbox/mapbox-sdk-py
Pull request #48 (2015-10-26 19:33:34)

Geocoder response geojson method
In the case of the geocoder, `json() == geojson()` but we'll do it for consistency


Patch:
diff --git a/mapbox/services/geocoding.py b/mapbox/services/geocoding.py
index 8180e55..f00ca91 100644
--- a/mapbox/services/geocoding.py
+++ b/mapbox/services/geocoding.py
@@ -27,7 +27,7 @@ class Geocoder(Service):
         """Returns a Requests response object that contains a GeoJSON
         collection of places matching the given address.
 
-        `response.json()` returns the geocoding result as GeoJSON.
+        `response.geojson()` returns the geocoding result as GeoJSON.
         `response.status_code` returns the HTTP API status code.
 
         Place results may be constrained to those of one or more types
@@ -43,13 +43,19 @@ class Geocoder(Service):
             params.update(proximity='{0},{1}'.format(round(float(lon), self.precision.get('proximity', 3)), round(float(lat), self.precision.get('proximity', 3))))
         resp = self.session.get(uri, params=params)
         self.handle_http_error(resp)
+
+        # for consistency with other services
+        def geojson():
+            return resp.json()
+        resp.geojson = geojson
+
         return resp
 
     def reverse(self, lon=None, lat=None, types=None):
         """Returns a Requests response object that contains a GeoJSON
         collection of places near the given longitude and latitude.
 
-        `response.json()` returns the geocoding result as GeoJSON.
+        `response.geojson()` returns the geocoding result as GeoJSON.
         `response.status_code` returns the HTTP API status code.
 
         See: https://www.mapbox.com/developers/api/geocoding/#reverse."""
@@ -60,6 +66,12 @@ class Geocoder(Service):
             params.update(self._validate_place_types(types))
         resp = self.session.get(uri, params=params)
         self.handle_http_error(resp)
+
+        # for consistency with other services
+        def geojson():
+            return resp.json()
+        resp.geojson = geojson
+
         return resp
 
     @property


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index fb4faf9..ac0e970 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -122,7 +122,8 @@ def test_cli_geocode_unauthorized():
         content_type='application/json')
 
     runner = CliRunner()
-    result = runner.invoke(main_group, ['geocode', '--forward', '1600 pennsylvania ave nw'])
+    result = runner.invoke(main_group, ['geocode', '--forward',
+                                        '1600 pennsylvania ave nw'])
     assert result.exit_code == 1
     assert result.output == 'Error: {"message":"Not Authorized - Invalid Token"}\n'
 
diff --git a/tests/test_geocoder.py b/tests/test_geocoder.py
index 6424904..0f256ce 100644
--- a/tests/test_geocoder.py
+++ b/tests/test_geocoder.py
@@ -40,6 +40,22 @@ def test_geocoder_forward():
     assert response.json()['query'] == ["1600", "pennsylvania", "ave", "nw"]
 
 
+@responses.activate
+def test_geocoder_forward_geojson():
+    """Forward geocoding .geojson method works"""
+
+    responses.add(
+        responses.GET,
+        'https://api.mapbox.com/geocoding/v5/mapbox.places/1600%20pennsylvania%20ave%20nw.json?access_token=pk.test',
+        match_querystring=True,
+        body='{"query": ["1600", "pennsylvania", "ave", "nw"]}', status=200,
+        content_type='application/json')
+
+    response = mapbox.Geocoder(access_token='pk.test').forward('1600 pennsylvania ave nw')
+    assert response.status_code == 200
+    assert response.geojson() == response.json()
+
+
 @responses.activate
 def test_geocoder_reverse():
     """Reverse geocoding works"""
@@ -59,6 +75,24 @@ def test_geocoder_reverse():
     assert response.status_code == 200
     assert response.json()['query'] == [lon, lat]
 
+@responses.activate
+def test_geocoder_reverse_geojson():
+    """Reverse geocoding geojson works"""
+
+    lon, lat = -77.4371, 37.5227
+    body = json.dumps({"query": [lon, lat]})
+
+    responses.add(
+        responses.GET,
+        'https://api.mapbox.com/geocoding/v5/mapbox.places/{0},{1}.json?access_token=pk.test'.format(lon, lat),
+        match_querystring=True,
+        body=body,
+        status=200,
+        content_type='application/json')
+
+    response = mapbox.Geocoder(access_token='pk.test').reverse(lon=lon, lat=lat)
+    assert response.status_code == 200
+    assert response.geojson() == response.json()
 
 def test_geocoder_place_types():
     """Place types are enumerated"""