Skip to content

Commit

Permalink
Added user agent version
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaryska committed Sep 19, 2024
1 parent 0bf3ef9 commit 132c821
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
5 changes: 4 additions & 1 deletion gopay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ 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)
config = config_model.dict()

# Create and return the Payments and GoPay objects
gopay = GoPay(config, services or {})
return Payments(gopay)
return Payments(gopay)
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 @@ -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
8 changes: 8 additions & 0 deletions gopay/utils.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 132c821

Please sign in to comment.