diff --git a/openaq_api/openaq_api/v3/routers/countries.py b/openaq_api/openaq_api/v3/routers/countries.py index 1a35d6e..7377773 100644 --- a/openaq_api/openaq_api/v3/routers/countries.py +++ b/openaq_api/openaq_api/v3/routers/countries.py @@ -2,7 +2,7 @@ import logging from typing import Annotated -from fastapi import APIRouter, Depends, Path, Query +from fastapi import APIRouter, Depends, HTTPException, Path, Query from openaq_api.db import DB from openaq_api.v3.models.queries import ( @@ -75,6 +75,8 @@ async def country_get( db: DB = Depends(), ): response = await fetch_countries(countries, db) + if len(response.results) == 0: + raise HTTPException(status_code=404, detail="Country not found") return response diff --git a/openaq_api/openaq_api/v3/routers/instruments.py b/openaq_api/openaq_api/v3/routers/instruments.py index 1bad6b0..6ea9940 100644 --- a/openaq_api/openaq_api/v3/routers/instruments.py +++ b/openaq_api/openaq_api/v3/routers/instruments.py @@ -2,7 +2,7 @@ import logging from typing import Annotated -from fastapi import APIRouter, Depends, Path, Query +from fastapi import APIRouter, Depends, HTTPException, Path, Query from openaq_api.db import DB from openaq_api.v3.models.queries import ( @@ -90,6 +90,8 @@ async def instrument_get( db: DB = Depends(), ): response = await fetch_instruments(instruments, db) + if len(response.results) == 0: + raise HTTPException(status_code=404, detail="Instrument not found") return response diff --git a/openaq_api/openaq_api/v3/routers/latest.py b/openaq_api/openaq_api/v3/routers/latest.py index 7693d0b..ff56cf8 100644 --- a/openaq_api/openaq_api/v3/routers/latest.py +++ b/openaq_api/openaq_api/v3/routers/latest.py @@ -2,9 +2,11 @@ import logging from typing import Annotated -from fastapi import APIRouter, Depends, Path, Query +from fastapi import APIRouter, Depends, HTTPException, Path, Query from openaq_api.db import DB +from openaq_api.v3.routers.locations import LocationPathQuery, fetch_locations +from openaq_api.v3.routers.parameters import fetch_parameters from openaq_api.v3.models.queries import QueryBaseModel, QueryBuilder, Paging from openaq_api.v3.models.responses import LatestResponse @@ -87,8 +89,8 @@ class ParametersLatestQueries(ParameterLatestPathQuery, DatetimeMinQuery, Paging @router.get( "/parameters/{parameters_id}/latest", response_model=LatestResponse, - summary="Get a owner by ID", - description="Provides a owner by owner ID", + summary="", + description="", ) async def parameters_latest_get( parameters_latest: Annotated[ @@ -97,6 +99,10 @@ async def parameters_latest_get( db: DB = Depends(), ): response = await fetch_latest(parameters_latest, db) + if len(response.results) == 0: + parameters_response = await fetch_parameters(parameters_latest, db) + if len(parameters_response.results) == 0: + raise HTTPException(status_code=404, detail="Parameter not found") return response @@ -130,16 +136,22 @@ class LocationsLatestQueries(LocationLatestPathQuery, DatetimeMinQuery, Paging): @router.get( "/locations/{locations_id}/latest", response_model=LatestResponse, - summary="Get a owner by ID", - description="Provides a owner by owner ID", + summary="Get a location's latest measurements", + description="Providers a location's latest measurement values", ) -async def owner_get( +async def location_latest_get( locations_latest: Annotated[ LocationsLatestQueries, Depends(LocationsLatestQueries.depends()) ], db: DB = Depends(), ): response = await fetch_latest(locations_latest, db) + if len(response.results) == 0: + locations_response = await fetch_locations( + LocationPathQuery(locations_id=locations_latest.locations_id), db + ) + if len(locations_response.results) == 0: + raise HTTPException(status_code=404, detail="Location not found") return response diff --git a/openaq_api/openaq_api/v3/routers/licenses.py b/openaq_api/openaq_api/v3/routers/licenses.py index e2e0014..05759a8 100644 --- a/openaq_api/openaq_api/v3/routers/licenses.py +++ b/openaq_api/openaq_api/v3/routers/licenses.py @@ -2,7 +2,7 @@ import logging from typing import Annotated -from fastapi import APIRouter, Depends, Path, Query +from fastapi import APIRouter, Depends, HTTPException, Path, Query from openaq_api.db import DB from openaq_api.v3.models.queries import ( @@ -72,6 +72,8 @@ async def license_get( db: DB = Depends(), ): response = await fetch_licenses(licenses, db) + if len(response.results) == 0: + raise HTTPException(status_code=404, detail="License not found") return response diff --git a/openaq_api/openaq_api/v3/routers/locations.py b/openaq_api/openaq_api/v3/routers/locations.py index f00dc99..1dd8995 100644 --- a/openaq_api/openaq_api/v3/routers/locations.py +++ b/openaq_api/openaq_api/v3/routers/locations.py @@ -1,7 +1,7 @@ import logging from typing import Annotated from enum import StrEnum, auto -from fastapi import APIRouter, Depends, Path, Query, Request +from fastapi import APIRouter, Depends, HTTPException, Path, Query, Request from openaq_api.db import DB from openaq_api.v3.models.queries import ( @@ -98,8 +98,9 @@ async def location_get( request: Request, db: DB = Depends(), ): - print("FOO", request.app.state.redis_client) response = await fetch_locations(locations, db) + if len(response.results) == 0: + raise HTTPException(status_code=404, detail="Location not found") return response @@ -142,5 +143,6 @@ async def fetch_locations(query, db): {query_builder.order_by()} {query_builder.pagination()} """ + print("SQL", sql) response = await db.fetchPage(sql, query_builder.params()) return response diff --git a/openaq_api/openaq_api/v3/routers/manufacturers.py b/openaq_api/openaq_api/v3/routers/manufacturers.py index fe539c0..e3aa0c1 100644 --- a/openaq_api/openaq_api/v3/routers/manufacturers.py +++ b/openaq_api/openaq_api/v3/routers/manufacturers.py @@ -2,7 +2,7 @@ import logging from typing import Annotated -from fastapi import APIRouter, Depends, Path, Query +from fastapi import APIRouter, Depends, HTTPException, Path, Query from openaq_api.db import DB from openaq_api.v3.models.queries import ( @@ -74,6 +74,8 @@ async def manufacturer_get( db: DB = Depends(), ): response = await fetch_manufacturers(manufacturers, db) + if len(response.results) == 0: + raise HTTPException(status_code=404, detail="Manufacturer not found") return response diff --git a/openaq_api/openaq_api/v3/routers/owners.py b/openaq_api/openaq_api/v3/routers/owners.py index 57ee4d9..96a3880 100644 --- a/openaq_api/openaq_api/v3/routers/owners.py +++ b/openaq_api/openaq_api/v3/routers/owners.py @@ -2,7 +2,7 @@ import logging from typing import Annotated -from fastapi import APIRouter, Depends, Path, Query +from fastapi import APIRouter, Depends, HTTPException, Path, Query from openaq_api.db import DB from openaq_api.v3.models.queries import ( @@ -73,6 +73,8 @@ async def owner_get( db: DB = Depends(), ): response = await fetch_owners(owners, db) + if len(response.results) == 0: + raise HTTPException(status_code=404, detail="Owner not found") return response diff --git a/openaq_api/openaq_api/v3/routers/parameters.py b/openaq_api/openaq_api/v3/routers/parameters.py index 93a5d1e..53aef62 100644 --- a/openaq_api/openaq_api/v3/routers/parameters.py +++ b/openaq_api/openaq_api/v3/routers/parameters.py @@ -2,7 +2,7 @@ from enum import StrEnum, auto from typing import Annotated -from fastapi import APIRouter, Depends, Path, Query +from fastapi import APIRouter, Depends, HTTPException, Path, Query from openaq_api.db import DB from openaq_api.v3.models.queries import ( @@ -149,6 +149,8 @@ async def parameter_get( db: DB = Depends(), ) -> ParametersResponse: response = await fetch_parameters(parameter, db) + if len(response.results) == 0: + raise HTTPException(status_code=404, detail="Parameter not found") return response diff --git a/openaq_api/openaq_api/v3/routers/providers.py b/openaq_api/openaq_api/v3/routers/providers.py index 981f3c5..ec7d5a7 100644 --- a/openaq_api/openaq_api/v3/routers/providers.py +++ b/openaq_api/openaq_api/v3/routers/providers.py @@ -2,7 +2,7 @@ from typing import Annotated from enum import StrEnum, auto -from fastapi import APIRouter, Depends, Path, Query +from fastapi import APIRouter, Depends, HTTPException, Path, Query from openaq_api.db import DB from openaq_api.v3.models.queries import ( @@ -101,6 +101,8 @@ async def provider_get( db: DB = Depends(), ): response = await fetch_providers(providers, db) + if len(response.results) == 0: + raise HTTPException(status_code=404, detail="Provider not found") return response diff --git a/openaq_api/openaq_api/v3/routers/sensors.py b/openaq_api/openaq_api/v3/routers/sensors.py index dc17a27..31c4360 100644 --- a/openaq_api/openaq_api/v3/routers/sensors.py +++ b/openaq_api/openaq_api/v3/routers/sensors.py @@ -1,7 +1,7 @@ import logging from typing import Annotated -from fastapi import APIRouter, Depends, Path +from fastapi import APIRouter, Depends, HTTPException, Path from openaq_api.db import DB from openaq_api.v3.models.queries import ( @@ -65,7 +65,10 @@ async def sensor_get( sensors: Annotated[SensorPathQuery, Depends(SensorPathQuery.depends())], db: DB = Depends(), ): - return await fetch_sensors(sensors, db) + response = await fetch_sensors(sensors, db) + if len(response.results) == 0: + raise HTTPException(status_code=404, detail="Sensor not found") + return response async def fetch_sensors(q, db): diff --git a/openaq_api/tests/fastapi-test.py b/openaq_api/tests/fastapi-test.py index ce4c307..c1accf4 100644 --- a/openaq_api/tests/fastapi-test.py +++ b/openaq_api/tests/fastapi-test.py @@ -43,7 +43,6 @@ "/v3/providers/62", "/v3/sensors/662", "/v3/locations/tiles/2/2/1.pbf", - "/v3/locations/2178/trends/2", ]