Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#3688] Implement Objecttypes client #3883

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/openforms/contrib/objects_api/clients/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .objects import ObjectsClient, ObjecttypesClient
from .objects import ObjectsClient
from .objecttypes import ObjecttypesClient

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

return response.json()


class ObjecttypesClient(NLXClient):
pass
54 changes: 54 additions & 0 deletions src/openforms/contrib/objects_api/clients/objecttypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from typing import Any
from uuid import UUID

from zgw_consumers.nlx import NLXClient

from openforms.utils.api_clients import PaginatedResponseData, pagination_helper


class ObjecttypesClient(NLXClient):

def _get_paginated(
self,
endpoint: str,
page: int | None = None,
page_size: int | None = None,
query_params: dict[Any, Any] | None = None,
):
query_params = query_params or {}
if page is None and page_size is None:
response = self.get(endpoint, params=query_params)
response.raise_for_status()
data: PaginatedResponseData[dict[str, Any]] = response.json()
all_data = pagination_helper(self, data)
return list(all_data)

if page is not None:
query_params["page"] = page
if page_size is not None:
query_params["pageSize"] = page_size

response = self.get(endpoint, params=query_params)
response.raise_for_status()
return response.json()["results"]

def list_objecttypes(
self,
page: int | None = None,
page_size: int | None = None,
) -> list[dict[str, Any]]:
return self._get_paginated(
"objecttypes",
page=page,
page_size=page_size,
)

