diff --git a/gopay/__init__.py b/gopay/__init__.py index 4caf798..db4807e 100644 --- a/gopay/__init__.py +++ b/gopay/__init__.py @@ -21,6 +21,9 @@ def payments(config: dict, services: dict | None = None) -> Payments: elif key == "gatewayUrl": config["gateway_url"] = config[key] del config[key] + elif key == "customUserAgent": + config["custom_user_agent"] = config[key] + del config[key] # Use Pydantic to validate the config object config_model = GopayConfig.model_validate(config) diff --git a/gopay/api.py b/gopay/api.py index 9d8a5eb..f14bdfa 100644 --- a/gopay/api.py +++ b/gopay/api.py @@ -5,6 +5,7 @@ from gopay.enums import ContentType, Language from gopay.http import ApiClient, Request, Response +from gopay.utils import DEFAULT_USER_AGENT @dataclass @@ -63,10 +64,16 @@ def call( method=method, path=path, content_type=content_type, body=body ) + user_agent = self.config.get("custom_user_agent") + if user_agent is None: + user_agent = DEFAULT_USER_AGENT + else: + user_agent = self.config["custom_user_agent"] + # Add some default headers request.headers = { "Accept": "application/json", - "User-Agent": "GoPay Python SDK", + "User-Agent": user_agent, "Accept-Language": "cs-CZ" if self.config["language"] in [Language.CZECH, Language.SLOVAK] else "en-US", diff --git a/gopay/models.py b/gopay/models.py index b54b7f8..99620be 100644 --- a/gopay/models.py +++ b/gopay/models.py @@ -18,3 +18,4 @@ class GopayConfig(GopayModel): timeout: Optional[int] = None scope: enums.TokenScope = enums.TokenScope.ALL language: enums.Language = enums.Language.CZECH + custom_user_agent: Optional[str] = None diff --git a/gopay/utils.py b/gopay/utils.py new file mode 100644 index 0000000..fc6ac76 --- /dev/null +++ b/gopay/utils.py @@ -0,0 +1,11 @@ +from pathlib import Path + +import tomli + +def get_project_version(): + pyproject_path = Path(__file__).resolve().parent.parent / 'pyproject.toml' + with open(pyproject_path, 'rb') as file: + pyproject_data = tomli.load(file) + return pyproject_data['tool']['poetry']['version'] + +DEFAULT_USER_AGENT = "GoPay Python " + get_project_version() \ No newline at end of file diff --git a/poetry.lock b/poetry.lock index 356811d..eca17a9 100644 --- a/poetry.lock +++ b/poetry.lock @@ -825,4 +825,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "e984eba9db1628d65127ff2e8b0a701d72e618402ae14f29c726652ae279e531" +content-hash = "79b25fc828ef3770baacad1402d7793f1b069a58766ac7d364d8b0bf29df9f3f" diff --git a/pyproject.toml b/pyproject.toml index a397bf7..72a1baf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,13 +14,14 @@ name = "gopay" packages = [{include = "gopay"}] readme = "README.md" repository = "https://github.com/gopaycommunity/gopay-python-api" -version = "2.2.1" +version = "2.2.2" [tool.poetry.dependencies] deprecated = "^1.2.14" pydantic = "^2.8.2" python = "^3.9" requests = "^2.31.0" +tomli = "^2.0.1" [tool.poetry.group.dev.dependencies] black = ">=23.3,<25.0"