Skip to main content

FastAPI middleware for mocking response data of non-implemented endpoints

Project description

FastAPI mock middleware

license pypi test codecov

FastAPI middleware for mocking response data of non-implemented endpoints.

Mock data is generated in accordance with endpoint return type or provided response_model using polifactory.


For more information on how to use fastapi-mock-middleware, please refer to the official documentation.

Installation

pip install fastapi-mock-middleware

Usage example

Add MockAPIMiddleware middleware to app and raise APINotImplementedError in your endpoint stubs.

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel

from fastapi_mock_middleware import MockAPIMiddleware, APINotImplementedError

app = FastAPI()
app.add_middleware(MockAPIMiddleware)


class Item(BaseModel):
    id: int
    name: str


@app.get('/')
async def list_items() -> list[Item]:
    raise APINotImplementedError()


if __name__ == '__main__':
    uvicorn.run('example:app', reload=True)

Check the response using curl.

curl http://127.0.0.1:8000/

Called API must return mocked data:

[
  {
    "id": 5392,
    "name": "gVzyVVUmGGevXlQvXGBW"
  }
]

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-mock-middleware-0.1.0.tar.gz (11.4 kB view hashes)

Uploaded Source

Built Distribution

fastapi_mock_middleware-0.1.0-py3-none-any.whl (6.2 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