Skip to content

Commit

Permalink
Merge pull request #2 from viaacode/adding-field-resources
Browse files Browse the repository at this point in the history
Adding field resources
  • Loading branch information
lennertvandevelde authored Oct 4, 2022
2 parents 40cd781 + 9e94245 commit a112906
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
2 changes: 2 additions & 0 deletions mediahaven/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

# Records
from mediahaven.resources.records import Records
from mediahaven.resources.field_definitions import FieldDefinitions


class MediaHaven(MediaHavenClient):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.records = Records(self)
self.fields = FieldDefinitions(self)
63 changes: 63 additions & 0 deletions mediahaven/resources/field_definitions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from mediahaven.mediahaven import DEFAULT_ACCEPT_FORMAT
from mediahaven.resources.base_resource import (
BaseResource,
MediaHavenPageObject,
MediaHavenPageObjectCreator,
MediaHavenSingleObject,
MediaHavenSingleObjectCreator,
)


class FieldDefinitions(BaseResource):
"""Public API endpoint of MediaHaven field definitions."""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._name = "field-definitions"

def get(
self, field: str=None, accept_format=DEFAULT_ACCEPT_FORMAT,
) -> MediaHavenSingleObject:
"""Get a single field definition.
Args:
field: The id or FlatKey of a metadata field definition.
accept_format: The "Accept" request header.
Returns:
A single metadata field definition.
"""
response = self.mh_client._get(
self._construct_path(field),
accept_format,
)
return MediaHavenSingleObjectCreator.create_object(response, accept_format)

def search(
self, accept_format: str = DEFAULT_ACCEPT_FORMAT, **query_params
) -> MediaHavenPageObject:
"""Search all field definitions.
Args:
accept_format: The "Accept" request header.
**query_params: The optional query paramaters:
query_params["startIndex"]: Used for pagination of search results,
search results will be returned starting from this index.
query_params["nrOfResults"]: the number of results that will be returned
query_params["nested"]: If true include children and parents in the response,
defualt is false.
query_params["sort"]: Determine how to sort the field definitions. (FieldDefinitionId or LongTranslation)
Returns:
A paged result with the metadata field definitions.
"""
response = self.mh_client._get(
self._construct_path(),
accept_format,
**query_params,
)
return MediaHavenPageObjectCreator.create_object(
response, accept_format, self, **query_params
)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="mediahaven",
version="0.1.2",
version="0.2.0",
license="GPL",
author="Mattias",
author_email="[email protected]",
Expand Down
43 changes: 43 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pytest
from unittest.mock import patch

from mediahaven.resources.base_resource import AcceptFormat
from mediahaven.resources.field_definitions import FieldDefinitions

class TestFields:
@pytest.fixture()
def field_definitions(self, mh_client_mock):
return FieldDefinitions(mh_client_mock)

@patch("mediahaven.resources.field_definitions.MediaHavenSingleObjectCreator")
def test_get(self, object_creator_mock, field_definitions: FieldDefinitions):
# Arrange
field_flat_key = "Title"
mh_client_mock = field_definitions.mh_client

# Act
field_definitions.get(field_flat_key)

# Assert
mh_client_mock._get.assert_called_once_with(
f"{field_definitions.name}/{field_flat_key}", AcceptFormat.JSON
)
object_creator_mock.create_object.assert_called_once_with(
mh_client_mock._get(), AcceptFormat.JSON
)

@patch("mediahaven.resources.field_definitions.MediaHavenPageObjectCreator")
def test_search(self, object_creator_mock, field_definitions: FieldDefinitions):
# Arrange
mh_client_mock = field_definitions.mh_client

# Act
field_definitions.search()

# Assert
mh_client_mock._get.assert_called_once_with(
field_definitions.name, AcceptFormat.JSON
)
object_creator_mock.create_object.assert_called_once_with(
mh_client_mock._get(), AcceptFormat.JSON, field_definitions
)

0 comments on commit a112906

Please sign in to comment.