Skip to content

Commit 7ac11c1

Browse files
authored
Merge pull request #33 from gopaycommunity/feat-configurable-timeout
feat: configurable request timeout
2 parents ab0ca89 + 3f4f05b commit 7ac11c1

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

gopay/api.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from gopay.enums import ContentType, Language
77
from gopay.http import ApiClient, Request, Response
8-
from gopay.services import DefaultCache, default_logger
98

109

1110
@dataclass
@@ -24,15 +23,29 @@ def __post_init__(self):
2423
# Make sure URL will be in the form of example.com/api
2524
urlparts = urlsplit(self.config["gateway_url"])
2625
self.base_url = urlunsplit((urlparts.scheme, urlparts.netloc, "/api", "", ""))
26+
27+
# Prepare
28+
api_client_config = {
29+
"client_id": self.config["client_id"],
30+
"client_secret": self.config["client_secret"],
31+
"gateway_url": self.base_url,
32+
"scope": self.config["scope"],
33+
}
34+
35+
# Add optional parameters if found
36+
if (timeout := self.config.get("timeout")) is not None:
37+
api_client_config.update({"timeout": timeout})
38+
39+
if (logger := self.services.get("logger")) is not None:
40+
api_client_config.update({"logger": logger})
41+
42+
if (cache := self.services.get("cache")) is not None:
43+
api_client_config.update({"cache": cache})
44+
2745

2846
# Create the API client
2947
self.api_client = ApiClient(
30-
client_id=self.config["client_id"],
31-
client_secret=self.config["client_secret"],
32-
gateway_url=self.base_url,
33-
scope=self.config["scope"],
34-
logger=self.services.get("logger") or default_logger,
35-
cache=self.services.get("cache") or DefaultCache(),
48+
**api_client_config
3649
)
3750

3851
def call(

gopay/http.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class ApiClient:
8888
client_secret: str
8989
gateway_url: str
9090
scope: TokenScope
91+
timeout: int = 180
9192
logger: LoggerType = default_logger
9293
cache: AbstractCache = field(default_factory=DefaultCache)
9394

@@ -141,7 +142,7 @@ def send_request(self, request: Request) -> Response:
141142
auth=(self.client_id, self.client_secret) if request.basic_auth else None,
142143
data=request.body if request.content_type == ContentType.FORM else None,
143144
json=request.body if request.content_type == ContentType.JSON else None,
144-
timeout=300
145+
timeout=self.timeout
145146
)
146147

147148
# Build Response instance, try to decode body as JSON

gopay/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from pydantic import BaseModel, Extra
4-
4+
from typing import Optional
55
from gopay import enums
66

77

@@ -16,5 +16,6 @@ class GopayConfig(GopayModel):
1616
client_id: str
1717
client_secret: str
1818
gateway_url: str
19+
timeout: Optional[int] = None
1920
scope: enums.TokenScope = enums.TokenScope.ALL
2021
language: enums.Language = enums.Language.CZECH

tests/test_payments.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
import gopay
44
from gopay.enums import Language, TokenScope
5-
from gopay.http import AccessToken, Request, Response, TokenScope
5+
from gopay.http import AccessToken, Request, Response
66
from gopay.payments import Payments
77
from gopay.services import AbstractCache
88

9-
109
def mock_logger(request: Request, response: Response) -> None:
1110
pass
1211

@@ -45,6 +44,7 @@ def test_full_config(
4544
"gateway_url": gateway_url,
4645
"scope": TokenScope.ALL,
4746
"language": Language.CZECH,
47+
"timeout": 300
4848
}
4949
)
5050
assert isinstance(payments, Payments)

0 commit comments

Comments
 (0)