INNER CODE UNIT · Python

client_ip

felipeall/transfermarkt-api · app/main.py:19

def client_ip(request: Request) -> str:
    """
    Address of the client calling the API, the key for rate limiting.

    On Fly.io, the proxy sets `Fly-Client-IP` to the address that connected to it, replacing any value sent by the
    client. `X-Forwarded-For` is ignored because clients can prepend arbitrary addresses to it. Without that header,
    the address of the TCP connection is used. Outside Fly.io, clients can set `Fly-Client-IP` themselves.
    """
    return request.headers.get("Fly-Client-IP") or (request.client.host if request.client else "unknown")


limiter = RateLimiter(settings.RATE_LIMITING_FREQUENCY, enabled=settings.RATE_LIMITING_ENABLE)


@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
    """Create the shared tfmkt client on startup and close it on shutdown."""
    app.state.tfmkt = TfmktClient()

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…