Skip to main content

API Idempotent requests

Project description

fastapi-idempotent

How to use package fastapi-idempotent

# Install package using poetry
poetry add fastapi-idempotent

# Using pip
pip install fastapi-idempotent

Set rate limit for FastAPI before starting server

Example code

  • main.py
# Import package
from fastapi import FastAPI
from fastapi_idempotent import IdempotentMiddleWare
from core.config import settings # settings is class, where the configuration parameters are saved
from api.responses import JSONResponse

...
# Setup FastAPI
app = FastAPI(
    title=settings.PROJECT_NAME,
    version=settings.API_VERSION,
    openapi_url=f"{settings.API_V1_PATH}/openapi.json",
    default_response_class=JSONResponse,
)

Set idempotent for FastAPI

  • Using redis storage
# Default idempotent expired is 10800s
app.add_middleware(
    IdempotentMiddleWare,
    redis_url=settings.IDEMPOTENT_STORAGE_URL,  # type: str
)

or

app.add_middleware(
    IdempotentMiddleWare,
    idempotent_expired=settings.IDEMPOTENT_EXPIRED, # type: int
    redis_url=settings.IDEMPOTENT_STORAGE_URL,  # type: str
)
  • Using memory storage
# Default idempotent expired is 10800s
app.add_middleware(
    IdempotentMiddleWare,
    testing=True, # type: bool
)

or

app.add_middleware(
    IdempotentMiddleWare,
    idempotent_expired=settings.IDEMPOTENT_EXPIRED,  # type: int
    testing=True, # type: bool
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi-idempotent-0.0.3.tar.gz (6.4 kB view hashes)

Uploaded Source

Built Distribution

fastapi_idempotent-0.0.3-py3-none-any.whl (8.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page