Skip to content

Commit

Permalink
Fix RW proxied geostore find-by-ids endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dmannarino committed Jan 16, 2025
1 parent 5c0ab0b commit 1c2ae72
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
35 changes: 34 additions & 1 deletion app/models/pydantic/geostore.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ class AdminBoundaryInfo(StrictBaseModel):
iso: str


class FindByIDsInfo(StrictBaseModel):
use: Dict
iso: str
name: str


class LandUseUse(StrictBaseModel):
use: LandUseTypeUseString
id: int
Expand Down Expand Up @@ -125,7 +131,7 @@ class RWGeostoreAttributes(StrictBaseModel):
areaHa: float
bbox: List[float]
lock: bool
info: AdminBoundaryInfo | LandUseInfo | WDPAInfo
info: AdminBoundaryInfo | FindByIDsInfo | LandUseInfo | WDPAInfo


class RWGeostore(StrictBaseModel):
Expand All @@ -136,3 +142,30 @@ class RWGeostore(StrictBaseModel):

class RWGeostoreResponse(StrictBaseModel):
data: RWGeostore


class RWFindByIDsGeostoreData(StrictBaseModel):
type: Literal["geoStore"]
id: str
attributes: RWGeostoreAttributes


class RWFindByIDsDataGeostore(StrictBaseModel):
data: RWFindByIDsGeostoreData


class RWFindByIDsData(StrictBaseModel):
geostoreId: str
geostore: RWFindByIDsGeostoreData
returned: int


class RWFindByIDsInfo(StrictBaseModel):
found: int
foundIDs: List[str]
returned: int


class RWFindByIDsResponse(StrictBaseModel):
data: List[RWFindByIDsData]
info: RWFindByIDsInfo
13 changes: 5 additions & 8 deletions app/routes/geostore/geostore.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from fastapi import APIRouter, Header, HTTPException, Path
from fastapi.responses import ORJSONResponse
from httpx import Response as HTTPXResponse

from ...crud import geostore
from ...errors import BadRequestError, RecordNotFoundError
Expand All @@ -18,6 +17,7 @@
RWCalcAreaForGeostoreIn,
RWCalcAreaForGeostoreResponse,
RWFindByIDsIn,
RWFindByIDsResponse,
RWGeostoreResponse,
RWViewGeostoreResponse,
)
Expand Down Expand Up @@ -72,7 +72,6 @@ async def rw_get_view_geostore_by_id(
):
"""Get a geostore object by Geostore id and view at GeoJSON.io
(proxies request to the RW API)"""
# FIXME: Should we be passing on things like the API key?
result: RWViewGeostoreResponse = await get_view_geostore_by_id(
rw_geostore_id, x_api_key
)
Expand Down Expand Up @@ -109,7 +108,6 @@ async def get_any_geostore(*, geostore_id: UUID = Path(..., title="geostore_id")
async def rw_get_admin_list(x_api_key: Annotated[str | None, Header()] = None):
"""Get all Geostore IDs, names and country codes
(proxies request to the RW API)"""
# FIXME: Should we be passing on things like the API key?
result: RWAdminListResponse = await get_admin_list(x_api_key)

return result
Expand Down Expand Up @@ -148,7 +146,6 @@ async def rw_get_boundary_by_region_id(
):
"""Get a GADM boundary by country and region IDs
(proxies request to the RW API)"""
# FIXME: Should we be passing on things like the API key?
result: RWGeostoreResponse = await get_boundary_by_region_id(
country_id, region_id, x_api_key
)
Expand Down Expand Up @@ -201,18 +198,18 @@ async def rw_calc_area(
@router.post(
"/find_by_ids",
response_class=ORJSONResponse,
# response_model=RWAdminListResponse,
response_model=RWFindByIDsResponse,
# tags=["Geostore"],
)
async def rw_find_by_ids(
request: RWFindByIDsIn,
x_api_key: Annotated[str | None, Header()] = None,
):
"""Get one or more geostore objects by IDs
(proxies request to the RW API)"""
# FIXME: Should we be passing on things like the API key?
payload: Dict = request.dict()

result: HTTPXResponse = await find_by_ids(payload)
result: RWFindByIDsResponse = await find_by_ids(payload, x_api_key)

return result

Expand All @@ -225,9 +222,9 @@ async def rw_find_by_ids(
)
async def rw_get_geostore_by_land_use_and_index(
*,
x_api_key: Annotated[str | None, Header()] = None,
land_use_type: LandUseType = Path(..., title="land_use_type"),
index: str = Path(..., title="index"),
x_api_key: Annotated[str | None, Header()] = None,
):
"""Get a geostore object by land use type name and id
(proxies request to the RW API)"""
Expand Down
18 changes: 15 additions & 3 deletions app/utils/rw_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
GeostoreCommon,
RWAdminListResponse,
RWCalcAreaForGeostoreResponse,
RWFindByIDsResponse,
RWGeostoreResponse,
RWViewGeostoreResponse,
)
Expand Down Expand Up @@ -214,12 +215,23 @@ async def calc_area(
raise HTTPException(response.status_code, response.text)


async def find_by_ids(payload: Dict) -> HTTPXResponse:
async def find_by_ids(
payload: Dict, x_api_key: str | None = None
) -> RWFindByIDsResponse:
url = f"{RW_API_URL}/v2/geostore/find_by_ids"

async with AsyncClient() as client:
response: HTTPXResponse = await client.post(url, json=payload)
return response
if x_api_key is not None:
response: HTTPXResponse = await client.get(
url, headers={"x-api-key": x_api_key}
)
else:
response = await client.get(url)

if response.status_code == 200:
return RWFindByIDsResponse.parse_obj(response.json())
else:
raise HTTPException(response.status_code, response.text)


async def get_admin_list(x_api_key: str | None = None) -> RWAdminListResponse:
Expand Down

0 comments on commit 1c2ae72

Please sign in to comment.