diff --git a/meorg_client/client.py b/meorg_client/client.py index 05e2ef4..a6d2c3d 100644 --- a/meorg_client/client.py +++ b/meorg_client/client.py @@ -112,6 +112,9 @@ def _make_request( # Assemble the headers _headers = self._merge_headers(headers) + # Attach the user agent + _headers['user-agent'] = mu.get_user_agent() + # Make the request, set it as the last response for future use self.last_response = func( url, data=data, json=json, headers=_headers, files=files, **kwargs diff --git a/meorg_client/tests/test_utilities.py b/meorg_client/tests/test_utilities.py index a1628eb..d304f9d 100644 --- a/meorg_client/tests/test_utilities.py +++ b/meorg_client/tests/test_utilities.py @@ -26,3 +26,8 @@ def test_load_package_data(): """Test load_package_data.""" result = mu.load_package_data("openapi.json") assert isinstance(result, dict) + + +def test_get_user_agent(): + """Test get_user_agent.""" + assert "meorg_client/" in mu.get_user_agent() diff --git a/meorg_client/utilities.py b/meorg_client/utilities.py index 8625a22..2895ccc 100644 --- a/meorg_client/utilities.py +++ b/meorg_client/utilities.py @@ -6,6 +6,8 @@ import os from pathlib import Path from importlib import resources +from . import _version +import platform # Single argument decoding functions. @@ -105,3 +107,23 @@ def get_uploaded_file_ids(response): def is_dev_mode(): return os.getenv("MEORG_DEV_MODE", "0") == "1" + + +def get_user_agent() -> str: + """Return the client's user agent to send along with the request. + + Returns + ------- + str + User agent. + """ + template = "{product}/{product_version} ({system_information})" + system_information = f"{platform.system()} {platform.machine()}" + + directives = dict( + product="meorg_client", + product_version=_version.get_versions()["version"], + system_information=system_information, + ) + + return template.format(**directives)