Skip to content

Commit

Permalink
(PC-34085)[API] fix: API Adresse query fails when address starts with…
Browse files Browse the repository at this point in the history
… quotes
  • Loading branch information
prouzet-pass authored and dramelet-pass committed Feb 5, 2025
1 parent e1ee0d3 commit 736717c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
14 changes: 10 additions & 4 deletions api/src/pcapi/connectors/api_adresse.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,20 @@ def _search_csv(self, files: list) -> str:
response = self._request("POST", url, files=files, timeout=60)
return response.text

@classmethod
def _remove_quotes(cls, value: str | None) -> str | None:
# API Adresse fails when q starts with non-alphanumeric characters, which are useless for search.
# Similar to PC Pro in pro/src/commons/utils/removeQuotes.ts
return re.sub('["“”«»]', "", value).strip("' ") if value is not None else None

def get_municipality_centroid(
self, city: str, postcode: str | None = None, citycode: str | None = None
) -> AddressInfo:
"""Fallback to querying the city, because the q parameter must contain part of the address label"""
if len(city) < 3 and postcode is not None:
city = f"{postcode} {city}"
params = {
"q": city,
"q": self._remove_quotes(city),
"postcode": postcode,
"citycode": citycode,
"type": "municipality",
Expand Down Expand Up @@ -361,10 +367,10 @@ def get_single_address_result(
If no result is found, we return the centroid of the municipality
"""
params = {
"q": address,
"q": self._remove_quotes(address),
"postcode": postcode,
"citycode": citycode,
"city": city,
"city": self._remove_quotes(city),
"autocomplete": 0,
"limit": 1,
}
Expand Down Expand Up @@ -438,7 +444,7 @@ def _format_result(self, data: dict) -> AddressInfo:

def search_address(self, address: str, limit: int) -> list[AddressInfo]:
params = {
"q": address,
"q": self._remove_quotes(address),
"autocomplete": 0,
"limit": limit,
}
Expand Down
13 changes: 12 additions & 1 deletion api/tests/connectors/api_adresse/api_adresse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_nominal_case(requests_mock):
postcode = "75018"
city = "Paris"
requests_mock.get(
"https://api-adresse.data.gouv.fr/search",
"https://api-adresse.data.gouv.fr/search?q=18+Rue+Duhesme&postcode=75018&city=Paris&autocomplete=0&limit=1",
json=fixtures.ONE_FEATURE_RESPONSE,
)
address_info = api_adresse.get_address(address, postcode=postcode, city=city)
Expand All @@ -39,6 +39,17 @@ def test_nominal_case(requests_mock):
)


@pytest.mark.settings(ADRESSE_BACKEND="pcapi.connectors.api_adresse.ApiAdresseBackend")
@pytest.mark.parametrize("address", ['"18 Rue Duhesme"', "« 18 Rue Duhesme »", "“18 Rue Duhesme”", "'18 Rue Duhesme'"])
def test_with_quotes(requests_mock, address):
requests_mock.get(
"https://api-adresse.data.gouv.fr/search?q=18+Rue+Duhesme&postcode=75018&city=Paris&autocomplete=0&limit=1",
json=fixtures.ONE_FEATURE_RESPONSE,
)
api_adresse.get_address(address, postcode="75018", city="Paris")
assert requests_mock.call_count == 1


@pytest.mark.settings(ADRESSE_BACKEND="pcapi.connectors.api_adresse.ApiAdresseBackend")
def test_municipality_centroid_with_city_less_than_3_characters(requests_mock):
postcode = "80190"
Expand Down

0 comments on commit 736717c

Please sign in to comment.