From 3a5cfd8e9a30df5a8fff46d522c461c8a4d8d996 Mon Sep 17 00:00:00 2001 From: vasileios Date: Thu, 22 Feb 2024 08:43:27 +0100 Subject: [PATCH] Added timeout field to the Service model --- manage.py | 4 +-- tests/test_config_adapter.py | 28 +++++++++++++++++++ zgw_consumers/client.py | 3 ++ .../migrations/0020_service_timeout.py | 22 +++++++++++++++ zgw_consumers/models/services.py | 5 ++++ 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 zgw_consumers/migrations/0020_service_timeout.py diff --git a/manage.py b/manage.py index cfde46a..9268fd1 100644 --- a/manage.py +++ b/manage.py @@ -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: @@ -16,5 +16,5 @@ def main(): execute_from_command_line(sys.argv) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/tests/test_config_adapter.py b/tests/test_config_adapter.py index 7cd28ce..0f9d7a9 100644 --- a/tests/test_config_adapter.py +++ b/tests/test_config_adapter.py @@ -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 diff --git a/zgw_consumers/client.py b/zgw_consumers/client.py index 8674291..fdaf9bd 100644 --- a/zgw_consumers/client.py +++ b/zgw_consumers/client.py @@ -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 diff --git a/zgw_consumers/migrations/0020_service_timeout.py b/zgw_consumers/migrations/0020_service_timeout.py new file mode 100644 index 0000000..34e23ab --- /dev/null +++ b/zgw_consumers/migrations/0020_service_timeout.py @@ -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", + ), + ), + ] diff --git a/zgw_consumers/models/services.py b/zgw_consumers/models/services.py index fbe45c2..594d403 100644 --- a/zgw_consumers/models/services.py +++ b/zgw_consumers/models/services.py @@ -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()