From 72374c93f6c32025d79332d01483d6751765ce11 Mon Sep 17 00:00:00 2001 From: Anikesh Suresh Date: Wed, 4 Dec 2024 11:41:55 +0000 Subject: [PATCH] Implement remaining PR Review changes #37 --- object_storage_api/core/exceptions.py | 16 +--------------- object_storage_api/repositories/image.py | 7 ++++--- object_storage_api/routers/image.py | 3 ++- object_storage_api/services/image.py | 2 +- test/unit/repositories/test_image.py | 2 +- 5 files changed, 9 insertions(+), 21 deletions(-) diff --git a/object_storage_api/core/exceptions.py b/object_storage_api/core/exceptions.py index 6aca36c..67d878b 100644 --- a/object_storage_api/core/exceptions.py +++ b/object_storage_api/core/exceptions.py @@ -49,23 +49,9 @@ class InvalidObjectIdError(DatabaseError): The provided value is not a valid ObjectId. """ - status_code = 404 + status_code = 422 response_detail = "Invalid ID given" - def __init__(self, detail: str, response_detail: Optional[str] = None, entity_name: Optional[str] = None): - """ - Initialise the exception. - - :param detail: Specific detail of the exception (just like Exception would take - this will only be logged - and not returned in a response). - :param response_detail: Generic detail of the exception to be returned in the response. - :param entity_name: Name of the entity to include in the response detail. - """ - super().__init__(detail, response_detail) - - if entity_name is not None: - self.response_detail = f"{entity_name.capitalize()} not found" - class InvalidImageFileError(BaseAPIException): """ diff --git a/object_storage_api/repositories/image.py b/object_storage_api/repositories/image.py index 5c434c4..c01c3b6 100644 --- a/object_storage_api/repositories/image.py +++ b/object_storage_api/repositories/image.py @@ -54,15 +54,16 @@ def get(self, image_id: str, session: ClientSession = None) -> ImageOut: :raises InvalidObjectIdError: If the supplied `image_id` is invalid. """ logger.info("Retrieving image with ID: %s from the database", image_id) - entity_name = "image" try: image_id = CustomObjectId(image_id) image = self._images_collection.find_one({"_id": image_id}, session=session) except InvalidObjectIdError as exc: - raise InvalidObjectIdError(detail=f"Invalid ObjectId value '{image_id}'", entity_name=entity_name) from exc + exc.status_code = 404 + exc.response_detail = "Image not found" + raise exc if image: return ImageOut(**image) - raise MissingRecordError(detail=f"No image found with ID: {image_id}", entity_name=entity_name) + raise MissingRecordError(detail=f"No image found with ID: {image_id}", entity_name="image") def list(self, entity_id: Optional[str], primary: Optional[bool], session: ClientSession = None) -> list[ImageOut]: """ diff --git a/object_storage_api/routers/image.py b/object_storage_api/routers/image.py index 9e90d69..73dcca9 100644 --- a/object_storage_api/routers/image.py +++ b/object_storage_api/routers/image.py @@ -71,7 +71,8 @@ def get_images( @router.get(path="/{image_id}", summary="Get an image by ID", response_description="Single image") def get_image( - image_id: Annotated[str, Path(description="ID of the image to get")], image_service: ImageServiceDep + image_id: Annotated[str, Path(description="ID of the image to get")], + image_service: ImageServiceDep, ) -> ImageSchema: # pylint: disable=missing-function-docstring logger.info("Getting image with ID: %s", image_id) diff --git a/object_storage_api/services/image.py b/object_storage_api/services/image.py index d186d91..1157510 100644 --- a/object_storage_api/services/image.py +++ b/object_storage_api/services/image.py @@ -80,7 +80,7 @@ def get(self, image_id: str) -> ImageSchema: Retrieve an image's metadata with its presigned get url by its ID. :param image_id: ID of the image to retrieve. - :return: An image's metadata with a presigned get url if it is obtained. + :return: An image's metadata with a presigned get url. """ image = self._image_repository.get(image_id=image_id) presigned_url = self._image_store.create_presigned_get(image) diff --git a/test/unit/repositories/test_image.py b/test/unit/repositories/test_image.py index 43810fe..15e6a75 100644 --- a/test/unit/repositories/test_image.py +++ b/test/unit/repositories/test_image.py @@ -180,7 +180,7 @@ def test_get_with_non_existent_id(self): self.mock_get(image_id, None) self.call_get_expecting_error(image_id, MissingRecordError) - self.check_get_failed_with_exception(f"Image with image_id {image_id} was not found.", True) + self.check_get_failed_with_exception(f"No image found with ID: {image_id}", True) def test_get_with_invalid_id(self): """Test getting an image with an invalid image ID."""