Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/niyaznz/fastapi-mock-middleware
FastAPI middleware for mocking response data of non-implemented endpoints
https://github.com/niyaznz/fastapi-mock-middleware
api-first fastapi mock mock-api mock-fastapi mock-response
Last synced: 21 days ago
JSON representation
FastAPI middleware for mocking response data of non-implemented endpoints
- Host: GitHub
- URL: https://github.com/niyaznz/fastapi-mock-middleware
- Owner: NiyazNz
- License: mit
- Created: 2024-03-19T17:45:27.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-07-09T19:42:13.000Z (6 months ago)
- Last Synced: 2024-12-15T23:13:59.633Z (22 days ago)
- Topics: api-first, fastapi, mock, mock-api, mock-fastapi, mock-response
- Language: Python
- Homepage: https://niyaznz.github.io/fastapi-mock-middleware/
- Size: 584 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# FastAPI mock middleware
[![license](https://img.shields.io/badge/License-MIT-dark)](https://github.com/NiyazNz/fastapi-mock-middleware/blob/main/LICENSE.txt)
[![pypi](https://img.shields.io/pypi/v/fastapi-mock-middleware?color=00FF00)](https://pypi.org/project/fastapi-mock-middleware/)
[![test](https://github.com/NiyazNz/fastapi-mock-middleware/workflows/Test/badge.svg)](https://github.com/NiyazNz/fastapi-mock-middleware/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/NiyazNz/fastapi-mock-middleware/graph/badge.svg?token=GX8A8HD0KL)](https://codecov.io/gh/NiyazNz/fastapi-mock-middleware)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](https://github.com/litestar-org/polyfactory).---
For more information on how to use fastapi-mock-middleware, please refer to the
[official documentation](https://niyaznz.github.io/fastapi-mock-middleware/).## Installation
```shell
pip install fastapi-mock-middleware
```## Usage example
Add `MockAPIMiddleware` middleware to app and raise `APINotImplementedError` in
your endpoint stubs.```Python hl_lines="8 18"
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModelfrom 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`.
```shell
curl http://127.0.0.1:8000/
```Called API must return mocked data:
```json
[
{
"id": 5392,
"name": "gVzyVVUmGGevXlQvXGBW"
}
]
```