Skip to content

Commit

Permalink
Merge pull request #33 from gopaycommunity/feat-configurable-timeout
Browse files Browse the repository at this point in the history
feat: configurable request timeout
  • Loading branch information
geeshta authored Aug 27, 2024
2 parents ab0ca89 + 3f4f05b commit 7ac11c1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
27 changes: 20 additions & 7 deletions gopay/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from gopay.enums import ContentType, Language
from gopay.http import ApiClient, Request, Response
from gopay.services import DefaultCache, default_logger


@dataclass
Expand All @@ -24,15 +23,29 @@ def __post_init__(self):
# Make sure URL will be in the form of example.com/api
urlparts = urlsplit(self.config["gateway_url"])
self.base_url = urlunsplit((urlparts.scheme, urlparts.netloc, "/api", "", ""))

# Prepare
api_client_config = {
"client_id": self.config["client_id"],
"client_secret": self.config["client_secret"],
"gateway_url": self.base_url,
"scope": self.config["scope"],
}

# Add optional parameters if found
if (timeout := self.config.get("timeout")) is not None:
api_client_config.update({"timeout": timeout})

if (logger := self.services.get("logger")) is not None:
api_client_config.update({"logger": logger})

if (cache := self.services.get("cache")) is not None:
api_client_config.update({"cache": cache})


# Create the API client
self.api_client = ApiClient(
client_id=self.config["client_id"],
client_secret=self.config["client_secret"],
gateway_url=self.base_url,
scope=self.config["scope"],
logger=self.services.get("logger") or default_logger,
cache=self.services.get("cache") or DefaultCache(),
**api_client_config
)

def call(
Expand Down
3 changes: 2 additions & 1 deletion gopay/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class ApiClient:
client_secret: str
gateway_url: str
scope: TokenScope
timeout: int = 180
logger: LoggerType = default_logger
cache: AbstractCache = field(default_factory=DefaultCache)

Expand Down Expand Up @@ -141,7 +142,7 @@ def send_request(self, request: Request) -> Response:
auth=(self.client_id, self.client_secret) if request.basic_auth else None,
data=request.body if request.content_type == ContentType.FORM else None,
json=request.body if request.content_type == ContentType.JSON else None,
timeout=300
timeout=self.timeout
)

# Build Response instance, try to decode body as JSON
Expand Down
3 changes: 2 additions & 1 deletion gopay/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from pydantic import BaseModel, Extra

from typing import Optional
from gopay import enums


Expand All @@ -16,5 +16,6 @@ class GopayConfig(GopayModel):
client_id: str
client_secret: str
gateway_url: str
timeout: Optional[int] = None
scope: enums.TokenScope = enums.TokenScope.ALL
language: enums.Language = enums.Language.CZECH
4 changes: 2 additions & 2 deletions tests/test_payments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import gopay
from gopay.enums import Language, TokenScope
from gopay.http import AccessToken, Request, Response, TokenScope
from gopay.http import AccessToken, Request, Response
from gopay.payments import Payments
from gopay.services import AbstractCache


def mock_logger(request: Request, response: Response) -> None:
pass

Expand Down Expand Up @@ -45,6 +44,7 @@ def test_full_config(
"gateway_url": gateway_url,
"scope": TokenScope.ALL,
"language": Language.CZECH,
"timeout": 300
}
)
assert isinstance(payments, Payments)
Expand Down

0 comments on commit 7ac11c1

Please sign in to comment.