diff --git a/meemoo/helpers.py b/meemoo/helpers.py index 9e518ae..607eec0 100644 --- a/meemoo/helpers.py +++ b/meemoo/helpers.py @@ -13,7 +13,6 @@ from io import BytesIO from ftplib import FTP as BuiltinFTP from urllib.parse import urlparse -import re # Third-party imports from viaa.configuration import ConfigParser @@ -236,5 +235,26 @@ def put(self, content_bytes, destination_path, destination_filename): raise e +def make_url(base_url, *path): + """Creates a valid url. + + Args: + base_url (string): The base url. + *path (variable length argument list, optional): + The path to be appended to the base url. + + Returns: + url (string): The created url. + + Raises a TypeError if base_url is None. + """ + + if base_url is None: + raise TypeError('base_url is required') + url = base_url.rstrip('/') # Remove optional trailing slash + for item in path: + url = f"{url}/{item}" + return url + # vim modeline # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/meemoo/services.py b/meemoo/services.py index 29dd597..4f15f50 100644 --- a/meemoo/services.py +++ b/meemoo/services.py @@ -23,6 +23,7 @@ from viaa.observability import logging # Local imports +from meemoo.helpers import make_url # Get logger config = ConfigParser() @@ -103,7 +104,8 @@ def get_organisation(self, or_id): # Make sure the OR is uppercase. Organisation api needs it. or_id = or_id[:2].upper() + or_id[2:] - response = requests.get(f"{self.host}org/{or_id}") + url = make_url(self.host, 'org', or_id) + response = requests.get(url) organisation = response.json()["data"] return organisation diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 1650e4d..b02e63f 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -26,7 +26,8 @@ normalize_or_id, is_event_valid, InvalidEventException, - get_destination_for_cp + get_destination_for_cp, + make_url ) from tests.resources import ( S3_MOCK_ESSENCE_EVENT, @@ -169,6 +170,7 @@ def test_is_event_valid_extra_fields(): # Assert assert event_valid is None + @pytest.mark.parametrize( "environment, cp, file_type, expected_destination", [ @@ -189,4 +191,57 @@ def test_file_destination(environment, cp, file_type, expected_destination): assert destination == expected_destination +def test_make_url_with_trailing_slash(): + # Arrange + base_url = 'https://example-url/' + # Act + url = make_url(base_url, 'org', 'test-org-id') + # Assert + assert url == 'https://example-url/org/test-org-id' + + +def test_make_url_multiple_trailing_slashes(): + # Arrange + base_url = 'https://example-url////' + # Act + url = make_url(base_url, 'org', 'test-org-id') + # Assert + assert url == 'https://example-url/org/test-org-id' + + +def test_make_url_missing_trailing_slash(): + # Arrange + base_url = 'https://example-url' + # Act + url = make_url(base_url, 'org', 'test-org-id') + # Assert + assert url == 'https://example-url/org/test-org-id' + + +def test_make_url_long_path(): + # Arrange + base_url = 'https://example-url' + # Act + url = make_url(base_url, 'api', 'deeply', 'nested', 'test', 'path') + # Assert + assert url == 'https://example-url/api/deeply/nested/test/path' + + +def test_make_url_none_base_url(): + # Arrange + base_url = None + # Act and assert + with pytest.raises(TypeError) as excinfo: + make_url(base_url, 'org', 'test-org-id') + assert 'base_url is required' in str(excinfo.value) + + +def test_make_url_no_path(): + # Arrange + base_url = 'https://example-url' + # Act + url = make_url(base_url) + # Assert + assert url == 'https://example-url' + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 smartindent