Skip to content

Commit

Permalink
Typeahead API
Browse files Browse the repository at this point in the history
  • Loading branch information
keul committed Nov 14, 2024
1 parent 7660de1 commit 2ac2e34
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cads_catalogue_api_service/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
messages,
middlewares,
schema_org,
typeahead,
vocabularies,
)

Expand Down Expand Up @@ -89,6 +90,7 @@ async def lifespan(application: fastapi.FastAPI):
app.include_router(collection_ext.router)
app.include_router(doi.router)
app.include_router(contents.router)
app.include_router(typeahead.router)


def catalogue_openapi() -> dict[str, Any]:
Expand Down
36 changes: 36 additions & 0 deletions cads_catalogue_api_service/typeahead.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import fastapi

from . import dependencies, search_utils

router = fastapi.APIRouter(
prefix="",
tags=["typeahead"],
responses={fastapi.status.HTTP_404_NOT_FOUND: {"description": "Not found"}},
include_in_schema=False,
)


@router.get("/typeahead")
def typeahead(
session=fastapi.Depends(dependencies.get_session),
portals: list[str] | None = fastapi.Depends(dependencies.get_portals),
chars: str = fastapi.Query(..., min_length=2, max_length=50),
) -> list[str]:
"""Typeahead for CADS webportal search feature."""
search = search_utils.apply_filters_typeahead(
session, chars, search=None, portals=portals
)
result = session.execute(search)
return result.scalars().all()

0 comments on commit 2ac2e34

Please sign in to comment.