diff options
author | Bryan Newbold <bnewbold@archive.org> | 2021-01-14 20:11:48 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@archive.org> | 2021-01-14 20:11:48 -0800 |
commit | 8aeec55922862c180f3bdaf5e02c4a37c3640d1c (patch) | |
tree | 554af379a31a9c21d7df83af004fdb088ae069cf /fatcat_scholar/web.py | |
parent | 10ff66b6bfdf33b3fba97800433a6692dcacce29 (diff) | |
download | fatcat-scholar-8aeec55922862c180f3bdaf5e02c4a37c3640d1c.tar.gz fatcat-scholar-8aeec55922862c180f3bdaf5e02c4a37c3640d1c.zip |
api: add type annotations for /search response
Diffstat (limited to 'fatcat_scholar/web.py')
-rw-r--r-- | fatcat_scholar/web.py | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/fatcat_scholar/web.py b/fatcat_scholar/web.py index b1107c6..0b82df9 100644 --- a/fatcat_scholar/web.py +++ b/fatcat_scholar/web.py @@ -5,8 +5,9 @@ So far there are few endpoints, so we just put them all here! """ import logging -from typing import Optional, Any +from typing import Optional, Any, List +from pydantic import BaseModel import babel.support from fastapi import FastAPI, APIRouter, Request, Depends, Response from fastapi.staticfiles import StaticFiles @@ -19,6 +20,7 @@ from starlette.exceptions import HTTPException as StarletteHTTPException from fatcat_scholar.config import settings, GIT_REVISION from fatcat_scholar.hacks import Jinja2Templates, parse_accept_lang from fatcat_scholar.search import do_fulltext_search, FulltextQuery, FulltextHits +from fatcat_scholar.schema import ScholarDoc logger = logging.getLogger() @@ -80,10 +82,33 @@ api = APIRouter() async def home() -> Any: return {"endpoints": {"/": "this", "/search": "fulltext search"}} +class HitsModel(BaseModel): + count_returned: int + count_found: int + offset: int + limit: int + query_time_ms: int + query_wall_time_ms: int + results: List[ScholarDoc] + +@api.get("/search", operation_id="get_search", response_model=HitsModel) +async def search(query: FulltextQuery = Depends(FulltextQuery)) -> FulltextHits: + if query.q is not None: + try: + hits: FulltextHits = do_fulltext_search(query) + except ValueError as e: + sentry_sdk.set_level("warning") + sentry_sdk.capture_exception(e) + raise HTTPException(status_code=400, detail=f"Query Error: {e}") + except IOError as e: + sentry_sdk.capture_exception(e) + raise HTTPException(status_code=500, detail=f"Backend Error: {e}") + + # remove internal context from hit objects + for doc in hits.results: + doc.pop('_obj', None) -@api.get("/search", operation_id="get_search") -async def search(query: FulltextQuery = Depends(FulltextQuery)) -> Any: - return {"message": "search results would go here, I guess"} + return hits web = APIRouter() |