Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 404 for resources #374

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion openaq_api/openaq_api/v3/routers/countries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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


Expand Down
4 changes: 3 additions & 1 deletion openaq_api/openaq_api/v3/routers/instruments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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


Expand Down
24 changes: 18 additions & 6 deletions openaq_api/openaq_api/v3/routers/latest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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[
Expand All @@ -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


Expand Down Expand Up @@ -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


Expand Down
4 changes: 3 additions & 1 deletion openaq_api/openaq_api/v3/routers/licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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


Expand Down
6 changes: 4 additions & 2 deletions openaq_api/openaq_api/v3/routers/locations.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion openaq_api/openaq_api/v3/routers/manufacturers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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


Expand Down
4 changes: 3 additions & 1 deletion openaq_api/openaq_api/v3/routers/owners.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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


Expand Down
4 changes: 3 additions & 1 deletion openaq_api/openaq_api/v3/routers/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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


Expand Down
4 changes: 3 additions & 1 deletion openaq_api/openaq_api/v3/routers/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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


Expand Down
7 changes: 5 additions & 2 deletions openaq_api/openaq_api/v3/routers/sensors.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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):
Expand Down
1 change: 0 additions & 1 deletion openaq_api/tests/fastapi-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"/v3/providers/62",
"/v3/sensors/662",
"/v3/locations/tiles/2/2/1.pbf",
"/v3/locations/2178/trends/2",
]


Expand Down
Loading