diff --git a/gopay/__init__.py b/gopay/__init__.py index a9f7be9..628e25f 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.parse_obj(config) @@ -28,4 +31,4 @@ def payments(config: dict, services: dict | None = None) -> Payments: # Create and return the Payments and GoPay objects gopay = GoPay(config, services or {}) - return Payments(gopay) + return Payments(gopay) \ No newline at end of file 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 022aeb7..5f980da 100644 --- a/gopay/models.py +++ b/gopay/models.py @@ -19,3 +19,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..7e68aef --- /dev/null +++ b/gopay/utils.py @@ -0,0 +1,8 @@ +import tomli + +def get_project_version(): + with open('../pyproject.toml', '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/pyproject.toml b/pyproject.toml index 05242da..d82d5d4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,7 @@ name = "gopay" packages = [{include = "gopay"}] readme = "README.md" repository = "https://github.com/gopaycommunity/gopay-python-api" -version = "2.2.0" +version = "2.2.1" [tool.poetry.dependencies] deprecated = "^1.2.14"