Skip to content

Commit

Permalink
Refactor code for species endpoint
Browse files Browse the repository at this point in the history
Make consistent with more recent coding style.
  • Loading branch information
JimBacon committed Aug 8, 2024
1 parent 0a5a847 commit dfd6fca
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 80 deletions.
4 changes: 2 additions & 2 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import app.main as app
from app.rule.rule_routes import router as rule_router
from app.settings import settings
import app.species as species
from app.species.species_routes import router as species_router
from app.user.user_routes import router as user_router
from app.validate.validate_routes import router as validate_router
from app.verify.verify_routes import router as verify_router
Expand Down Expand Up @@ -38,7 +38,7 @@ class SettingResponse(BaseModel):
router = APIRouter()
router.include_router(auth.router)
router.include_router(rule_router)
router.include_router(species.router)
router.include_router(species_router)
router.include_router(user_router)
router.include_router(validate_router)
router.include_router(verify_router)
Expand Down
63 changes: 0 additions & 63 deletions app/species/__init__.py
Original file line number Diff line number Diff line change
@@ -1,63 +0,0 @@
from fastapi import APIRouter, HTTPException, status

import app.auth as auth
from app.database import DB
import app.species.cache as cache
# Indicia is the current source of taxon data but one day, maybe, there will
# be a UKSI API. For this reason, it is abstracted into its own module.
import app.species.indicia as driver
from app.sqlmodels import Taxon

router = APIRouter()
router.include_router(driver.router)
router.include_router(cache.router)


@router.get(
"/species/taxon_by_tvk/{tvk}",
tags=['Species'],
summary="Get taxon with given TVK.",
response_model=Taxon)
async def read_taxon_by_tvk(
auth: auth.Auth,
session: DB,
tvk: str):

try:
taxon = cache.get_taxon_by_tvk(session, tvk)
if taxon is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Taxon not found with TVK {tvk}.")

return taxon

except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=str(e))


@router.get(
'/species/taxon_by_name/{name}',
tags=['Species'],
summary="Get taxon with given name.",
response_model=Taxon)
async def read_taxon_by_name(
auth: auth.Auth,
session: DB,
name: str):

try:
taxon = cache.get_taxon_by_name(session, name)
if taxon is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Taxon not found with name {name}.")

return taxon

except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=str(e))
21 changes: 8 additions & 13 deletions app/species/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@


@router.get(
"/species/cache/count",
"/cache/count",
tags=['Species Cache'],
summary="Get number of records in species cache.",
response_model=int)
response_model=dict)
async def read_cache_size(
auth: Auth,
session: DB
):
count = session.exec(
Expand All @@ -28,12 +27,11 @@ async def read_cache_size(


@router.get(
"/species/cache/{id}",
"/cache/{id}",
tags=['Species Cache'],
summary="Get item from species cache.",
response_model=Taxon)
async def read_cache_item(
auth: Auth,
session: DB,
id: int):
taxon = session.get(Taxon, id)
Expand All @@ -46,12 +44,11 @@ async def read_cache_item(


@router.delete(
"/species/cache/all",
"/cache/all",
tags=['Species Cache'],
summary="Empty the species cache.",
response_model=bool)
response_model=dict)
async def cache_clear(
auth: Auth,
session: DB
):
session.exec(
Expand All @@ -62,12 +59,11 @@ async def cache_clear(


@router.delete(
"/species/cache/{id}",
"/cache/{id}",
tags=['Species Cache'],
summary="Delete item from species cache.",
response_model=bool)
response_model=dict)
async def delete_cache_item(
auth: Auth,
session: DB,
id: int):
session.exec(
Expand All @@ -78,12 +74,11 @@ async def delete_cache_item(


@router.get(
"/species/cache/taxon_by_tvk/{tvk}",
"/cache/taxon_by_tvk/{tvk}",
tags=['Species Cache'],
summary="Get taxon with given TVK from cache.",
response_model=Taxon)
async def read_taxon_by_tvk(
auth: Auth,
session: DB,
tvk: str):
return get_taxon_by_tvk(session, tvk)
Expand Down
3 changes: 1 addition & 2 deletions app/species/indicia.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ class IndiciaError(Exception):


@router.get(
'/species/indicia/taxon',
'/indicia/taxon',
tags=['Indicia'],
summary="Search Indicia for taxa matching your parameters.",
response_model=IndiciaResponse)
async def search_taxa(
auth: auth.Auth,
searchQuery: Annotated[
str,
Query(description="Search text which will be used to look up species "
Expand Down
64 changes: 64 additions & 0 deletions app/species/species_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from fastapi import APIRouter, Depends, HTTPException, status

from app.auth import get_current_user
from app.database import DB
import app.species.cache as cache
# Indicia is the current source of taxon data but one day, maybe, there will
# be a UKSI API. For this reason, it is abstracted into its own module.
import app.species.indicia as driver
from app.sqlmodels import Taxon

router = APIRouter(
prefix="/species",
dependencies=[Depends(get_current_user)]
)
router.include_router(driver.router)
router.include_router(cache.router)


@router.get(
"/taxon_by_tvk/{tvk}",
tags=["Species"],
summary="Get taxon with given TVK.",
response_model=Taxon)
async def read_taxon_by_tvk(
session: DB,
tvk: str):

try:
taxon = cache.get_taxon_by_tvk(session, tvk)
if taxon is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Taxon not found with TVK {tvk}.")

return taxon

except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=str(e))


@router.get(
'/taxon_by_name/{name}',
tags=["Species"],
summary="Get taxon with given name.",
response_model=Taxon)
async def read_taxon_by_name(
session: DB,
name: str):

try:
taxon = cache.get_taxon_by_name(session, name)
if taxon is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Taxon not found with name {name}.")

return taxon

except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=str(e))

0 comments on commit dfd6fca

Please sign in to comment.