Skip to content

Commit ff74f70

Browse files
authored
Merge pull request #37 from gopaycommunity/feature/user-agent-version
Feature/user agent version
2 parents 9185430 + 2251f8d commit ff74f70

6 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎gopay/__init__.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ def payments(config: dict, services: dict | None = None) -> Payments:
2121
elif key == "gatewayUrl":
2222
config["gateway_url"] = config[key]
2323
del config[key]
24+
elif key == "customUserAgent":
25+
config["custom_user_agent"] = config[key]
26+
del config[key]
2427

2528
# Use Pydantic to validate the config object
2629
config_model = GopayConfig.model_validate(config)

‎gopay/api.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from gopay.enums import ContentType, Language
77
from gopay.http import ApiClient, Request, Response
8+
from gopay.utils import DEFAULT_USER_AGENT
89

910

1011
@dataclass
@@ -63,10 +64,16 @@ def call(
6364
method=method, path=path, content_type=content_type, body=body
6465
)
6566

67+
user_agent = self.config.get("custom_user_agent")
68+
if user_agent is None:
69+
user_agent = DEFAULT_USER_AGENT
70+
else:
71+
user_agent = self.config["custom_user_agent"]
72+
6673
# Add some default headers
6774
request.headers = {
6875
"Accept": "application/json",
69-
"User-Agent": "GoPay Python SDK",
76+
"User-Agent": user_agent,
7077
"Accept-Language": "cs-CZ"
7178
if self.config["language"] in [Language.CZECH, Language.SLOVAK]
7279
else "en-US",

‎gopay/models.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ class GopayConfig(GopayModel):
1818
timeout: Optional[int] = None
1919
scope: enums.TokenScope = enums.TokenScope.ALL
2020
language: enums.Language = enums.Language.CZECH
21+
custom_user_agent: Optional[str] = None

‎gopay/utils.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from pathlib import Path
2+
3+
import tomli
4+
5+
def get_project_version():
6+
pyproject_path = Path(__file__).resolve().parent.parent / 'pyproject.toml'
7+
with open(pyproject_path, 'rb') as file:
8+
pyproject_data = tomli.load(file)
9+
return pyproject_data['tool']['poetry']['version']
10+
11+
DEFAULT_USER_AGENT = "GoPay Python " + get_project_version()

‎poetry.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pyproject.toml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ name = "gopay"
1414
packages = [{include = "gopay"}]
1515
readme = "README.md"
1616
repository = "https://github.com/gopaycommunity/gopay-python-api"
17-
version = "2.2.1"
17+
version = "2.2.2"
1818

1919
[tool.poetry.dependencies]
2020
deprecated = "^1.2.14"
2121
pydantic = "^2.8.2"
2222
python = "^3.9"
2323
requests = "^2.31.0"
24+
tomli = "^2.0.1"
2425

2526
[tool.poetry.group.dev.dependencies]
2627
black = ">=23.3,<25.0"

0 commit comments

Comments
 (0)