diff --git a/lib/ingestor-api/runtime/src/collection.py b/lib/ingestor-api/runtime/src/collection.py index 7e6c2a5..41665e5 100644 --- a/lib/ingestor-api/runtime/src/collection.py +++ b/lib/ingestor-api/runtime/src/collection.py @@ -14,16 +14,13 @@ def ingest(collection: StacCollection): does necessary preprocessing, and loads into the PgSTAC collection table """ - try: - creds = get_db_credentials(os.environ["DB_SECRET_ARN"]) - with PgstacDB(dsn=creds.dsn_string, debug=True) as db: - loader = Loader(db=db) - collection = [ - collection.to_dict() - ] # pypgstac wants either a string or an Iterable of dicts. - loader.load_collections(file=collection, insert_mode=Methods.upsert) - except Exception as e: - print(f"Encountered failure loading collection into pgSTAC: {e}") + creds = get_db_credentials(os.environ["DB_SECRET_ARN"]) + with PgstacDB(dsn=creds.dsn_string, debug=True) as db: + loader = Loader(db=db) + collection = [ + collection.to_dict() + ] # pypgstac wants either a string or an Iterable of dicts. + loader.load_collections(file=collection, insert_mode=Methods.upsert) def delete(collection_id: str): diff --git a/lib/ingestor-api/runtime/src/main.py b/lib/ingestor-api/runtime/src/main.py index 8ab4678..fe5d379 100644 --- a/lib/ingestor-api/runtime/src/main.py +++ b/lib/ingestor-api/runtime/src/main.py @@ -88,33 +88,22 @@ def cancel_ingestion( @app.post( "/collections", tags=["Collection"], - status_code=201, dependencies=[Depends(dependencies.get_username)], + status_code=201, ) -def publish_collection(collection: schemas.StacCollection): - # pgstac create collection - try: - collection_loader.ingest(collection) - return {f"Successfully published: {collection.id}"} - except Exception as e: - raise HTTPException( - status_code=400, - detail=(f"Unable to publish collection: {e}"), - ) +def publish_collection(collection: schemas.StacCollection) -> schemas.StacCollection: + collection_loader.ingest(collection) + return collection @app.delete( "/collections/{collection_id}", tags=["Collection"], dependencies=[Depends(dependencies.get_username)], + status_code=204, ) def delete_collection(collection_id: str): - try: - collection_loader.delete(collection_id=collection_id) - return {f"Successfully deleted: {collection_id}"} - except Exception as e: - print(e) - raise HTTPException(status_code=400, detail=(f"{e}")) + collection_loader.delete(collection_id=collection_id) @app.get("/auth/me") diff --git a/lib/ingestor-api/runtime/src/utils.py b/lib/ingestor-api/runtime/src/utils.py index c0cbb1f..34d2cc8 100644 --- a/lib/ingestor-api/runtime/src/utils.py +++ b/lib/ingestor-api/runtime/src/utils.py @@ -2,9 +2,9 @@ import boto3 import pydantic +from fastapi.encoders import jsonable_encoder from pypgstac.db import PgstacDB from pypgstac.load import Methods -from fastapi.encoders import jsonable_encoder from .loader import Loader from .schemas import Ingestion diff --git a/lib/ingestor-api/runtime/tests/test_collection.py b/lib/ingestor-api/runtime/tests/test_collection.py index 6141342..b0ce9ca 100644 --- a/lib/ingestor-api/runtime/tests/test_collection.py +++ b/lib/ingestor-api/runtime/tests/test_collection.py @@ -1,9 +1,10 @@ +import os from unittest.mock import Mock, patch + import pytest +import src.collection as collection from pypgstac.load import Methods from src.utils import DbCreds -import src.collection as collection -import os @pytest.fixture() diff --git a/lib/ingestor-api/runtime/tests/test_collection_endpoint.py b/lib/ingestor-api/runtime/tests/test_collection_endpoint.py index f9d4a15..f282428 100644 --- a/lib/ingestor-api/runtime/tests/test_collection_endpoint.py +++ b/lib/ingestor-api/runtime/tests/test_collection_endpoint.py @@ -31,7 +31,7 @@ def test_auth_delete_collection(delete, example_stac_collection, client_authenti headers={"Authorization": f"bearer {token}"}, ) delete.assert_called_once_with(collection_id=example_stac_collection["id"]) - assert response.status_code == 200 + assert response.status_code == 204 def test_unauth_delete_collection(client, example_stac_collection): diff --git a/lib/ingestor-api/runtime/tests/test_registration.py b/lib/ingestor-api/runtime/tests/test_registration.py index 55721de..f83cbe0 100644 --- a/lib/ingestor-api/runtime/tests/test_registration.py +++ b/lib/ingestor-api/runtime/tests/test_registration.py @@ -4,9 +4,8 @@ from typing import TYPE_CHECKING, List from unittest.mock import call, patch -from fastapi.encoders import jsonable_encoder import pytest - +from fastapi.encoders import jsonable_encoder if TYPE_CHECKING: from fastapi.testclient import TestClient diff --git a/lib/ingestor-api/runtime/tests/test_utils.py b/lib/ingestor-api/runtime/tests/test_utils.py index f03bb1d..cdba105 100644 --- a/lib/ingestor-api/runtime/tests/test_utils.py +++ b/lib/ingestor-api/runtime/tests/test_utils.py @@ -1,8 +1,8 @@ from unittest.mock import Mock, patch import pytest -from pypgstac.load import Methods from fastapi.encoders import jsonable_encoder +from pypgstac.load import Methods from src.utils import DbCreds