Skip to content

Commit

Permalink
Merge pull request #37 from gopaycommunity/feature/user-agent-version
Browse files Browse the repository at this point in the history
Feature/user agent version
  • Loading branch information
pmaryska authored Sep 24, 2024
2 parents 9185430 + 2251f8d commit ff74f70
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions gopay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion gopay/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions gopay/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions gopay/utils.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ff74f70

Please sign in to comment.