|
12 | 12 | <img src="https://codecov.io/gh/mr-fatalyst/fastopenapi/branch/master/graph/badge.svg?token=USHR1I0CJB"> |
13 | 13 | <img src="https://img.shields.io/pypi/v/fastopenapi"> |
14 | 14 | <img src="https://img.shields.io/pypi/pyversions/fastopenapi"> |
| 15 | + <img src="https://static.pepy.tech/badge/fastopenapi" alt="PyPI Downloads"> |
15 | 16 | </p> |
16 | 17 |
|
17 | 18 | --- |
@@ -39,153 +40,209 @@ pip install fastopenapi[starlette] |
39 | 40 |
|
40 | 41 | --- |
41 | 42 |
|
42 | | -## ⚙️ Features |
43 | | -- 📄 **Generate OpenAPI schemas** with Pydantic v2. |
44 | | -- 🛡️ **Data validation** using Pydantic models. |
45 | | -- 🛠️ **Supports multiple frameworks:** Falcon, Flask, Sanic, Starlette. |
46 | | -- ✅ **Compatible with Pydantic v2.** |
47 | | - |
48 | | ---- |
49 | | - |
50 | 43 | ## 🛠️ Quick Start |
51 | 44 |
|
52 | | -###  Example |
53 | | -<details> |
54 | | -<summary>Click to expand</summary> |
55 | | - |
56 | | -```python |
57 | | -import falcon.asgi |
58 | | -import uvicorn |
59 | | -from pydantic import BaseModel |
60 | | - |
61 | | -from fastopenapi.routers.falcon import FalconRouter |
62 | | - |
63 | | -app = falcon.asgi.App() |
64 | | -router = FalconRouter(app=app, docs_url="/docs/", openapi_version="3.0.0") |
65 | | - |
66 | | - |
67 | | -class HelloResponse(BaseModel): |
68 | | - message: str |
69 | | - |
70 | | - |
71 | | -@router.get("/hello", tags=["Hello"], status_code=200, response_model=HelloResponse) |
72 | | -async def hello(name: str): |
73 | | - """Say hello from Falcon""" |
74 | | - return HelloResponse(message=f"Hello, {name}! It's Falcon!") |
| 45 | +### Step 1. Create an application |
| 46 | + |
| 47 | +- Create the `main.py` file |
| 48 | +- Copy the code from an example |
| 49 | +- For some examples uvicorn is required (`pip install uvicorn`) |
| 50 | + |
| 51 | +#### Examples: |
| 52 | + |
| 53 | +-  |
| 54 | + <details> |
| 55 | + <summary>Click to expand the Falcon Example</summary> |
| 56 | + |
| 57 | + ```python |
| 58 | + import falcon.asgi |
| 59 | + import uvicorn |
| 60 | + from pydantic import BaseModel |
| 61 | + |
| 62 | + from fastopenapi.routers import FalconRouter |
| 63 | + |
| 64 | + app = falcon.asgi.App() |
| 65 | + router = FalconRouter(app=app, docs_url="/docs/", openapi_version="3.0.0") |
| 66 | + |
| 67 | + |
| 68 | + class HelloResponse(BaseModel): |
| 69 | + message: str |
| 70 | + |
| 71 | + |
| 72 | + @router.get("/hello", tags=["Hello"], status_code=200, response_model=HelloResponse) |
| 73 | + async def hello(name: str): |
| 74 | + """Say hello from Falcon""" |
| 75 | + return HelloResponse(message=f"Hello, {name}! It's Falcon!") |
| 76 | + |
| 77 | + |
| 78 | + if __name__ == "__main__": |
| 79 | + uvicorn.run(app, host="127.0.0.1", port=8000) |
| 80 | + ``` |
| 81 | + </details> |
| 82 | + |
| 83 | +-  |
| 84 | + <details> |
| 85 | + <summary>Click to expand the Flask Example</summary> |
| 86 | + |
| 87 | + ```python |
| 88 | + from flask import Flask |
| 89 | + from pydantic import BaseModel |
| 90 | + |
| 91 | + from fastopenapi.routers import FlaskRouter |
| 92 | + |
| 93 | + app = Flask(__name__) |
| 94 | + router = FlaskRouter(app=app, docs_url="/docs/", openapi_version="3.0.0") |
| 95 | + |
| 96 | + |
| 97 | + class HelloResponse(BaseModel): |
| 98 | + message: str |
| 99 | + |
| 100 | + |
| 101 | + @router.get("/hello", tags=["Hello"], status_code=200, response_model=HelloResponse) |
| 102 | + def hello(name: str): |
| 103 | + """Say hello from Flask""" |
| 104 | + return HelloResponse(message=f"Hello, {name}! It's Flask!") |
| 105 | + |
| 106 | + |
| 107 | + if __name__ == "__main__": |
| 108 | + app.run(port=8000) |
| 109 | + ``` |
| 110 | + </details> |
| 111 | + |
| 112 | +-  |
| 113 | + <details> |
| 114 | + <summary>Click to expand the Quart Example</summary> |
| 115 | + |
| 116 | + ```python |
| 117 | + from pydantic import BaseModel |
| 118 | + from quart import Quart |
| 119 | + |
| 120 | + from fastopenapi.routers import QuartRouter |
| 121 | + |
| 122 | + app = Quart(__name__) |
| 123 | + router = QuartRouter(app=app, docs_url="/docs/", openapi_version="3.0.0") |
| 124 | + |
| 125 | + |
| 126 | + class HelloResponse(BaseModel): |
| 127 | + message: str |
| 128 | + |
| 129 | + |
| 130 | + @router.get("/hello", tags=["Hello"], status_code=200, response_model=HelloResponse) |
| 131 | + async def hello(name: str): |
| 132 | + """Say hello from Quart""" |
| 133 | + return HelloResponse(message=f"Hello, {name}! It's Quart!") |
| 134 | + |
| 135 | + |
| 136 | + if __name__ == "__main__": |
| 137 | + app.run(port=8000) |
| 138 | + ``` |
| 139 | + </details> |
| 140 | + |
| 141 | +-  |
| 142 | + <details> |
| 143 | + <summary>Click to expand the Sanic Example</summary> |
| 144 | + |
| 145 | + ```python |
| 146 | + from pydantic import BaseModel |
| 147 | + from sanic import Sanic |
| 148 | + |
| 149 | + from fastopenapi.routers import SanicRouter |
| 150 | + |
| 151 | + app = Sanic("MySanicApp") |
| 152 | + router = SanicRouter(app=app, docs_url="/docs/", openapi_version="3.0.0") |
| 153 | + |
| 154 | + |
| 155 | + class HelloResponse(BaseModel): |
| 156 | + message: str |
| 157 | + |
| 158 | + |
| 159 | + @router.get("/hello", tags=["Hello"], status_code=200, response_model=HelloResponse) |
| 160 | + async def hello(name: str): |
| 161 | + """Say hello from Sanic""" |
| 162 | + return HelloResponse(message=f"Hello, {name}! It's Sanic!") |
| 163 | + |
| 164 | + |
| 165 | + if __name__ == "__main__": |
| 166 | + app.run(host="0.0.0.0", port=8000) |
| 167 | + ``` |
| 168 | + </details> |
| 169 | + |
| 170 | +-  |
| 171 | + <details> |
| 172 | + <summary>Click to expand the Starlette Example</summary> |
| 173 | + |
| 174 | + ```python |
| 175 | + import uvicorn |
| 176 | + from pydantic import BaseModel |
| 177 | + from starlette.applications import Starlette |
| 178 | + |
| 179 | + from fastopenapi.routers import StarletteRouter |
| 180 | + |
| 181 | + app = Starlette() |
| 182 | + router = StarletteRouter(app=app, docs_url="/docs/", openapi_version="3.0.0") |
| 183 | + |
| 184 | + |
| 185 | + class HelloResponse(BaseModel): |
| 186 | + message: str |
| 187 | + |
| 188 | + |
| 189 | + @router.get("/hello", tags=["Hello"], status_code=200, response_model=HelloResponse) |
| 190 | + async def hello(name: str): |
| 191 | + """Say hello from Starlette""" |
| 192 | + return HelloResponse(message=f"Hello, {name}! It's Starlette!") |
| 193 | + |
| 194 | + if __name__ == "__main__": |
| 195 | + uvicorn.run(app, host="127.0.0.1", port=8000) |
| 196 | + ``` |
| 197 | + </details> |
| 198 | + |
| 199 | +### Step 2. Run the server |
| 200 | + |
| 201 | +Launch the application: |
75 | 202 |
|
76 | | - |
77 | | -if __name__ == "__main__": |
78 | | - uvicorn.run(app, host="127.0.0.1", port=8000) |
| 203 | +```bash |
| 204 | +python main.py |
79 | 205 | ``` |
80 | | -</details> |
81 | | - |
82 | | ---- |
83 | | - |
84 | | -###  Example |
85 | | -<details> |
86 | | -<summary>Click to expand</summary> |
87 | | - |
88 | | -```python |
89 | | -from flask import Flask |
90 | | -from pydantic import BaseModel |
91 | | - |
92 | | -from fastopenapi.routers.flask import FlaskRouter |
93 | | - |
94 | | -app = Flask(__name__) |
95 | | -router = FlaskRouter(app=app, docs_url="/docs/", openapi_version="3.0.0") |
96 | | - |
97 | | - |
98 | | -class HelloResponse(BaseModel): |
99 | | - message: str |
100 | 206 |
|
| 207 | +Once launched, the documentation will be available at: |
101 | 208 |
|
102 | | -@router.get("/hello", tags=["Hello"], status_code=200, response_model=HelloResponse) |
103 | | -def hello(name: str): |
104 | | - """Say hello from Flask""" |
105 | | - return HelloResponse(message=f"Hello, {name}! It's Flask!") |
106 | | - |
107 | | - |
108 | | -if __name__ == "__main__": |
109 | | - app.run(debug=True, port=8000) |
110 | 209 | ``` |
111 | | -</details> |
| 210 | +http://127.0.0.1:8000/docs/ |
| 211 | +``` |
112 | 212 |
|
113 | 213 | --- |
114 | 214 |
|
115 | | -###  Example |
116 | | -<details> |
117 | | -<summary>Click to expand</summary> |
118 | | - |
119 | | -```python |
120 | | -from pydantic import BaseModel |
121 | | -from sanic import Sanic |
122 | | - |
123 | | -from fastopenapi.routers.sanic import SanicRouter |
124 | | - |
125 | | -app = Sanic("MySanicApp") |
126 | | -router = SanicRouter(app=app, docs_url="/docs/", openapi_version="3.0.0") |
127 | | - |
128 | | - |
129 | | -class HelloResponse(BaseModel): |
130 | | - message: str |
131 | | - |
132 | | - |
133 | | -@router.get("/hello", tags=["Hello"], status_code=200, response_model=HelloResponse) |
134 | | -async def hello(name: str): |
135 | | - """Say hello from Sanic""" |
136 | | - return HelloResponse(message=f"Hello, {name}! It's Sanic!") |
137 | | - |
138 | | - |
139 | | -if __name__ == "__main__": |
140 | | - app.run(host="0.0.0.0", port=8000, debug=True) |
141 | | -``` |
142 | | -</details> |
| 215 | +## ⚙️ Features |
| 216 | +- **Generate OpenAPI schemas** with Pydantic v2. |
| 217 | +- **Data validation** using Pydantic models. |
| 218 | +- **Supports multiple frameworks:** Falcon, Flask, Quart, Sanic, Starlette. |
| 219 | +- **Proxy routing provides FastAPI-style routing** |
143 | 220 |
|
144 | 221 | --- |
145 | 222 |
|
146 | | -###  Example |
147 | | -<details> |
148 | | -<summary>Click to expand</summary> |
149 | | - |
150 | | -```python |
151 | | -import uvicorn |
152 | | -from pydantic import BaseModel |
153 | | -from starlette.applications import Starlette |
| 223 | +## 📖 Documentation |
154 | 224 |
|
155 | | -from fastopenapi.routers.starlette import StarletteRouter |
| 225 | +Explore the [Docs](https://github.com/mr-fatalyst/fastopenapi/blob/master/docs/en/index.md) for an overview of FastOpenAPI, its core components, and usage guidelines. The documentation is continuously updated and improved. |
156 | 226 |
|
157 | | -app = Starlette() |
158 | | -router = StarletteRouter(app=app, docs_url="/docs/", openapi_version="3.0.0") |
| 227 | +--- |
159 | 228 |
|
| 229 | +## 📂 Advanced Examples |
160 | 230 |
|
161 | | -class HelloResponse(BaseModel): |
162 | | - message: str |
| 231 | +Examples of integration and detailed usage for each framework are available in the [`examples`](https://github.com/mr-fatalyst/fastopenapi/tree/master/examples) directory. |
163 | 232 |
|
| 233 | +--- |
164 | 234 |
|
165 | | -@router.get("/hello", tags=["Hello"], status_code=200, response_model=HelloResponse) |
166 | | -async def hello(name: str): |
167 | | - """Say hello from Starlette""" |
168 | | - return HelloResponse(message=f"Hello, {name}! It's Starlette!") |
| 235 | +## ✅ Development Recommendations |
169 | 236 |
|
170 | | -if __name__ == "__main__": |
171 | | - uvicorn.run(app, host="127.0.0.1", port=8000) |
172 | | -``` |
173 | | -</details> |
| 237 | +- Use Pydantic models for strict typing and data validation. |
| 238 | +- Follow the project structure similar to provided examples for easy scalability. |
| 239 | +- Regularly update dependencies and monitor library updates for new features. |
174 | 240 |
|
175 | 241 | --- |
176 | 242 |
|
177 | | -## 🛡️ **Type Safety with Pydantic v2** |
178 | | -```python |
179 | | -from pydantic import BaseModel |
| 243 | +## 🛠️ Contributing |
180 | 244 |
|
181 | | -class User(BaseModel): |
182 | | - id: int |
183 | | - name: str |
184 | | - |
185 | | -@router.post("/api/v1/users/") |
186 | | -def create_user(user: User) -> User: |
187 | | - return user |
188 | | -``` |
| 245 | +If you have suggestions or find a bug, please open an issue or create a pull request on GitHub. |
189 | 246 |
|
190 | 247 | --- |
191 | 248 |
|
|
0 commit comments