From 1a21dda52a1f47f129ab3ec2241980ba2d8cf897 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Tue, 3 Nov 2015 19:21:28 +0100 Subject: [PATCH] Controls empty response from geocoding service --- lib/python/heremaps/heremapsexceptions.py | 4 ++++ lib/python/heremaps/heremapsgeocoder.py | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) 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']