RDFLib / prez

Prez is a data-configurable Linked Data API framework that delivers profiles of Knowledge Graph data according to the Content Negotiation by Profile standard.
BSD 3-Clause "New" or "Revised" License
22 stars 8 forks source link

Should swagger display additional mediatypes #3

Open nicholascar opened 2 years ago

nicholascar commented 2 years ago

As per title, assume it should, currently only application/json is getting picked up in the dropdown, e.g. here:

http://da-ap-loadb-12x6lrd462qbh-6729920eb84d632c.elb.ap-southeast-2.amazonaws.com:8000/docs#/default/sparql_get_sparql_get

nicholascar commented 2 years ago

Something like this:

@app.get(
    "/sparql",
    summary="SPARQL Endpoint",
    responses={
        200: {
            "content": {
                "text/turtle": {},
                "text/nt": {},
                "application/ld+json": {},
                "application/rdf+xml": {},
                "application/sparql-results+xml": {},
                "application/sparql-results+json": {},
                "text/plain": {},
                "text/csv": {},
                "text/tab-separated-values": {},
            },
            "description": "Returns the standard SPARQL endpoint responses",
        }
    },
)
async def sparql_get(request: Request, query: Optional[str] = None)

But need to remove the default JSON and, generally, review what the API's saying in Swagger.

I think this task shouldn't be done in isolation: a whole-Prez Swagger generation review is needed.

recalcitrantsupplant commented 1 year ago

implementation might be like this: so may require creation of different pydantic response classes e.g. TurtleResponse etc.?

from fastapi import FastAPI
from fastapi.responses import JSONResponse, HTMLResponse
from pydantic import BaseModel
from typing import Union

app = FastAPI()

class JsonResponseModel(BaseModel):
    message: str

class HtmlResponseModel(BaseModel):
    content: str

ResponseUnion = Union[JsonResponseModel, HtmlResponseModel]

@app.api_route(
    "/multiple-media-types",
    methods=["GET"],
    response_model=ResponseUnion,
    responses={
        200: {
            "description": "Return data in JSON or HTML format",
            "content": {
                "application/json": {"schema": JsonResponseModel.schema()},
                "text/html": {"schema": HtmlResponseModel.schema()},
            },
        },
    },
)
async def multiple_media_types(format: str = "json"):
    if format == "json":
        return JsonResponseModel(message="Hello, JSON!")
    elif format == "html":
        return HTMLResponse(content="<h1>Hello, HTML!</h1>")
    else:
        return JSONResponse(content={"error": "Invalid format"}, status_code=400)