Skip to content

A middleware to handle ETag for Starletter or FastAPI.

License

Notifications You must be signed in to change notification settings

keakon/etag-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ETag Middleware

Build Status Coverage

A middleware to handle ETag for Starlette or FastAPI.

Installation

pip install etag-middleware

Usage

FastAPI

from etag import ETagMiddleware
from fastapi import FastAPI
from fastapi.middleware import Middleware

app = FastAPI(middleware=[Middleware(ETagMiddleware)])

Starlette

from etag import ETagMiddleware
from starlette.applications import Starlette
from starlette.middleware import Middleware

app = Starlette(middleware=[Middleware(ETagMiddleware)])

Notice

  1. It won't compute ETag for requests with body size less than 80 bytes by default. Beacause ETag and If-None-Match increase more than 70 bytes to the response header, and computing ETag consumes CPU. You can adjust it by setting minimum_size:

    app = FastAPI(middleware=[Middleware(ETagMiddleware, minimum_size=0)])
  2. It won't compute ETag for StreamingResponse. Because it will delay sending the response until collected all its body and consumes more memory.

  3. It compares ETag with If-None-Match for FileResponse.

About

A middleware to handle ETag for Starletter or FastAPI.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages