Skip to content

Commit

Permalink
Merge pull request #3 from viaacode/mhpy-8-organisations
Browse files Browse the repository at this point in the history
MHPY-8 Added organisations resource
  • Loading branch information
lennertvandevelde authored Oct 4, 2022
2 parents a112906 + bb2386f commit 700848d
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mediahaven/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
# Records
from mediahaven.resources.records import Records
from mediahaven.resources.field_definitions import FieldDefinitions
from mediahaven.resources.organisations import Organisations


class MediaHaven(MediaHavenClient):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.records = Records(self)
self.fields = FieldDefinitions(self)
self.organisations = Organisations(self)
58 changes: 58 additions & 0 deletions mediahaven/resources/organisations.py
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
)
43 changes: 43 additions & 0 deletions tests/test_organisations.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.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
)

0 comments on commit 700848d

Please sign in to comment.