Пример #1
0
def hello_endpoint(app):
    add_exception_handler(app)

    async def get_hello_etag(request: Request):
        return "etagfor" + request.path_params["name"]

    @app.get("/hello/{name}", dependencies=[Depends(Etag(get_hello_etag))])
    async def hello(name: str):
        return {"hello": name}

    @app.get("/no-etag", dependencies=[Depends(Etag(lambda x: None))])
    async def get_missing_etag():
        """
        This endpoint has no stored etag
        """
        return {"a": "b"}

    @app.get(
        "/hello/{name}/extra-headers",
        dependencies=[
            Depends(Etag(get_hello_etag, extra_headers=EXTRA_HEADERS))
        ],
    )
    async def hello_extra_headers(name: str):
        return {"hello": name}
Пример #2
0
def hello_endpoint(app):
    add_exception_handler(app)

    async def get_hello_etag(request: Request):
        return "etagfor" + request.path_params["name"]

    @app.get("/hello/{name}", dependencies=[Depends(Etag(get_hello_etag))])
    async def hello(name: str):
        return {"hello": name}
Пример #3
0
        return datetime.datetime.fromtimestamp(
            int(d)).strftime('%Y-%m-%d %H:%M:%S')
    except OSError:
        return "-"


@template_filter('filesize')
def filter_filesize(d: int) -> str:
    d = int(d)
    if d > 1024**3:
        return "%.2f GB" % (d / (1024**3))
    else:
        return "%.2f MB" % (d / (1024**2))


@router.get('/repos', dependencies=[Depends(Etag(get_etag))])
async def repos(request: Request, response: Response) -> Response:
    return templates.TemplateResponse("repos.html", {
        "request": request,
        "repos": get_repositories()
    },
                                      headers=dict(response.headers))


@router.get('/stats', dependencies=[Depends(Etag(get_etag))])
async def stats(request: Request, response: Response) -> Response:
    return templates.TemplateResponse("stats.html", {"request": request},
                                      headers=dict(response.headers))


@router.get('/mirrors', dependencies=[Depends(Etag(get_etag))])
Пример #4
0
from fastapi import FastAPI
from starlette.requests import Request
from fastapi_etag import Etag, add_exception_handler

app = FastAPI()
add_exception_handler(app)


async def get_hello_etag(request: Request):
    return "etagfor" + request.path_params["name"]


@app.get("/hello/{name}", dependencies=[Depends(Etag(get_hello_etag))])
async def hello(name: str):
    return {"hello": name}