Skip to content

Commit

Permalink
🚧(dashboard) add AnnuaireEntrepriseAPI client for enterprise data ret…
Browse files Browse the repository at this point in the history
…rieval

Introduced a new API client to communicate with the Annuaire Entreprise API.
The client includes methods for fetching enterprise information using SIREN, with placeholder values for token and context.
  • Loading branch information
ssorin committed Feb 17, 2025
1 parent e900df4 commit 02bacf4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/dashboard/apps/consent/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class IndexView(BreadcrumbContextMixin, TemplateView):
def get_context_data(self, **kwargs): # noqa: D102
context = super().get_context_data(**kwargs)
context["entities"] = self.request.user.get_entities()

entity = Entity.objects.get(id="d8e57e88-f9a8-49dd-ba8c-f52ee0a16137")
entity.siret = "55204944776279"
entity.save()
entity.populate_company_information()

return context


Expand Down
63 changes: 63 additions & 0 deletions src/dashboard/apps/core/annuaire_entreprise_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Dashboard core annuaire entreprise API."""
import requests


class CompanyInformationWrapper:
"""Company information wrapper."""
def __init__(self, company_info):
"""Initialize the wrapper."""
self.company_info = company_info

class CompanyAddressWrapper:
def __init__(self, company_info):
"""Initialize the wrapper."""
self.company_info = company_info

class AnnuaireEntrepriseAPI:
"""Annuaire entreprise API client."""

def __init__(self):
"""Initialize the API client."""
# todo : add in settings
self.api_url = "https://staging.entreprise.api.gouv.fr/v3/"
self.token = ""
# todo config context - recipient must be a SIREN
self.context = "?context=test+API&object=test+API&recipient=10000001700010"

def _make_request(self, endpoint, params=None):
"""Make a request to the API."""
headers = {"Authorization": f"Bearer {self.token}"}
url = f"{self.api_url}/{endpoint}"
response = requests.get(url, headers=headers, params=params)
return response.json()

def get_entreprise_info(self, siren):
"""Get the enterprise information from the API."""
endpoint = f"insee/sirene/unites_legales/{siren}{self.context}"
return self._make_request(endpoint)

def get_entreprise_address(self, siret):
"""Get the enterprise information from the API."""
# todo config context - recipient must be a SIREN
endpoint = f"insee/sirene/etablissements/{siret}/adresse{self.context}"
return self._make_request(endpoint)


# TEST ...
# api = AnnuaireEntrepriseAPI()
# _siren = "55204944776279"
# _siret = "552049447"
#
# entreprise_info = api.get_entreprise_info(_siret)
#
# print(entreprise_info)
# print(entreprise_info['data']["siren"]) #siren
# print(entreprise_info['data']["siret_siege_social"]) #siret_siege_social
# siret_siege = entreprise_info['data']["siret_siege_social"]
# print(entreprise_info['data']['activite_principale']["code"]) #naf
# print(entreprise_info['data']["personne_morale_attributs"]["raison_sociale"]) #name
# print(entreprise_info['data']["forme_juridique"]["libelle"]) #legal_form
#
#
# address = api.get_entreprise_address(siret_siege)
# print(address)
25 changes: 25 additions & 0 deletions src/dashboard/apps/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,31 @@ def get_awaiting_consents(self) -> QuerySet:
"""Get all awaiting consents for this entity."""
return self.get_consents(AWAITING)

def populate_company_information(self):
"""Populate the company informations of the entity with API data."""
from .annuaire_entreprise_api import AnnuaireEntrepriseAPI

if self.siret:
api = AnnuaireEntrepriseAPI()

siren = self.siret[:9]
info = api.get_entreprise_info(siren)
data = info["data"]
self.name = data["personne_morale_attributs"]["raison_sociale"]
self.legal_form = data["forme_juridique"]["libelle"]
naf = data['activite_principale']["code"]
self.naf = naf.replace(".", "")
self.siret_siege = data["siret_siege_social"]
self.save()

address = api.get_entreprise_address(self.siret_siege)
data = address["data"]
self.address_1 = (f"{data['numero_voie']} {data['indice_repetition_voie']} "
f"{data['type_voie']} {data['libelle_voie']}")
self.address_2 = data['complement_adresse']
self.address_city = data['libelle_commune']
self.address_zip_code = data['code_postal']
self.save()

class DeliveryPoint(DashboardBase):
"""Represents a delivery point for electric vehicles.
Expand Down

0 comments on commit 02bacf4

Please sign in to comment.