Skip to content

Commit

Permalink
Merge pull request #85 from maykinmedia/feature/add-custom-field-for-…
Browse files Browse the repository at this point in the history
…timeout-in-service

Add timeout field to the Service model
  • Loading branch information
sergei-maertens authored Feb 22, 2024
2 parents be1150e + 3a5cfd8 commit 82e56b6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
4 changes: 2 additions & 2 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'testapp.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testapp.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand All @@ -16,5 +16,5 @@ def main():
execute_from_command_line(sys.argv)


if __name__ == '__main__':
if __name__ == "__main__":
main()
28 changes: 28 additions & 0 deletions tests/test_config_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,31 @@ def test_zgw_auth():

headers = m.last_request.headers
assert "Authorization" in headers


def test_default_timeout():
service = ServiceFactory.build(api_root="https://example.com/")
adapter = ServiceConfigAdapter(service)
client = APIClient.configure_from(adapter)

with requests_mock.Mocker() as m, client:
m.get("https://example.com/foo")

client.get("foo")

timeout = m.last_request.timeout
assert timeout == 10


def test_custom_timeout():
service = ServiceFactory.build(api_root="https://example.com/", timeout=30)
adapter = ServiceConfigAdapter(service)
client = APIClient.configure_from(adapter)

with requests_mock.Mocker() as m, client:
m.get("https://example.com/foo")

client.get("foo")

timeout = m.last_request.timeout
assert timeout == 30
3 changes: 3 additions & 0 deletions zgw_consumers/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def get_client_session_kwargs(self) -> dict[str, Any]:
case AuthTypes.zgw:
kwargs["auth"] = ZGWAuth(service=self.service)

# set timeout for the requests
kwargs["timeout"] = self.service.timeout

return kwargs


Expand Down
22 changes: 22 additions & 0 deletions zgw_consumers/migrations/0020_service_timeout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.24 on 2024-02-22 01:41

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("zgw_consumers", "0019_alter_service_uuid"),
]

operations = [
migrations.AddField(
model_name="service",
name="timeout",
field=models.PositiveSmallIntegerField(
default=10,
help_text="Timeout (in seconds) for HTTP calls.",
verbose_name="timeout",
),
),
]
5 changes: 5 additions & 0 deletions zgw_consumers/models/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class Service(RestAPIService):
on_delete=models.PROTECT,
related_name="service_server",
)
timeout = models.PositiveSmallIntegerField(
_("timeout"),
help_text=_("Timeout (in seconds) for HTTP calls."),
default=10,
)

objects = ServiceManager()

Expand Down

0 comments on commit 82e56b6

Please sign in to comment.