Skip to content

Commit

Permalink
Merge pull request #6 from open-formulieren/feature/client-timeout
Browse files Browse the repository at this point in the history
✨ Add configurable client timeout to config
  • Loading branch information
joeribekker authored Feb 13, 2024
2 parents d5ee808 + 45d1bd9 commit e81630d
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions openformsclient/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ConfigurationAdmin(SingletonModelAdmin):
"fields": (
"api_root",
"api_token",
"client_timeout",
"status",
)
},
Expand Down
6 changes: 3 additions & 3 deletions openformsclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@


class Client:
def __init__(self, api_root, api_token):
def __init__(self, api_root, api_token, client_timeout):
self.api_root = api_root
self.api_token = api_token
self.timeout = client_timeout

def _request(self, method, relative_url, **extra_kwargs):

kwargs = {
"headers": {"Authorization": f"Token {self.api_token}"},
"timeout": 5,
"timeout": self.timeout,
}
kwargs.update(extra_kwargs)

Expand Down
1 change: 0 additions & 1 deletion openformsclient/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

initial = True

dependencies = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("openformsclient", "0001_initial"),
]
Expand Down
21 changes: 21 additions & 0 deletions openformsclient/migrations/0003_configuration_client_timeout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.2.23 on 2024-02-13 08:57

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("openformsclient", "0002_alter_configuration_options"),
]

operations = [
migrations.AddField(
model_name="configuration",
name="client_timeout",
field=models.PositiveIntegerField(
default=5,
help_text="The timeout that is used for requests (in seconds)",
verbose_name="Client request timeout",
),
),
]
7 changes: 6 additions & 1 deletion openformsclient/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class Configuration(SingletonModel):
"The Open Forms API token value. Example: 7ab84e80b3d68d52a5f9e1712e3d0eda27d21e58"
),
)
client_timeout = models.PositiveIntegerField(
_("Client request timeout"),
default=5,
help_text=_("The timeout that is used for requests (in seconds)"),
)

sdk_css_url = models.URLField(
_("SDK CSS URL"),
Expand Down Expand Up @@ -73,7 +78,7 @@ def save(self, *args, **kwargs):

@cached_property
def client(self):
return Client(self.api_root, self.api_token)
return Client(self.api_root, self.api_token, self.client_timeout)


class OpenFormsBaseField:
Expand Down
17 changes: 15 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest.mock import patch

from django.test import TestCase

import requests_mock
Expand All @@ -11,11 +13,12 @@ class ClientTests(TestCase):
def setUp(self):
self.api_root = "https://example.com/api/v1/"
self.api_token = "token"
self.client = Client(self.api_root, self.api_token)
self.client_timeout = 2
self.client = Client(self.api_root, self.api_token, self.client_timeout)

def test_has_config(self, m):
self.assertTrue(self.client.has_config())
self.assertFalse(Client("", "").has_config())
self.assertFalse(Client("", "", "").has_config())

def test_is_healthy(self, m):
m.head(
Expand Down Expand Up @@ -76,3 +79,13 @@ def test_get_form_with_error(self, m):

with self.assertRaises(HTTPError):
self.client.get_form("myform")

def test_request_uses_configured_timeout(self, m):
with patch("openformsclient.client.requests.request") as mock_request:
self.client.get_form("myform")
mock_request.assert_called_with(
"get",
"https://example.com/api/v1/forms/myform",
headers={"Authorization": "Token token"},
timeout=2,
)
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def setUp(self):
)

def test_get_form_choices_without_config(self, m):
client = Client("", "")
client = Client("", "", "")
result = get_form_choices(client)
self.assertEqual(result, [])

Expand Down

0 comments on commit e81630d

Please sign in to comment.