-
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 #3 from viaacode/mhpy-8-organisations
MHPY-8 Added organisations resource
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 deletions.
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,58 @@ | ||
#!/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 Organisations(BaseResource): | ||
"""Public API endpoint of MediaHaven tenants.""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self._name = "organisations" | ||
|
||
def get( | ||
self, organisation_id: str, accept_format=DEFAULT_ACCEPT_FORMAT, | ||
) -> MediaHavenSingleObject: | ||
"""Get a single organisation. | ||
Args: | ||
organisation_id: The id of an organisation. | ||
accept_format: The "Accept" request header. | ||
Returns: | ||
A single organisation. | ||
""" | ||
response = self.mh_client._get( | ||
self._construct_path(organisation_id), | ||
accept_format, | ||
) | ||
return MediaHavenSingleObjectCreator.create_object(response, accept_format) | ||
|
||
def search( | ||
self, accept_format: str = DEFAULT_ACCEPT_FORMAT, **query_params | ||
) -> MediaHavenPageObject: | ||
"""Search all organisations. | ||
Args: | ||
query: The search query. | ||
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 | ||
Returns: | ||
A paged result with the organisations. | ||
""" | ||
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import pytest | ||
from unittest.mock import patch | ||
|
||
from mediahaven.resources.base_resource import AcceptFormat | ||
from mediahaven.resources.organisations import Organisations | ||
|
||
class TestOrganisations: | ||
@pytest.fixture() | ||
def organisations(self, mh_client_mock): | ||
return Organisations(mh_client_mock) | ||
|
||
@patch("mediahaven.resources.organisations.MediaHavenSingleObjectCreator") | ||
def test_get(self, object_creator_mock, organisations: Organisations): | ||
# Arrange | ||
organisation_id = "1" | ||
mh_client_mock = organisations.mh_client | ||
|
||
# Act | ||
organisations.get(organisation_id) | ||
|
||
# Assert | ||
mh_client_mock._get.assert_called_once_with( | ||
f"{organisations.name}/{organisation_id}", AcceptFormat.JSON | ||
) | ||
object_creator_mock.create_object.assert_called_once_with( | ||
mh_client_mock._get(), AcceptFormat.JSON | ||
) | ||
|
||
@patch("mediahaven.resources.organisations.MediaHavenPageObjectCreator") | ||
def test_search(self, object_creator_mock, organisations: Organisations): | ||
# Arrange | ||
mh_client_mock = organisations.mh_client | ||
|
||
# Act | ||
organisations.search() | ||
|
||
# Assert | ||
mh_client_mock._get.assert_called_once_with( | ||
organisations.name, AcceptFormat.JSON | ||
) | ||
object_creator_mock.create_object.assert_called_once_with( | ||
mh_client_mock._get(), AcceptFormat.JSON, organisations | ||
) |