Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/dapla_metadata/_shared/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from importlib import metadata

DAPLA_TOOLBELT_METADATA_PACKAGE_NAME = "dapla-toolbelt-metadata"
TEAM_METADATA_CONTACT_EMAIL = "metadata@ssb.no"


def get_app_version() -> str:
"""Get the version of the this package."""
return metadata.distribution(DAPLA_TOOLBELT_METADATA_PACKAGE_NAME).version


def get_user_agent() -> str:
"""Get the value for the Use-Agent header."""
return f"{DAPLA_TOOLBELT_METADATA_PACKAGE_NAME}/{get_app_version()} ({TEAM_METADATA_CONTACT_EMAIL})"
7 changes: 6 additions & 1 deletion src/dapla_metadata/datasets/statistic_subject_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from bs4 import BeautifulSoup
from bs4 import ResultSet

from dapla_metadata._shared.utils import get_user_agent
from dapla_metadata.datasets.external_sources.external_sources import GetExternalSource
from dapla_metadata.datasets.utility.enums import SupportedLanguages

Expand Down Expand Up @@ -121,7 +122,11 @@ def _fetch_data_from_external_source(self) -> ResultSet | None:
return None

try:
response = requests.get(str(self.source_url), timeout=30)
response = requests.get(
str(self.source_url),
headers={"User-Agent": get_user_agent()},
timeout=30,
)
response.encoding = "utf-8"
logger.debug("Got response %s from %s", response, self.source_url)
soup = BeautifulSoup(response.text, features="xml")
Expand Down
2 changes: 2 additions & 0 deletions src/dapla_metadata/variable_definitions/_utils/_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dapla_metadata._shared.utils import get_user_agent
from dapla_metadata.variable_definitions._generated.vardef_client.api_client import (
ApiClient,
)
Expand Down Expand Up @@ -40,5 +41,6 @@ def get_client(cls) -> ApiClient:
if not cls._config:
cls._config = get_vardef_client_configuration()
cls._client = ApiClient(cls._config)
cls._client.user_agent = get_user_agent()
cls._client.configuration.access_token = refresh_access_token()
return cls._client
Loading