-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from viaacode/adding-field-resources
Adding field resources
- Loading branch information
Showing
4 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
setup( | ||
name="mediahaven", | ||
version="0.1.2", | ||
version="0.2.0", | ||
license="GPL", | ||
author="Mattias", | ||
author_email="[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |