Skip to content

Commit

Permalink
Update geocoder_api.py to handle optional city, country and postal code
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazurl committed Jun 6, 2024
1 parent cffef39 commit 5b2929e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 11 deletions.
28 changes: 19 additions & 9 deletions herepy/geocoder_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -122,23 +123,32 @@ def address_with_details(
city name.
country (str):
country name.
postal_code (str):
postal code.
lang (str):
BCP47 compliant Language Code.
Returns:
GeocoderResponse
Raises:
HEREError"""

qq_query = ""
qq_params = []
if house_number is not None:
qq_query += f"houseNumber={house_number};"
qq_params.append(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"street={street}")
if city is not None:
qq_params.append(f"city={city}")
if postal_code is not None:
qq_params.append(f"postalCode={postal_code}")
if country is not None:
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,
}
Expand Down
52 changes: 50 additions & 2 deletions tests/test_geocoder_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

@responses.activate
def test_addresswithdetails_whenerroroccurred(self):
with open("testdata/models/geocoder_error.json", "r") as f:
Expand Down

0 comments on commit 5b2929e

Please sign in to comment.