Skip to content

Commit

Permalink
refactor(common): centraliser la logique API Adresse dans un nouveau …
Browse files Browse the repository at this point in the history
…fichier api/adresse.py (#4963)
  • Loading branch information
raphodn authored Jan 29, 2025
1 parent aab167f commit dc04f52
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
2 changes: 1 addition & 1 deletion api/views/canteen.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
SatelliteCanteenSerializer,
)
from api.views.utils import camelize, update_change_reason_with_auth
from common.api.adresse import fetch_geo_data_from_api_entreprise_by_siret
from common.api.insee import (
fetch_geo_data_from_api_insee_sirene_by_siret,
get_token_sirene,
Expand All @@ -80,7 +81,6 @@
Teledeclaration,
)
from data.region_choices import Region
from macantine.utils import fetch_geo_data_from_api_entreprise_by_siret

logger = logging.getLogger(__name__)
redis = r.from_url(settings.REDIS_URL, decode_responses=True)
Expand Down
45 changes: 45 additions & 0 deletions common/api/adresse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import logging

import requests

from data.region_choices import Region

logger = logging.getLogger(__name__)

REGIONS_LIB = {i.label.split(" - ")[1]: i.value for i in Region}


def fetch_geo_data_from_api_entreprise_by_siret(response):
try:
location_response = requests.get(
f"https://api-adresse.data.gouv.fr/search/?q={response['cityInseeCode']}&citycode={response['cityInseeCode']}&type=municipality&autocomplete=1"
)
if location_response.ok:
location_response = location_response.json()
results = location_response["features"]
if results and results[0]:
try:
result = results[0]["properties"]
if result:
response["city"] = result["label"]
response["city_insee_code"] = result["citycode"]
response["postal_code"] = result["postcode"]
response["department"] = result["context"].split(",")[0]
response["department_lib"] = result["context"].split(", ")[1]
response["region_lib"] = result["context"].split(", ")[2]
response["region"] = int(REGIONS_LIB[response["region_lib"]])
return response
except KeyError as e:
logger.warning(f"unexpected location response format : {location_response}. Unknown key : {e}")
else:
logger.warning(
f"features array for city '{response['city']}' in location response format is non-existant or empty : {location_response}"
)
else:
logger.warning(
f"location fetching failed, code {location_response.status_code} : {location_response.json()}"
)
except Exception as e:
logger.exception(f"Error completing location data with SIRET for city: {response['city']}")
logger.exception(e)
return response
40 changes: 0 additions & 40 deletions macantine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
import requests
from django.conf import settings

from data.region_choices import Region

logger = logging.getLogger(__name__)
redis = r.from_url(settings.REDIS_URL, decode_responses=True)

REGIONS_LIB = {i.label.split(" - ")[1]: i.value for i in Region}

CAMPAIGN_DATES = {
2021: {
"start_date": datetime(2022, 7, 16, 0, 0, tzinfo=zoneinfo.ZoneInfo("Europe/Paris")),
Expand All @@ -31,39 +27,3 @@
"end_date": datetime(2025, 3, 31, 0, 0, tzinfo=zoneinfo.ZoneInfo("Europe/Paris")),
},
}


def fetch_geo_data_from_api_entreprise_by_siret(response):
try:
location_response = requests.get(
f"https://api-adresse.data.gouv.fr/search/?q={response['cityInseeCode']}&citycode={response['cityInseeCode']}&type=municipality&autocomplete=1"
)
if location_response.ok:
location_response = location_response.json()
results = location_response["features"]
if results and results[0]:
try:
result = results[0]["properties"]
if result:
response["city"] = result["label"]
response["city_insee_code"] = result["citycode"]
response["postal_code"] = result["postcode"]
response["department"] = result["context"].split(",")[0]
response["department_lib"] = result["context"].split(", ")[1]
response["region_lib"] = result["context"].split(", ")[2]
response["region"] = int(REGIONS_LIB[response["region_lib"]])
return response
except KeyError as e:
logger.warning(f"unexpected location response format : {location_response}. Unknown key : {e}")
else:
logger.warning(
f"features array for city '{response['city']}' in location response format is non-existant or empty : {location_response}"
)
else:
logger.warning(
f"location fetching failed, code {location_response.status_code} : {location_response.json()}"
)
except Exception as e:
logger.exception(f"Error completing location data with SIRET for city: {response['city']}")
logger.exception(e)
return response

0 comments on commit dc04f52

Please sign in to comment.