From bc57ab687ec2355f5313addde8f1a05d82fbd8cc Mon Sep 17 00:00:00 2001 From: Victor Perron Date: Tue, 11 Feb 2025 18:02:03 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Add=20a=20minimum=20quality=20score=20to=20?= =?UTF-8?q?data=E2=8B=85inclusion=20API=20requests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One of the latest additions to the dataâ‹…inclusion API is a quality score, enabling the API consumer to select only services that respect a combination of various quality factors. This ensures the redirected service is not too poorly filled in. --- config/settings/base.py | 1 + itou/utils/apis/data_inclusion.py | 1 + tests/utils/apis/test_data_inclusion_api.py | 1 + 3 files changed, 3 insertions(+) diff --git a/config/settings/base.py b/config/settings/base.py index 1d596f7df4..5eb4a39a1a 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -386,6 +386,7 @@ API_DATA_INCLUSION_BASE_URL = os.getenv("API_DATA_INCLUSION_BASE_URL") API_DATA_INCLUSION_TOKEN = os.getenv("API_DATA_INCLUSION_TOKEN") API_DATA_INCLUSION_SOURCES = os.getenv("API_DATA_INCLUSION_SOURCES", "").split(",") +API_DATA_INCLUSION_SCORE_QUALITE_MINIMUM = os.getenv("API_DATA_INCLUSION_SCORE_QUALITE_MINIMUM", 0.6) API_GEIQ_LABEL_BASE_URL = os.getenv("API_GEIQ_LABEL_BASE_URL") API_GEIQ_LABEL_TOKEN = os.getenv("API_GEIQ_LABEL_TOKEN") diff --git a/itou/utils/apis/data_inclusion.py b/itou/utils/apis/data_inclusion.py index 386e81b4df..6c4e5247da 100644 --- a/itou/utils/apis/data_inclusion.py +++ b/itou/utils/apis/data_inclusion.py @@ -38,6 +38,7 @@ def search_services(self, code_insee: str) -> list[dict]: "code_insee": code_insee, "sources": settings.API_DATA_INCLUSION_SOURCES, "thematiques": API_THEMATIQUES, + "score_qualite_minimum": settings.API_DATA_INCLUSION_SCORE_QUALITE_MINIMUM, }, ).raise_for_status() except httpx.HTTPError as exc: diff --git a/tests/utils/apis/test_data_inclusion_api.py b/tests/utils/apis/test_data_inclusion_api.py index da616bd7a5..9ce3fa9257 100644 --- a/tests/utils/apis/test_data_inclusion_api.py +++ b/tests/utils/apis/test_data_inclusion_api.py @@ -31,6 +31,7 @@ def test_data_inclusion_client(settings, respx_mock): assert parse_qs(str(api_mock.calls[0].request.url.params)) == { "code_insee": ["fake-insee-code"], "sources": ["dora,toto"], + "score_qualite_minimum": ["0.6"], "thematiques": [ "acces-aux-droits-et-citoyennete", "accompagnement-social-et-professionnel-personnalise", From e33ded5d6f7248d2a6589370c87372d289be1853 Mon Sep 17 00:00:00 2001 From: Victor Perron Date: Wed, 12 Feb 2025 14:36:07 +0100 Subject: [PATCH 2/2] data_inclusion_api: Clarify the case when no source is specified --- config/settings/base.py | 2 +- itou/utils/apis/data_inclusion.py | 14 ++++++++------ tests/utils/apis/test_data_inclusion_api.py | 10 +++++++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/config/settings/base.py b/config/settings/base.py index 5eb4a39a1a..69a9a30d5b 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -385,7 +385,7 @@ API_DATA_INCLUSION_BASE_URL = os.getenv("API_DATA_INCLUSION_BASE_URL") API_DATA_INCLUSION_TOKEN = os.getenv("API_DATA_INCLUSION_TOKEN") -API_DATA_INCLUSION_SOURCES = os.getenv("API_DATA_INCLUSION_SOURCES", "").split(",") +API_DATA_INCLUSION_SOURCES = os.getenv("API_DATA_INCLUSION_SOURCES") API_DATA_INCLUSION_SCORE_QUALITE_MINIMUM = os.getenv("API_DATA_INCLUSION_SCORE_QUALITE_MINIMUM", 0.6) API_GEIQ_LABEL_BASE_URL = os.getenv("API_GEIQ_LABEL_BASE_URL") diff --git a/itou/utils/apis/data_inclusion.py b/itou/utils/apis/data_inclusion.py index 6c4e5247da..27c25537ef 100644 --- a/itou/utils/apis/data_inclusion.py +++ b/itou/utils/apis/data_inclusion.py @@ -31,15 +31,17 @@ def __init__(self, base_url: str, token: str): ) def search_services(self, code_insee: str) -> list[dict]: + params = { + "code_insee": code_insee, + "thematiques": API_THEMATIQUES, + "score_qualite_minimum": settings.API_DATA_INCLUSION_SCORE_QUALITE_MINIMUM, + } + if settings.API_DATA_INCLUSION_SOURCES: + params["sources"] = settings.API_DATA_INCLUSION_SOURCES.split(",") try: response = self.client.get( "/search/services", - params={ - "code_insee": code_insee, - "sources": settings.API_DATA_INCLUSION_SOURCES, - "thematiques": API_THEMATIQUES, - "score_qualite_minimum": settings.API_DATA_INCLUSION_SCORE_QUALITE_MINIMUM, - }, + params=params, ).raise_for_status() except httpx.HTTPError as exc: logger.info("data.inclusion request error code_insee=%s error=%s", code_insee, exc) diff --git a/tests/utils/apis/test_data_inclusion_api.py b/tests/utils/apis/test_data_inclusion_api.py index 9ce3fa9257..037f5f8627 100644 --- a/tests/utils/apis/test_data_inclusion_api.py +++ b/tests/utils/apis/test_data_inclusion_api.py @@ -1,3 +1,5 @@ +from urllib.parse import parse_qs + import httpx import pytest @@ -6,7 +8,6 @@ def test_data_inclusion_client(settings, respx_mock): client = DataInclusionApiClient("https://fake.api.gouv.fr/", "fake-token") - settings.API_DATA_INCLUSION_SOURCES = "dora,toto" api_mock = respx_mock.get("https://fake.api.gouv.fr/search/services") api_mock.respond( 200, @@ -26,11 +27,9 @@ def test_data_inclusion_client(settings, respx_mock): {"id": "svc3"}, {"id": "svc4"}, ] - from urllib.parse import parse_qs assert parse_qs(str(api_mock.calls[0].request.url.params)) == { "code_insee": ["fake-insee-code"], - "sources": ["dora,toto"], "score_qualite_minimum": ["0.6"], "thematiques": [ "acces-aux-droits-et-citoyennete", @@ -43,6 +42,11 @@ def test_data_inclusion_client(settings, respx_mock): } assert api_mock.calls[0].request.headers["authorization"] == "Bearer fake-token" + # check setting sources + settings.API_DATA_INCLUSION_SOURCES = "dora,toto" + client.search_services("fake-insee-code") + assert parse_qs(str(api_mock.calls[1].request.url.params))["sources"] == ["dora", "toto"] + # check exceptions api_mock.respond(200, json={"something": "else"}) with pytest.raises(DataInclusionApiException):