Skip to content

Commit

Permalink
Updates to many of the endpoint return schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
bergsalex committed Nov 13, 2024
1 parent 76352cb commit 24a45f8
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 128 deletions.
179 changes: 81 additions & 98 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "geneweaver-api"
version = "0.11.0"
version = "0.12.0a0"
description = "The Geneweaver API"
authors = [
"Alexander Berger <alexander.berger@jax.org>",
Expand All @@ -19,7 +19,7 @@ packages = [
python = "^3.9"
geneweaver-core = "^0.10.0a3"
fastapi = {extras = ["all"], version = "^0.115.0"}
uvicorn = {extras = ["standard"], version = "^0.30.0"}
uvicorn = {extras = ["standard"], version = "^0.32.0"}
geneweaver-db = "^0.6.0a3"
psycopg-pool = "^3.1.7"
requests = "^2.32.3"
Expand Down
10 changes: 5 additions & 5 deletions src/geneweaver/api/controller/genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
GeneIdMappingAonReq,
GeneIdMappingReq,
GeneIdMappingResp,
GeneReturn,
)
from geneweaver.api.services import genes as genes_service
from geneweaver.core.enum import GeneIdentifier, Species
from geneweaver.core.schema.gene import Gene
from jax.apiutils import CollectionResponse, Response
from typing_extensions import Annotated

from . import message as api_message
Expand Down Expand Up @@ -50,15 +50,15 @@ def get_genes(
description=api_message.OFFSET,
),
] = None,
) -> GeneReturn:
) -> CollectionResponse[Gene]:
"""Get geneweaver list of genes."""
if limit is None:
limit = 100

response = genes_service.get_genes(
cursor, reference_id, gene_database, species, preferred, limit, offset
)
return response
return CollectionResponse[Gene](**response)


@router.get("/{gene_id}/preferred")
Expand All @@ -67,10 +67,10 @@ def get_gene_preferred(
int, Path(format="int64", minimum=0, maxiumum=9223372036854775807)
],
cursor: Optional[deps.Cursor] = Depends(deps.cursor),
) -> Gene:
) -> Response[Gene]:
"""Get preferred gene for a given gene ode_id."""
response = genes_service.get_gene_preferred(cursor, gene_id)
return response
return Response[Gene](response)


@router.post("/homologs", response_model=GeneIdMappingResp, deprecated=True)
Expand Down
20 changes: 11 additions & 9 deletions src/geneweaver/api/controller/genesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
from geneweaver.api.services import geneset as genset_service
from geneweaver.api.services import publications as publication_service
from geneweaver.core.enum import GeneIdentifier, GenesetTier, Species
from geneweaver.core.schema.publication import Publication
from geneweaver.core.schema.score import GenesetScoreType, ScoreType
from geneweaver.db import search as db_search
from jax.apiutils import CollectionResponse
from jax.apiutils import CollectionResponse, Response
from typing_extensions import Annotated

from . import message as api_message
Expand Down Expand Up @@ -114,7 +115,7 @@ def get_visible_genesets(
description=api_message.OFFSET,
),
] = None,
) -> dict:
) -> CollectionResponse:
"""Get all visible genesets."""
response = genset_service.get_visible_genesets(
cursor=cursor,
Expand Down Expand Up @@ -144,7 +145,7 @@ def get_visible_genesets(

raise_http_error(response)

return response
return CollectionResponse(**response)


@router.get("/search")
Expand Down Expand Up @@ -175,7 +176,7 @@ def get_geneset(
cursor: Optional[deps.Cursor] = Depends(deps.cursor),
gene_id_type: Optional[GeneIdentifier] = None,
in_threshold: Optional[bool] = None,
) -> dict:
) -> Response:
"""Get a geneset by ID. Optional filter results by gene identifier type."""
if gene_id_type:
response = genset_service.get_geneset_w_gene_id_type(
Expand Down Expand Up @@ -276,15 +277,15 @@ def get_geneset_metadata(
user: deps.OptionalFullUserDep,
cursor: Optional[deps.Cursor] = Depends(deps.cursor),
include_pub_info: Optional[bool] = False,
) -> dict:
) -> Response:
"""Get a geneset metadata by geneset id."""
response = genset_service.get_geneset_metadata(
cursor, geneset_id, user, include_pub_info
)

raise_http_error(response)

return response
return Response(**response)


@router.get("/{geneset_id}/publication")
Expand All @@ -294,14 +295,15 @@ def get_publication_for_geneset(
],
user: deps.OptionalFullUserDep,
cursor: Optional[deps.Cursor] = Depends(deps.cursor),
) -> dict:
) -> Response[Publication]:
"""Get the publication associated with the geneset."""
geneset_resp = genset_service.get_geneset_metadata(cursor, geneset_id, user, True)

