Skip to content

Commit

Permalink
✨(backend) make backend request timeout configurable
Browse files Browse the repository at this point in the history
Sometime error can be raised as our payment backend reached the request
timeout sets to 5s. So we make this value configurable to be able to increase it
 with ease.
  • Loading branch information
jbpenrath committed Oct 22, 2024
1 parent a841f0b commit 4dcd9f4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to
- Add `appendix` field on `ContractDefinition` model
- Allow to edit `appendix` `ContractDefinition` field through the back office

### Changed

- Make payment backend request timeout configurable

## [2.8.0] - 2024-10-16

### Added
Expand Down
5 changes: 5 additions & 0 deletions src/backend/joanie/payment/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class BasePaymentBackend:

name = "base"

@property
def timeout(self):
"""A class property to get the request timeout value from the settings"""
return settings.JOANIE_PAYMENT_BACKEND["timeout"]

def __init__(self, configuration=None):
self.configuration = configuration

Expand Down
4 changes: 3 additions & 1 deletion src/backend/joanie/payment/backends/lyra/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ def _call_api(self, url, payload):
logger.info("Calling Lyra API %s", url, extra={"context": context})

try:
response = requests.post(url, json=payload, headers=self.headers, timeout=5)
response = requests.post(
url, json=payload, headers=self.headers, timeout=self.timeout
)
response.raise_for_status()
except requests.exceptions.RequestException as e:
context = context.copy()
Expand Down
4 changes: 4 additions & 0 deletions src/backend/joanie/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,9 @@ class Base(Configuration):
environ_name="JOANIE_PAYMENT_BACKEND",
environ_prefix=None,
),
"timeout": values.PositiveIntegerValue(
5, environ_name="JOANIE_PAYMENT_TIMEOUT", environ_prefix=None
),
# Check the docstring of the related payment backend to know
# which dict to pass here.
"configuration": values.DictValue(
Expand Down Expand Up @@ -745,6 +748,7 @@ class Test(Base):

JOANIE_PAYMENT_BACKEND = {
"backend": "joanie.payment.backends.dummy.DummyPaymentBackend",
"timeout": 5,
}

JOANIE_SIGNATURE_BACKEND = "joanie.signature.backends.dummy.DummySignatureBackend"
Expand Down
13 changes: 13 additions & 0 deletions src/backend/joanie/tests/payment/test_backend_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ def test_payment_backend_base_configuration(self):

self.assertEqual(backend.configuration, {"secret": "aDummyPassphraseForTest"})

@override_settings(
JOANIE_PAYMENT_BACKEND={
"backend": "joanie.core.payment.backends.dummy.DummyPaymentBackend",
"timeout": 42,
},
)
def test_payment_backend_base_request_timeout_is_configurable(self):
"""
The request timeout should be configurable in the settings.
"""
backend = BasePaymentBackend()
self.assertEqual(backend.timeout, 42)

def test_payment_backend_base_create_payment_not_implemented(self):
"""Invoke create_payment should raise a Not ImplementedError"""
backend = BasePaymentBackend()
Expand Down

0 comments on commit 4dcd9f4

Please sign in to comment.