def list_objecttype_versions(
self,
objecttype_uuid: str | UUID,
page: int | None = None,
page_size: int | None = None,
) -> list[dict[str, Any]]:
return self._get_paginated(
f"objecttypes/{objecttype_uuid}/versions", page=page, page_size=page_size
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
interactions:
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate, br
Authorization:
- Token 171be5abaf41e7856b423ad513df1ef8f867ff48
sergei-maertens marked this conversation as resolved.
Show resolved Hide resolved
Connection:
- keep-alive
User-Agent:
- python-requests/2.31.0
method: GET
uri: http://localhost:8001/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48/versions
response:
body:
string: '{"count":3,"next":null,"previous":null,"results":[{"url":"http://localhost:8001/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48/versions/3","version":3,"objectType":"http://localhost:8001/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48","status":"draft","jsonSchema":{"$id":"https://example.com/person.schema.json","type":"object","title":"Person","$schema":"https://json-schema.org/draft/2020-12/schema","properties":{"age":{"type":"integer","minimum":18,"description":"Age
in years which must be equal to or greater than 18."},"country":{"type":"string","description":"The
person''s current country."},"lastName":{"type":"string","description":"The
person''s last name."},"firstName":{"type":"string","description":"The person''s
first name."}}},"createdAt":"2024-02-08","modifiedAt":"2024-02-08","publishedAt":"2024-02-08"},{"url":"http://localhost:8001/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48/versions/2","version":2,"objectType":"http://localhost:8001/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48","status":"published","jsonSchema":{"$id":"https://example.com/person.schema.json","type":"object","title":"Person","$schema":"https://json-schema.org/draft/2020-12/schema","properties":{"age":{"type":"integer","minimum":18,"description":"Age
in years which must be equal to or greater than 18."},"lastName":{"type":"string","description":"The
person''s last name."},"firstName":{"type":"string","description":"The person''s
first name."}}},"createdAt":"2024-02-08","modifiedAt":"2024-02-08","publishedAt":"2024-02-08"},{"url":"http://localhost:8001/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48/versions/1","version":1,"objectType":"http://localhost:8001/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48","status":"published","jsonSchema":{"$id":"https://example.com/person.schema.json","type":"object","title":"Person","$schema":"https://json-schema.org/draft/2020-12/schema","properties":{"age":{"type":"integer","minimum":0,"description":"Age
in years which must be equal to or greater than zero."},"lastName":{"type":"string","description":"The
person''s last name."},"firstName":{"type":"string","description":"The person''s
first name."}}},"createdAt":"2023-10-24","modifiedAt":"2024-02-08","publishedAt":"2024-02-08"}]}'
headers:
Allow:
- GET, POST, HEAD, OPTIONS
Content-Length:
- '2282'
Content-Type:
- application/json
Referrer-Policy:
- same-origin
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
status:
code: 200
message: OK
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
interactions:
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate, br
Authorization:
- Token 171be5abaf41e7856b423ad513df1ef8f867ff48
Connection:
- keep-alive
User-Agent:
- python-requests/2.31.0
method: GET
uri: http://localhost:8001/api/v2/objecttypes
response:
body:
string: '{"count":2,"next":null,"previous":null,"results":[{"url":"http://localhost:8001/api/v2/objecttypes/3edfdaf7-f469-470b-a391-bb7ea015bd6f","uuid":"3edfdaf7-f469-470b-a391-bb7ea015bd6f","name":"Tree","namePlural":"Trees","description":"","dataClassification":"confidential","maintainerOrganization":"","maintainerDepartment":"","contactPerson":"","contactEmail":"","source":"","updateFrequency":"unknown","providerOrganization":"","documentationUrl":"","labels":{},"createdAt":"2024-02-08","modifiedAt":"2024-02-08","allowGeometry":true,"versions":["http://localhost:8001/api/v2/objecttypes/3edfdaf7-f469-470b-a391-bb7ea015bd6f/versions/1"]},{"url":"http://localhost:8001/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48","uuid":"8e46e0a5-b1b4-449b-b9e9-fa3cea655f48","name":"Person","namePlural":"Persons","description":"","dataClassification":"open","maintainerOrganization":"","maintainerDepartment":"","contactPerson":"","contactEmail":"","source":"","updateFrequency":"unknown","providerOrganization":"","documentationUrl":"","labels":{},"createdAt":"2023-10-24","modifiedAt":"2024-02-08","allowGeometry":true,"versions":["http://localhost:8001/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48/versions/1","http://localhost:8001/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48/versions/2","http://localhost:8001/api/v2/objecttypes/8e46e0a5-b1b4-449b-b9e9-fa3cea655f48/versions/3"]}]}'
headers:
Allow:
- GET, POST, HEAD, OPTIONS
Content-Length:
- '1407'
Content-Type:
- application/json
Referrer-Policy:
- same-origin
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
status:
code: 200
message: OK
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
interactions:
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate, br
Authorization:
- Token 171be5abaf41e7856b423ad513df1ef8f867ff48
Connection:
- keep-alive
User-Agent:
- python-requests/2.31.0
method: GET
uri: http://localhost:8001/api/v2/objecttypes?page=1&pageSize=1
response:
body:
string: '{"count":2,"next":"http://localhost:8001/api/v2/objecttypes?page=2&pageSize=1","previous":null,"results":[{"url":"http://localhost:8001/api/v2/objecttypes/3edfdaf7-f469-470b-a391-bb7ea015bd6f","uuid":"3edfdaf7-f469-470b-a391-bb7ea015bd6f","name":"Tree","namePlural":"Trees","description":"","dataClassification":"confidential","maintainerOrganization":"","maintainerDepartment":"","contactPerson":"","contactEmail":"","source":"","updateFrequency":"unknown","providerOrganization":"","documentationUrl":"","labels":{},"createdAt":"2024-02-08","modifiedAt":"2024-02-08","allowGeometry":true,"versions":["http://localhost:8001/api/v2/objecttypes/3edfdaf7-f469-470b-a391-bb7ea015bd6f/versions/1"]}]}'
headers:
Allow:
- GET, POST, HEAD, OPTIONS
Content-Length:
- '696'
Content-Type:
- application/json
Referrer-Policy:
- same-origin
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
status:
code: 200
message: OK
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from pathlib import Path
from unittest.mock import patch

from django.test import TestCase

from zgw_consumers.constants import APITypes, AuthTypes
from zgw_consumers.models import Service

from openforms.utils.tests.vcr import OFVCRMixin

from ..client import get_objecttypes_client
from ..models import ObjectsAPIConfig


def get_test_config() -> ObjectsAPIConfig:
"""Returns a preconfigured ``ObjectsAPIConfig`` instance matching the docker compose configuration."""
return ObjectsAPIConfig(
objecttypes_service=Service(
api_root="http://localhost:8001/api/v2/",
api_type=APITypes.orc,
oas="https://example.com/",
header_key="Authorization",
header_value="Token 171be5abaf41e7856b423ad513df1ef8f867ff48",
auth_type=AuthTypes.api_key,
)
)


class ObjecttypesClientTest(OFVCRMixin, TestCase):

VCR_TEST_FILES = Path(__file__).parent / "files"

def setUp(self) -> None:
super().setUp()

patcher = patch(
"openforms.registrations.contrib.objects_api.client.ObjectsAPIConfig.get_solo",
return_value=get_test_config(),
)

self.config_mock = patcher.start()
self.addCleanup(patcher.stop)

def test_list_objecttypes(self):
with get_objecttypes_client() as client:
data = client.list_objecttypes()

self.assertEqual(len(data), 2)

def test_list_objectypes_pagination(self):
with get_objecttypes_client() as client:
data = client.list_objecttypes(page=1, page_size=1)

self.assertEqual(len(data), 1)

def test_list_objecttype_version(self):
with get_objecttypes_client() as client:
data = client.list_objecttype_versions(
"8e46e0a5-b1b4-449b-b9e9-fa3cea655f48"
)

self.assertEqual(len(data), 3)
Loading