if "error" in geneset_resp:
raise_http_error(geneset_resp)

geneset = geneset_resp.get("geneset")
geneset = geneset_resp.get("object")

if geneset is None:
raise HTTPException(status_code=404, detail=api_message.RECORD_NOT_FOUND_ERROR)

Expand All @@ -314,7 +316,7 @@ def get_publication_for_geneset(
if pub_resp is None:
raise HTTPException(status_code=404, detail=api_message.RECORD_NOT_FOUND_ERROR)

return pub_resp
return Response[Publication](pub_resp)


@router.put("/{geneset_id}/threshold", status_code=204)
Expand Down
17 changes: 9 additions & 8 deletions src/geneweaver/api/controller/publications.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from geneweaver.api.schemas.auth import UserInternal
from geneweaver.api.services import publications as publication_service
from geneweaver.core.schema.publication import Publication
from jax.apiutils import CollectionResponse, Response
from typing_extensions import Annotated

from . import message as api_message
Expand Down Expand Up @@ -51,7 +52,7 @@ def get_publication(
description=api_message.OFFSET,
),
] = None,
) -> dict:
) -> CollectionResponse[Publication]:
"""Get all publication publications with optional filters."""
response = publication_service.get(
cursor,
Expand All @@ -70,7 +71,7 @@ def get_publication(
offset=offset,
)

return response
return CollectionResponse(**response)


@router.get("/{publication_id}")
Expand All @@ -80,19 +81,19 @@ def get_publication_by_id(
],
as_pubmed_id: Optional[bool] = True,
cursor: Optional[deps.Cursor] = Depends(deps.cursor),
) -> Publication:
) -> Response[Publication]:
"""Get a publication by id."""
if as_pubmed_id:
response = publication_service.get_publication_by_pubmed_id(
cursor, publication_id
cursor, str(publication_id)
)
else:
response = publication_service.get_publication(cursor, publication_id)

if response is None:
raise HTTPException(status_code=404, detail=api_message.RECORD_NOT_FOUND_ERROR)

return response
return Response(response)


@router.put("/{publication_id}")
Expand All @@ -102,10 +103,10 @@ def add_publication(
],
user: UserInternal = Security(deps.full_user),
cursor: Optional[deps.Cursor] = Depends(deps.cursor),
) -> NewPubmedRecord:
) -> Response[NewPubmedRecord]:
"""Add pubmed publication endpoint."""
response = publication_service.add_pubmed_record(
cursor=cursor, user=user, pubmed_id=publication_id
cursor=cursor, user=user, pubmed_id=str(publication_id)
)

if "error" in response:
Expand All @@ -120,4 +121,4 @@ def add_publication(
else:
raise HTTPException(status_code=500, detail=api_message.UNEXPECTED_ERROR)

return response
return Response(response)
10 changes: 5 additions & 5 deletions src/geneweaver/api/controller/species.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

from fastapi import APIRouter, Depends, Query
from geneweaver.api import dependencies as deps
from geneweaver.api.schemas.apimodels import SpeciesReturn
from geneweaver.api.services import species as species_service
from geneweaver.core.enum import GeneIdentifier, Species
from geneweaver.core.schema.species import Species as SpeciesSchema
from jax.apiutils import CollectionResponse, Response
from typing_extensions import Annotated

router = APIRouter(prefix="/species", tags=["species"])
Expand All @@ -20,18 +20,18 @@ def get_species(
Optional[int], Query(format="int64", minimum=0, maxiumum=9223372036854775807)
] = None,
reference_gene_id_type: Optional[GeneIdentifier] = None,
) -> SpeciesReturn:
) -> CollectionResponse[SpeciesSchema]:
"""Get species."""
response = species_service.get_species(cursor, taxonomy_id, reference_gene_id_type)

return response
return CollectionResponse(**response)


@router.get("/{species_id}")
def get_species_by_id(
species_id: Species, cursor: Optional[deps.Cursor] = Depends(deps.cursor)
) -> SpeciesSchema:
) -> Response[SpeciesSchema]:
"""Get species."""
response = species_service.get_species_by_id(cursor, species_id)

return response
return Response(response)
2 changes: 1 addition & 1 deletion src/geneweaver/api/services/geneset.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def get_geneset_metadata(
if len(results) <= 0:
return {"error": True, "message": message.INACCESSIBLE_OR_FORBIDDEN}

return {"geneset": results[0]}
return {"object": results[0]}

except Exception as err:
logger.error(err)
Expand Down

0 comments on commit 24a45f8

Please sign in to comment.