From bcd43d656649767efe5137652bf7721df3abf136 Mon Sep 17 00:00:00 2001 From: dyf Date: Mon, 5 Feb 2024 13:34:10 -0800 Subject: [PATCH 1/3] lint --- doc_template/source/conf.py | 1 + src/aind_codeocean_api/__init__.py | 1 + src/aind_codeocean_api/codeocean.py | 48 +++++++++++++++++++++++++++ src/aind_codeocean_api/credentials.py | 1 + tests/test_codeocean_requests.py | 1 + tests/test_credentials.py | 1 + 6 files changed, 53 insertions(+) diff --git a/doc_template/source/conf.py b/doc_template/source/conf.py index 230d761..16aa411 100644 --- a/doc_template/source/conf.py +++ b/doc_template/source/conf.py @@ -1,4 +1,5 @@ """Configuration file for the Sphinx documentation builder.""" + # # For the full list of built-in configuration values, see the documentation: # https://www.sphinx-doc.org/en/master/usage/configuration.html diff --git a/src/aind_codeocean_api/__init__.py b/src/aind_codeocean_api/__init__.py index dc06f2a..4126d57 100644 --- a/src/aind_codeocean_api/__init__.py +++ b/src/aind_codeocean_api/__init__.py @@ -1,2 +1,3 @@ """Python wrapper of CodeOcean's REST API""" + __version__ = "0.4.1" diff --git a/src/aind_codeocean_api/codeocean.py b/src/aind_codeocean_api/codeocean.py index 35e0e0d..6b441c8 100644 --- a/src/aind_codeocean_api/codeocean.py +++ b/src/aind_codeocean_api/codeocean.py @@ -1,5 +1,6 @@ """Module to interface with Code Ocean's backend. """ + import json import logging from enum import Enum @@ -23,6 +24,7 @@ class CodeOceanClient: class _URLStrings(Enum): """Enum class for CodeOcean's url strings""" + ARCHIVE = "archive" CAPSULES = "capsules" COMPUTATIONS = "computations" DATA_ASSETS = "data_assets" @@ -553,3 +555,49 @@ def update_permissions( ) response = requests.post(url, json=permissions, auth=(self.token, "")) return response + + def archive_data_asset( + self, data_asset_id: str, archive: bool = True + ) -> requests.models.Response: + """ + This will archive or unarchive a data asset using a PATCH request to the Code Ocean API. + + Parameters + --------------- + data_asset_id : string + ID of the data asset + + Returns + --------------- + requests.models.Response + """ + + url = ( + f"{self.asset_url}/{data_asset_id}/" + f"{self._URLStrings.ARCHIVE.value}" + ) + response = requests.patch( + url, params={"archive": archive}, auth=(self.token, "") + ) + return response + + def delete_data_asset( + self, data_asset_id: str + ) -> requests.models.Response: + """ + This will delete a data asset using a DELETE request to the Code Ocean API. + + Parameters + --------------- + data_asset_id : string + ID of the data asset + + Returns + --------------- + requests.models.Response + """ + + url = f"{self.asset_url}/{data_asset_id}" + + response = requests.delete(url, auth=(self.token, "")) + return response diff --git a/src/aind_codeocean_api/credentials.py b/src/aind_codeocean_api/credentials.py index f0a90c5..dd8fbb7 100644 --- a/src/aind_codeocean_api/credentials.py +++ b/src/aind_codeocean_api/credentials.py @@ -1,4 +1,5 @@ """Basic CodeOcean Credentials Handling.""" + import functools import json import os diff --git a/tests/test_codeocean_requests.py b/tests/test_codeocean_requests.py index 8e1ac10..6df3869 100644 --- a/tests/test_codeocean_requests.py +++ b/tests/test_codeocean_requests.py @@ -1,4 +1,5 @@ """Tests CodeOcean API python interface""" + import json import unittest from typing import Any, Callable, List diff --git a/tests/test_credentials.py b/tests/test_credentials.py index 741a203..cb02484 100644 --- a/tests/test_credentials.py +++ b/tests/test_credentials.py @@ -1,4 +1,5 @@ """Tests credentials loader.""" + import json import os import unittest From 3b942016be8855a0e5a2ecbede693ff454c3c0af Mon Sep 17 00:00:00 2001 From: dyf Date: Mon, 5 Feb 2024 17:10:42 -0800 Subject: [PATCH 2/3] tests --- tests/test_codeocean_requests.py | 70 ++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/test_codeocean_requests.py b/tests/test_codeocean_requests.py index 6df3869..dd0b0b2 100644 --- a/tests/test_codeocean_requests.py +++ b/tests/test_codeocean_requests.py @@ -86,11 +86,29 @@ def request_put_response(url: str, json: dict) -> MockResponse: status_code=200, content=success_message, url=url ) + def request_patch_response(url: str) -> MockResponse: + """Mock a patch response""" + success_message = map_input_to_success_message(url) + return MockResponse( + status_code=202, content=success_message, url=url + ) + + def request_delete_response(url: str) -> MockResponse: + """Mock a delete response""" + success_message = map_input_to_success_message(url) + return MockResponse( + status_code=204, content=success_message, url=url + ) + # TODO: Change these to enums if req_type == "post": return request_post_response elif req_type == "get": return request_get_response + elif req_type == "patch": + return request_patch_response + elif req_type == "delete": + return request_delete_response else: return request_put_response @@ -991,6 +1009,58 @@ def request_post_response(json: dict) -> MockResponse: ) self.assertEqual(response.status_code, 204) + @mock.patch("requests.patch") + def test_archive_data_asset( + self, mock_api_patch: unittest.mock.MagicMock + ) -> None: + """Tests the response of archiving a data asset""" + + def map_to_success_message(_) -> dict: + """Map to a success message""" + return "" + + mocked_success_patch = self.mock_success_response( + map_to_success_message, req_type="patch" + ) + + example_data_asset_id = "da8dd108-2a10-471d-82b9-1e671b107bf8" + expected_url = f"{self.co_client.asset_url}/{example_data_asset_id}/archive?archive=True" + + mock_api_patch.return_value = mocked_success_patch(url=expected_url) + + response = self.co_client.archive_data_asset( + data_asset_id=example_data_asset_id, archive=True + ) + + self.assertEqual(response.url, expected_url) + self.assertEqual(response.status_code, 202) + + @mock.patch("requests.delete") + def test_delete_data_asset( + self, mock_api_delete: unittest.mock.MagicMock + ) -> None: + """Tests the response of deleting a data asset""" + + def map_to_success_message(_) -> dict: + """Map to a success message""" + return "" + + mocked_success_delete = self.mock_success_response( + map_to_success_message, req_type="delete" + ) + + example_data_asset_id = "da8dd108-2a10-471d-82b9-1e671b107bf8" + expected_url = f"{self.co_client.asset_url}/{example_data_asset_id}" + + mock_api_delete.return_value = mocked_success_delete(url=expected_url) + + response = self.co_client.delete_data_asset( + data_asset_id=example_data_asset_id + ) + + self.assertEqual(response.url, expected_url) + self.assertEqual(response.status_code, 204) + if __name__ == "__main__": unittest.main() From 74cf944bf996f4859e4dfc33199ab88ab7610a6c Mon Sep 17 00:00:00 2001 From: dyf Date: Mon, 5 Feb 2024 17:15:20 -0800 Subject: [PATCH 3/3] line length --- src/aind_codeocean_api/codeocean.py | 6 ++++-- tests/test_codeocean_requests.py | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/aind_codeocean_api/codeocean.py b/src/aind_codeocean_api/codeocean.py index 6b441c8..761d6a8 100644 --- a/src/aind_codeocean_api/codeocean.py +++ b/src/aind_codeocean_api/codeocean.py @@ -560,7 +560,8 @@ def archive_data_asset( self, data_asset_id: str, archive: bool = True ) -> requests.models.Response: """ - This will archive or unarchive a data asset using a PATCH request to the Code Ocean API. + This will archive or unarchive a data asset using a PATCH request to + the Code Ocean API. Parameters --------------- @@ -585,7 +586,8 @@ def delete_data_asset( self, data_asset_id: str ) -> requests.models.Response: """ - This will delete a data asset using a DELETE request to the Code Ocean API. + This will delete a data asset using a DELETE request to the Code Ocean + API. Parameters --------------- diff --git a/tests/test_codeocean_requests.py b/tests/test_codeocean_requests.py index dd0b0b2..112d247 100644 --- a/tests/test_codeocean_requests.py +++ b/tests/test_codeocean_requests.py @@ -1024,7 +1024,10 @@ def map_to_success_message(_) -> dict: ) example_data_asset_id = "da8dd108-2a10-471d-82b9-1e671b107bf8" - expected_url = f"{self.co_client.asset_url}/{example_data_asset_id}/archive?archive=True" + expected_url = ( + f"{self.co_client.asset_url}/" + "{example_data_asset_id}/archive?archive=True" + ) mock_api_patch.return_value = mocked_success_patch(url=expected_url)