acdh-oeaw / rdfproxy

Functionality for mapping SPARQL query result sets to Pydantic models
GNU General Public License v3.0
2 stars 0 forks source link

Use Query Parameter model for SPARQLModelAdapter.query parameters #150

Closed lu-pl closed 1 hour ago

lu-pl commented 4 days ago

Using a Query Parameter model for SPARQLModelAdapter.query parameters provides better OpenAPI/Swagger definitions for FastAPI code, is generally more explicit and allows for early runtime validation of query parameters.

Passing a single QueryParameters instance to SPARQLModelAdapter.query is much more convenient and less error-prone than requiring all route parameter kwargs to be passed manually. Also, this makes introducing additional parameters for SPARQLModelAdapter.query (e.g. filtering and ordering) easy and defines a single interface for extensions.

class QueryParameters(BaseModel):
    page: int = Field(default=1, gt=0)
    size: int = Field(default=100, ge=1)

@app.get("/")
def base_route(query_paramters: Annotated[QueryParameters, Query()]) -> Page[Author]:
    return adapter.query(query_paramters=query_paramters)
lu-pl commented 4 days ago

Note: rdfproxy.QueryParameters must be publicly available because non-FastAPI code will have to manually instantiate QueryParameters, which btw is a good thing. :)

lu-pl commented 4 days ago

Having query parameters live in their own namespace is also convenient, e.g. SPARQLModelAdapter.query would return a Page object like so:

return Page(
    items=items,
    page=query_parameters.page,
    size=query_parameters.size,
    total=total,
    pages=pages,
)