From 0d75cf88f04f1d5f971238d351403fa997d932fc Mon Sep 17 00:00:00 2001 From: Hazurl Date: Thu, 6 Jun 2024 17:09:47 +0200 Subject: [PATCH] Update geocoder_api.py to handle optional city, country and postal code --- herepy/geocoder_api.py | 30 ++++++++++++++-------- tests/test_geocoder_api.py | 52 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 12 deletions(-) diff --git a/herepy/geocoder_api.py b/herepy/geocoder_api.py index 143ed7c..e84d400 100644 --- a/herepy/geocoder_api.py +++ b/herepy/geocoder_api.py @@ -106,11 +106,12 @@ def address_with_boundingbox( def address_with_details( self, - city: str, - country: str, - lang: str = "en-US", house_number: Optional[int] = None, street: Optional[str] = None, + city: Optional[str] = None, + postal_code: Optional[str] = None, + country: Optional[str] = None, + lang: str = "en-US", ) -> Optional[GeocoderResponse]: """Geocodes with given address details Args: @@ -122,6 +123,8 @@ def address_with_details( city name. country (str): country name. + postal_code (str): + postal code. lang (str): BCP47 compliant Language Code. Returns: @@ -129,16 +132,23 @@ def address_with_details( Raises: HEREError""" - qq_query = "" + qq_params = [] if house_number is not None: - qq_query += f"houseNumber={house_number};" - if street is not None: - qq_query += f"street={street};" - qq_query += f"city={city};" - qq_query += f"country={country}" + qq_params.append(f"houseNumber={house_number}") + if street: + qq_params.append(f"street={street}") + if city: + qq_params.append(f"city={city}") + if postal_code: + qq_params.append(f"postalCode={postal_code}") + if country: + qq_params.append(f"country={country}") + + if len(qq_params) == 0: + raise HEREError("At least one of the parameters should be provided") data = { - "qq": qq_query, + "qq": ";".join(qq_params), "apiKey": self._api_key, "lang": lang, } diff --git a/tests/test_geocoder_api.py b/tests/test_geocoder_api.py index 8e10a0f..caedaf3 100644 --- a/tests/test_geocoder_api.py +++ b/tests/test_geocoder_api.py @@ -94,7 +94,8 @@ def test_addresswithdetails_whensucceed(self): street="Barbaros", city="Istanbul", country="Turkey", - house_number=34 + house_number=34, + postal_code="34353", ) self.assertTrue(response) self.assertIsInstance(response, herepy.GeocoderResponse) @@ -136,7 +137,54 @@ def test_address_with_detail_without_house_number(self): ) self.assertTrue(response) self.assertIsInstance(response, herepy.GeocoderResponse) - + + @responses.activate + def test_address_with_detail_without_country(self): + with open("testdata/models/geocoder.json", "r") as f: + expected_response = f.read() + responses.add( + responses.GET, + "https://geocode.search.hereapi.com/v1/geocode", + expected_response, + status=200, + ) + response = self._api.address_with_details( + street="Barbaros", + city="Istanbul", + country= None + ) + self.assertTrue(response) + self.assertIsInstance(response, herepy.GeocoderResponse) + + @responses.activate + def test_address_with_detail_with_only_country(self): + with open("testdata/models/geocoder.json") as f: + expected_response = f.read() + responses.add( + responses.GET, + "https://geocode.search.hereapi.com/v1/geocode", + expected_response, + status=200, + ) + response = self._api.address_with_details( + country="FR", + ) + self.assertTrue(response) + self.assertIsInstance(response, herepy.GeocoderResponse) + + @responses.activate + def test_address_with_detail_without_any_detail_raise(self): + with open("testdata/models/geocoder.json") as f: + expected_response = f.read() + responses.add( + responses.GET, + "https://geocode.search.hereapi.com/v1/geocode", + expected_response, + status=200, + ) + with self.assertRaises(herepy.HEREError): + self._api.address_with_details(country="") + @responses.activate def test_addresswithdetails_whenerroroccurred(self): with open("testdata/models/geocoder_error.json", "r") as f: