Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/user agent version #37

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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