Skip to content

Commit

Permalink
Fix dropping payload and post->get in find-by-ids, calc area function…
Browse files Browse the repository at this point in the history
…s; implement proxying add geostore route
  • Loading branch information
dmannarino committed Jan 17, 2025
1 parent 8e63c8c commit 6dff707
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
10 changes: 9 additions & 1 deletion app/models/pydantic/geostore.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class GeostoreIn(StrictBaseModel):
geometry: Geometry


class RWGeostoreIn(StrictBaseModel):
geojson: Geometry | Feature | FeatureCollection


class GeostoreResponse(Response):
data: Geostore

Expand Down Expand Up @@ -93,6 +97,10 @@ class AdminBoundaryInfo(StrictBaseModel):
iso: str


class CreateGeostoreResponseInfo(StrictBaseModel):
use: Dict


class FindByIDsInfo(StrictBaseModel):
use: Dict
iso: str
Expand Down Expand Up @@ -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):
Expand Down
14 changes: 10 additions & 4 deletions app/routes/geostore/geostore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down
23 changes: 21 additions & 2 deletions app/utils/rw_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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())
Expand All @@ -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())
Expand Down

0 comments on commit 6dff707

Please sign in to comment.