Skip to content

Commit

Permalink
[#3688] Add a config check and a placeholder client
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Feb 1, 2024
1 parent a043c53 commit 527f818
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/openforms/contrib/objects_api/clients/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .objects import ObjectsClient
from .objects import ObjectsClient, ObjecttypesClient

__all__ = ["ObjectsClient"]
__all__ = ["ObjectsClient", "ObjecttypesClient"]
4 changes: 4 additions & 0 deletions src/openforms/contrib/objects_api/clients/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ def create_object(self, object_data: dict) -> dict:
response.raise_for_status()

return response.json()


class ObjecttypesClient(NLXClient):
pass
15 changes: 13 additions & 2 deletions src/openforms/registrations/contrib/objects_api/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@

from openforms.plugins.exceptions import InvalidPluginConfiguration

from .client import NoServiceConfigured, get_documents_client, get_objects_client
from .client import (
NoServiceConfigured,
get_documents_client,
get_objects_client,
get_objecttypes_client,
)
from .models import ObjectsAPIConfig


def check_objects_service():
with get_objects_client() as client:
client.get("objects")
client.get("objects", params={"pageSize": 1})


def check_objecttypes_service():
with get_objecttypes_client() as client:
client.get("objecttypes", params={"pageSize": 1})


def check_documents_service():
Expand All @@ -22,6 +32,7 @@ def check_config():
# First, check that the necessary services are configured correctly.
services = (
(check_objects_service, "objects_service"),
(check_objecttypes_service, "objecttypes_service"),
(check_documents_service, "drc_service"),
)

Expand Down
13 changes: 10 additions & 3 deletions src/openforms/registrations/contrib/objects_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
"""

from zgw_consumers.client import build_client
from zgw_consumers.nlx import NLXClient

from openforms.contrib.objects_api.clients import ObjectsClient
from openforms.contrib.objects_api.clients import ObjectsClient, ObjecttypesClient
from openforms.contrib.zgw.clients import DocumentenClient

from .models import ObjectsAPIConfig
Expand All @@ -22,14 +21,22 @@ class NoServiceConfigured(RuntimeError):
pass


def get_objects_client() -> NLXClient:
def get_objects_client() -> ObjectsClient:
config = ObjectsAPIConfig.get_solo()
assert isinstance(config, ObjectsAPIConfig)
if not (service := config.objects_service):
raise NoServiceConfigured("No Objects API service configured!")
return build_client(service, client_factory=ObjectsClient)


def get_objecttypes_client() -> ObjecttypesClient:
config = ObjectsAPIConfig.get_solo()
assert isinstance(config, ObjectsAPIConfig)
if not (service := config.objecttypes_service):
raise NoServiceConfigured("No Objects API service configured!")

Check warning on line 36 in src/openforms/registrations/contrib/objects_api/client.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/registrations/contrib/objects_api/client.py#L36

Added line #L36 was not covered by tests
return build_client(service, client_factory=ObjecttypesClient)


def get_documents_client() -> DocumentenClient:
config = ObjectsAPIConfig.get_solo()
assert isinstance(config, ObjectsAPIConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def setUp(self):
objects_service=ServiceFactory.build(
api_root="https://objects.example.com/api/v1/",
),
objecttypes_service=ServiceFactory.build(
api_root="https://objecttypes.example.com/api/v1/",
),
drc_service=ServiceFactory.build(
api_root="https://documents.example.com/api/v1/",
api_type=APITypes.drc,
Expand All @@ -41,7 +44,11 @@ def setUp(self):

def _mockForValidServiceConfiguration(self, m: requests_mock.Mocker) -> None:
m.get(
"https://objects.example.com/api/v1/objects",
"https://objects.example.com/api/v1/objects?pageSize=1",
json={"results": []},
)
m.get(
"https://objecttypes.example.com/api/v1/objecttypes?pageSize=1",
json={"results": []},
)
m.get(
Expand All @@ -59,7 +66,7 @@ def test_no_objects_service_configured(self):
@requests_mock.Mocker()
def test_objects_service_misconfigured(self, m):
m.get(
"https://objects.example.com/api/v1/objects",
"https://objects.example.com/api/v1/objects?pageSize=1",
exc=requests.ConnectionError,
)
plugin = ObjectsAPIRegistration(PLUGIN_IDENTIFIER)
Expand All @@ -71,7 +78,11 @@ def test_objects_service_misconfigured(self, m):
def test_no_documents_service_configured(self, m):
self.config.drc_service = None
m.get(
"https://objects.example.com/api/v1/objects",
"https://objects.example.com/api/v1/objects?pageSize=1",
json={"results": []},
)
m.get(
"https://objecttypes.example.com/api/v1/objecttypes?pageSize=1",
json={"results": []},
)
plugin = ObjectsAPIRegistration(PLUGIN_IDENTIFIER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from django.test import TestCase

from zgw_consumers.constants import APITypes

from zgw_consumers_ext.tests.factories import ServiceFactory
from zgw_consumers.test.factories import ServiceFactory

from ..models import ObjectsAPIConfig

Expand Down

0 comments on commit 527f818

Please sign in to comment.