Skip to content

Commit 5b2929e

Browse files
author
Hazurl
committed
Update geocoder_api.py to handle optional city, country and postal code
1 parent cffef39 commit 5b2929e

File tree

2 files changed

+69
-11
lines changed

2 files changed

+69
-11
lines changed

herepy/geocoder_api.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,12 @@ def address_with_boundingbox(
106106

107107
def address_with_details(
108108
self,
109-
city: str,
110-
country: str,
111-
lang: str = "en-US",
112109
house_number: Optional[int] = None,
113110
street: Optional[str] = None,
111+
city: Optional[str] = None,
112+
postal_code: Optional[str] = None,
113+
country: Optional[str] = None,
114+
lang: str = "en-US",
114115
) -> Optional[GeocoderResponse]:
115116
"""Geocodes with given address details
116117
Args:
@@ -122,23 +123,32 @@ def address_with_details(
122123
city name.
123124
country (str):
124125
country name.
126+
postal_code (str):
127+
postal code.
125128
lang (str):
126129
BCP47 compliant Language Code.
127130
Returns:
128131
GeocoderResponse
129132
Raises:
130133
HEREError"""
131134

132-
qq_query = ""
135+
qq_params = []
133136
if house_number is not None:
134-
qq_query += f"houseNumber={house_number};"
137+
qq_params.append(f"houseNumber={house_number}")
135138
if street is not None:
136-
qq_query += f"street={street};"
137-
qq_query += f"city={city};"
138-
qq_query += f"country={country}"
139+
qq_params.append(f"street={street}")
140+
if city is not None:
141+
qq_params.append(f"city={city}")
142+
if postal_code is not None:
143+
qq_params.append(f"postalCode={postal_code}")
144+
if country is not None:
145+
qq_params.append(f"country={country}")
146+
147+
if len(qq_params) == 0:
148+
raise HEREError("At least one of the parameters should be provided")
139149

140150
data = {
141-
"qq": qq_query,
151+
"qq": ";".join(qq_params),
142152
"apiKey": self._api_key,
143153
"lang": lang,
144154
}

tests/test_geocoder_api.py

+50-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ def test_addresswithdetails_whensucceed(self):
9494
street="Barbaros",
9595
city="Istanbul",
9696
country="Turkey",
97-
house_number=34
97+
house_number=34,
98+
postal_code="34353",
9899
)
99100
self.assertTrue(response)
100101
self.assertIsInstance(response, herepy.GeocoderResponse)
@@ -136,7 +137,54 @@ def test_address_with_detail_without_house_number(self):
136137
)
137138
self.assertTrue(response)
138139
self.assertIsInstance(response, herepy.GeocoderResponse)
139-
140+
141+
@responses.activate
142+
def test_address_with_detail_without_country(self):
143+
with open("testdata/models/geocoder.json", "r") as f:
144+
expected_response = f.read()
145+
responses.add(
146+
responses.GET,
147+
"https://geocode.search.hereapi.com/v1/geocode",
148+
expected_response,
149+
status=200,
150+
)
151+
response = self._api.address_with_details(
152+
street="Barbaros",
153+
city="Istanbul",
154+
country= None
155+
)
156+
self.assertTrue(response)
157+
self.assertIsInstance(response, herepy.GeocoderResponse)
158+
159+
@responses.activate
160+
def test_address_with_detail_with_only_country(self):
161+
with open("testdata/models/geocoder.json") as f:
162+
expected_response = f.read()
163+
responses.add(
164+
responses.GET,
165+
"https://geocode.search.hereapi.com/v1/geocode",
166+
expected_response,
167+
status=200,
168+
)
169+
response = self._api.address_with_details(
170+
country="FR",
171+
)
172+
self.assertTrue(response)
173+
self.assertIsInstance(response, herepy.GeocoderResponse)
174+
175+
@responses.activate
176+
def test_address_with_detail_without_any_detail_raise(self):
177+
with open("testdata/models/geocoder.json") as f:
178+
expected_response = f.read()
179+
responses.add(
180+
responses.GET,
181+
"https://geocode.search.hereapi.com/v1/geocode",
182+
expected_response,
183+
status=200,
184+
)
185+
with self.assertRaises(herepy.HEREError):
186+
self._api.address_with_details()
187+
140188
@responses.activate
141189
def test_addresswithdetails_whenerroroccurred(self):
142190
with open("testdata/models/geocoder_error.json", "r") as f:

0 commit comments

Comments
 (0)