diff --git a/lib/python/heremaps/heremapsexceptions.py b/lib/python/heremaps/heremapsexceptions.py index cbbab2e..5e89a1b 100644 --- a/lib/python/heremaps/heremapsexceptions.py +++ b/lib/python/heremaps/heremapsexceptions.py @@ -9,3 +9,7 @@ class BadGeocodingParams(Exception): class NoGeocodingParams(Exception): def __str__(self): return repr('No params for geocoding specified') + +class EmptyGeocoderResponse(Exception): + def __str__(self): + return repr('The request could not be geocoded') diff --git a/lib/python/heremaps/heremapsgeocoder.py b/lib/python/heremaps/heremapsgeocoder.py index aa3876f..1b72fed 100644 --- a/lib/python/heremaps/heremapsgeocoder.py +++ b/lib/python/heremaps/heremapsgeocoder.py @@ -2,7 +2,7 @@ import inspect import json import urllib -from heremapsexceptions import BadGeocodingParams, NoGeocodingParams +from heremapsexceptions import BadGeocodingParams, EmptyGeocoderResponse, NoGeocodingParams class Geocoder: 'A Here Maps Geocoder wrapper for python' @@ -94,8 +94,12 @@ class Geocoder: return self.geocode(params) - def extractLngLatFromResponse(response): - location = response['Response']['View'][0]['Result'][0]['Location'] + def extractLngLatFromResponse(self, response): + view = response['Response']['View'] + + if len(view) is 0: raise EmptyGeocoderResponse() + + location = view[0]['Result'][0]['Location'] longitude = location['DisplayPosition']['Longitude'] latitude = location['DisplayPosition']['Latitude']