From 6dff7071e30ea4269f9215ac9d0e83bb02172c22 Mon Sep 17 00:00:00 2001 From: Daniel Mannarino Date: Thu, 16 Jan 2025 21:50:16 -0500 Subject: [PATCH] Fix dropping payload and post->get in find-by-ids, calc area functions; implement proxying add geostore route --- app/models/pydantic/geostore.py | 10 +++++++++- app/routes/geostore/geostore.py | 14 ++++++++++---- app/utils/rw_api.py | 23 +++++++++++++++++++++-- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/app/models/pydantic/geostore.py b/app/models/pydantic/geostore.py index 9edbd2c8..59c3c0be 100644 --- a/app/models/pydantic/geostore.py +++ b/app/models/pydantic/geostore.py @@ -51,6 +51,10 @@ class GeostoreIn(StrictBaseModel): geometry: Geometry +class RWGeostoreIn(StrictBaseModel): + geojson: Geometry | Feature | FeatureCollection + + class GeostoreResponse(Response): data: Geostore @@ -93,6 +97,10 @@ class AdminBoundaryInfo(StrictBaseModel): iso: str +class CreateGeostoreResponseInfo(StrictBaseModel): + use: Dict + + class FindByIDsInfo(StrictBaseModel): use: Dict iso: str @@ -131,7 +139,7 @@ class RWGeostoreAttributes(StrictBaseModel): areaHa: float bbox: List[float] lock: bool - info: AdminBoundaryInfo | FindByIDsInfo | LandUseInfo | WDPAInfo + info: AdminBoundaryInfo | CreateGeostoreResponseInfo | FindByIDsInfo | LandUseInfo | WDPAInfo class RWGeostore(StrictBaseModel): diff --git a/app/routes/geostore/geostore.py b/app/routes/geostore/geostore.py index 3fab0f3f..acfe4002 100644 --- a/app/routes/geostore/geostore.py +++ b/app/routes/geostore/geostore.py @@ -18,11 +18,13 @@ RWCalcAreaForGeostoreResponse, RWFindByIDsIn, RWFindByIDsResponse, + RWGeostoreIn, RWGeostoreResponse, RWViewGeostoreResponse, ) from ...utils.rw_api import ( calc_area, + create_rw_geostore, find_by_ids, get_admin_list, get_boundary_by_country_id, @@ -45,18 +47,22 @@ ) async def add_new_geostore( *, - request: GeostoreIn, + request: GeostoreIn | RWGeostoreIn, response: ORJSONResponse, # Is this used? + x_api_key: Annotated[str | None, Header()] = None, ): """Add geostore feature to user area of geostore.""" - + # If request follows RW style, forward to RW + if isinstance(request, RWGeostoreIn): + result: RWGeostoreResponse = await create_rw_geostore(request.dict(), x_api_key) + return result + # Otherwise, meant for GFW Data API geostore try: new_user_area: Geostore = await geostore.create_user_area(request.geometry) + return GeostoreResponse(data=new_user_area) except BadRequestError as e: raise HTTPException(status_code=400, detail=str(e)) - return GeostoreResponse(data=new_user_area) - # Endpoint proxied to RW geostore microservice: @router.get( diff --git a/app/utils/rw_api.py b/app/utils/rw_api.py index 48a07771..97b1af80 100644 --- a/app/utils/rw_api.py +++ b/app/utils/rw_api.py @@ -196,6 +196,25 @@ async def signup(name: str, email: str) -> User: return User(**response.json()["data"]) +async def create_rw_geostore( + payload: Dict, x_api_key: str | None = None +) -> RWGeostoreResponse: + url = f"{RW_API_URL}/v1/geostore/area" + + async with AsyncClient() as client: + if x_api_key is not None: + response: HTTPXResponse = await client.post( + url, json=payload, headers={"x-api-key": x_api_key} + ) + else: + response = await client.post(url, json=payload) + + if response.status_code == 200: + return RWGeostoreResponse.parse_obj(response.json()) + else: + raise HTTPException(response.status_code, response.text) + + async def calc_area( payload: Dict, x_api_key: str | None = None ) -> RWCalcAreaForGeostoreResponse: @@ -207,7 +226,7 @@ async def calc_area( url, json=payload, headers={"x-api-key": x_api_key} ) else: - response = await client.get(url) + response = await client.post(url, json=payload) if response.status_code == 200: return RWCalcAreaForGeostoreResponse.parse_obj(response.json()) @@ -226,7 +245,7 @@ async def find_by_ids( url, json=payload, headers={"x-api-key": x_api_key} ) else: - response = await client.get(url) + response = await client.post(url, json=payload) if response.status_code == 200: return RWFindByIDsResponse.parse_obj(response.json())