From e98712376c5f339e27e9782f6aba09edab467cee Mon Sep 17 00:00:00 2001 From: dcleres Date: Tue, 15 Oct 2024 17:40:33 +0200 Subject: [PATCH 1/4] Close #LGVISIUM-94: Renamed API create PNG path output --- src/app/api/v1/endpoints/create_pngs.py | 8 ++++---- src/app/common/schemas.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/app/api/v1/endpoints/create_pngs.py b/src/app/api/v1/endpoints/create_pngs.py index 6808b59a..9007956f 100644 --- a/src/app/api/v1/endpoints/create_pngs.py +++ b/src/app/api/v1/endpoints/create_pngs.py @@ -9,7 +9,7 @@ from fastapi import HTTPException -def create_pngs(aws_filename: Path): +def create_pngs(aws_filename: Path) -> PNGResponse: """Convert a PDF document to PNG images. Please note that this function will overwrite any existing PNG files. Args: @@ -28,7 +28,7 @@ def create_pngs(aws_filename: Path): # Initialize the S3 client pdf_document = load_pdf_from_aws(aws_filename) - png_urls = [] + s3_keys = [] # Convert each page of the PDF to PNG try: @@ -48,11 +48,11 @@ def create_pngs(aws_filename: Path): ) # Generate the S3 URL - png_urls.append(s3_bucket_png_path) + s3_keys.append(s3_bucket_png_path) # Clean up the local file os.remove(png_path) except Exception: raise HTTPException(status_code=500, detail="An error occurred while processing the PDF.") from None - return PNGResponse(png_urls=png_urls) + return PNGResponse(key=s3_keys) diff --git a/src/app/common/schemas.py b/src/app/common/schemas.py index e53bc6f6..fdd6cb8b 100644 --- a/src/app/common/schemas.py +++ b/src/app/common/schemas.py @@ -42,7 +42,7 @@ class Config: class PNGResponse(BaseModel): """Response schema for the create_pngs endpoint.""" - png_urls: list[str] + key: list[str] # key in the S3 bucket ######################################################################################################################## From 5b4acab35b8ffaa9dd66844851ccb09fcbe618a0 Mon Sep 17 00:00:00 2001 From: dcleres Date: Tue, 15 Oct 2024 17:48:21 +0200 Subject: [PATCH 2/4] Minor edits after PR --- src/app/api/v1/endpoints/create_pngs.py | 4 ++-- tests/test_create_pngs.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/app/api/v1/endpoints/create_pngs.py b/src/app/api/v1/endpoints/create_pngs.py index 9007956f..89e272c3 100644 --- a/src/app/api/v1/endpoints/create_pngs.py +++ b/src/app/api/v1/endpoints/create_pngs.py @@ -16,7 +16,7 @@ def create_pngs(aws_filename: Path) -> PNGResponse: aws_filename (str): The key of the PDF document in the S3 bucket. For example, "10012.pdf". Returns: - PNGResponse: The URLs of the PNG images in the S3 bucket. + PNGResponse: The S3 keys of the PNG images in the S3 bucket. """ # Check if the PDF name is valid if not aws_filename.suffix == ".pdf": @@ -47,7 +47,7 @@ def create_pngs(aws_filename: Path) -> PNGResponse: s3_bucket_png_path, # The key (name) of the file in the bucket ) - # Generate the S3 URL + # Generate the S3 key s3_keys.append(s3_bucket_png_path) # Clean up the local file diff --git a/tests/test_create_pngs.py b/tests/test_create_pngs.py index 37efbf60..08cebd30 100644 --- a/tests/test_create_pngs.py +++ b/tests/test_create_pngs.py @@ -48,11 +48,11 @@ def test_create_pngs_success(test_client: TestClient, s3_client, upload_test_pdf response = test_client.post("/api/V1/create_pngs", json={"filename": TEST_PDF_KEY.rsplit("/", 1)[-1]}) assert response.status_code == 200 json_response = response.json() - assert "png_urls" in json_response - assert len(json_response["png_urls"]) > 0 + assert "key" in json_response + assert len(json_response["key"]) > 0 # Verify that PNG files are uploaded to S3 - for png_url in json_response["png_urls"]: + for png_url in json_response["key"]: try: s3_client.head_object(Bucket=config.test_bucket_name, Key=png_url) except ClientError: From a9573310391f3c3c09a60817ffedb4779de6bd6a Mon Sep 17 00:00:00 2001 From: dcleres Date: Fri, 25 Oct 2024 09:22:32 +0200 Subject: [PATCH 3/4] Updates key to keys as suggested in the PR --- src/app/api/v1/endpoints/create_pngs.py | 2 +- src/app/common/schemas.py | 2 +- tests/test_create_pngs.py | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/app/api/v1/endpoints/create_pngs.py b/src/app/api/v1/endpoints/create_pngs.py index 89e272c3..8ce1389d 100644 --- a/src/app/api/v1/endpoints/create_pngs.py +++ b/src/app/api/v1/endpoints/create_pngs.py @@ -55,4 +55,4 @@ def create_pngs(aws_filename: Path) -> PNGResponse: except Exception: raise HTTPException(status_code=500, detail="An error occurred while processing the PDF.") from None - return PNGResponse(key=s3_keys) + return PNGResponse(keys=s3_keys) diff --git a/src/app/common/schemas.py b/src/app/common/schemas.py index fdd6cb8b..6467882d 100644 --- a/src/app/common/schemas.py +++ b/src/app/common/schemas.py @@ -42,7 +42,7 @@ class Config: class PNGResponse(BaseModel): """Response schema for the create_pngs endpoint.""" - key: list[str] # key in the S3 bucket + keys: list[str] # keys in the S3 bucket ######################################################################################################################## diff --git a/tests/test_create_pngs.py b/tests/test_create_pngs.py index 08cebd30..e064f588 100644 --- a/tests/test_create_pngs.py +++ b/tests/test_create_pngs.py @@ -48,11 +48,11 @@ def test_create_pngs_success(test_client: TestClient, s3_client, upload_test_pdf response = test_client.post("/api/V1/create_pngs", json={"filename": TEST_PDF_KEY.rsplit("/", 1)[-1]}) assert response.status_code == 200 json_response = response.json() - assert "key" in json_response - assert len(json_response["key"]) > 0 + assert "keys" in json_response + assert len(json_response["keys"]) > 0 # Verify that PNG files are uploaded to S3 - for png_url in json_response["key"]: + for png_url in json_response["keys"]: try: s3_client.head_object(Bucket=config.test_bucket_name, Key=png_url) except ClientError: From c62f834a25f77882e3e0815475ee4383d2657050 Mon Sep 17 00:00:00 2001 From: dcleres Date: Fri, 25 Oct 2024 09:25:43 +0200 Subject: [PATCH 4/4] Adjusted comment in the code --- src/app/api/v1/endpoints/create_pngs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/api/v1/endpoints/create_pngs.py b/src/app/api/v1/endpoints/create_pngs.py index 8ce1389d..534bdf9e 100644 --- a/src/app/api/v1/endpoints/create_pngs.py +++ b/src/app/api/v1/endpoints/create_pngs.py @@ -47,7 +47,7 @@ def create_pngs(aws_filename: Path) -> PNGResponse: s3_bucket_png_path, # The key (name) of the file in the bucket ) - # Generate the S3 key + # Save the generated the S3 keys as a list to return through the API s3_keys.append(s3_bucket_png_path) # Clean up the local file