|
| 1 | + <a id="top"></a> |
| 2 | + <p align="center"> |
| 3 | + <img src="https://raw.githubusercontent.com/spec-first/connexion/main/docs/images/logo_banner.svg" width="100%"/> |
| 4 | + </p> |
| 5 | + <p align="center"> |
| 6 | + <a href="https://pypi.org/project/connexion"><img alt="coveralls" src="https://img.shields.io/pypi/status/connexion.svg?style=flat-square&color=brightgreen"></a> |
| 7 | + <a href="https://pypi.org/project/connexion"><img alt="PyPI version" src="https://img.shields.io/pypi/v/connexion?color=brightgreen&style=flat-square"></a> |
| 8 | + <a href="https://pypistats.org/packages/connexion"><img alt="PyPI" src="https://img.shields.io/pypi/dm/connexion?style=flat-square&color=brightgreen"></a> |
| 9 | + <a href="https://github.com/spec-first/connexion/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/pypi/l/connexion?style=flat-square&color=brightgreen"></a> |
| 10 | + <a href="https://github.com/spec-first/connexion/actions/workflows/pipeline.yml"><img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/spec-first/connexion/pipeline.yml?style=flat-square"></a> |
| 11 | + <a href="https://coveralls.io/github/spec-first/connexion?branch=main"><img alt="Coveralls" src="https://img.shields.io/coverallsCoverage/github/spec-first/connexion?style=flat-square"></a> |
| 12 | + <br> |
| 13 | + <br> |
| 14 | + <a href="https://connexion.readthedocs.io/en/stable/"><strong>Explore the docs »</strong></a> |
| 15 | + </p> |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +Connexion is a modern Python web framework that makes spec-first and api-first development easy. |
| 20 | +You describe your API in an [OpenAPI][OpenAPI] (or [Swagger][Swagger]) specification with as much |
| 21 | +detail as you want and Connexion will guarantee that it works as you specified. |
| 22 | + |
| 23 | +It works either standalone, or in combination with any ASGI or WSGI-compatible framework! |
| 24 | + |
| 25 | +<p align="center"> |
| 26 | + <br> |
| 27 | + <a href="https://connexion.readthedocs.io/en/latest/v3.html"><strong>📢 Connexion 3 was recently released! Read about the changes here »</strong></a> |
| 28 | + <br> |
| 29 | + <br> |
| 30 | +</p> |
| 31 | + |
| 32 | +## ✨ Features |
| 33 | + |
| 34 | +Connexion provides the following functionality **based on your specification**: |
| 35 | + |
| 36 | +- 🚏 **Automatic route registration**, no ``@route`` decorators needed |
| 37 | +- 🔒 **Authentication**, split from your application logic |
| 38 | +- 🔎 **Request and response validation** of headers, parameters, and body |
| 39 | +- 📬 **Parameter parsing and injection**, no request object needed |
| 40 | +- 📨 **Response serialization**, you can return regular Python objects |
| 41 | +- 📺 **A Swagger UI console** with live documentation and ‘try it out’ feature |
| 42 | +- 🧩 **Pluggability**, in all dimensions |
| 43 | + |
| 44 | +Connexion also **helps you write your OpenAPI specification** and develop against it by providing a command line interface which lets you test and mock your specification. |
| 45 | + |
| 46 | +```shell |
| 47 | + connexion run openapi.yaml |
| 48 | +``` |
| 49 | + |
| 50 | + <p align="right">(<a href="#top">back to top</a>)</p> |
| 51 | + |
| 52 | + |
| 53 | +## 🫶 Sponsors |
| 54 | + |
| 55 | +<a href="https://www.ml6.eu"><img src="https://raw.githubusercontent.com/spec-first/connexion/main/docs/images/sponsors/ML6.png" title=ML6 height="100"></a> |
| 56 | +<a href="https://www.devmark.ai/fern/?utm_source=connexion&utm_loc=readme&utm_type=logo"><img src="https://raw.githubusercontent.com/spec-first/connexion/main/docs/images/sponsors/Fern.png" title=Fern height="100"></a> |
| 57 | + |
| 58 | +Sponsors help us dedicate time to maintain Connexion. Want to help? |
| 59 | + |
| 60 | +<a href="https://github.com/sponsors/spec-first"><strong>Explore the options »</strong></a> |
| 61 | + |
| 62 | +<p align="right">(<a href="#top">back to top</a>)</p> |
| 63 | + |
| 64 | +## 🪤 Why Connexion |
| 65 | + |
| 66 | +With Connexion, you write the spec first. Connexion then calls your Python |
| 67 | +code, handling the mapping from the specification to the code. This |
| 68 | +incentivizes you to write the specification so that all of your |
| 69 | +developers can understand what your API does, even before you write a |
| 70 | +single line of code. |
| 71 | + |
| 72 | +If multiple teams depend on your APIs, you can use Connexion to easily |
| 73 | +send them the documentation of your API. This guarantees that your API will |
| 74 | +follow the specification that you wrote. This is a different process from |
| 75 | +the one offered by most frameworks, which generate a specification |
| 76 | +*after* you've written the code. |
| 77 | +Some disadvantages of generating specifications based on code is that |
| 78 | +they often end up lacking details or mix your documentation with the implementation |
| 79 | +logic of your application. |
| 80 | + |
| 81 | +<p align="right">(<a href="#top">back to top</a>)</p> |
| 82 | + |
| 83 | +## ⚒️ How to Use |
| 84 | + |
| 85 | +### Installation |
| 86 | + |
| 87 | +You can install connexion using pip: |
| 88 | + |
| 89 | +```shell |
| 90 | + $ pip install connexion |
| 91 | +``` |
| 92 | + |
| 93 | +Connexion provides 'extras' with optional dependencies to unlock additional features: |
| 94 | + |
| 95 | +- `swagger-ui`: Enables a Swagger UI console for your application. |
| 96 | +- `uvicorn`: Enables to run the your application using `app.run()` for |
| 97 | + development instead of using an external ASGI server. |
| 98 | +- `flask`: Enables the `FlaskApp` to build applications compatible with the Flask |
| 99 | + ecosystem. |
| 100 | + |
| 101 | +You can install them as follows: |
| 102 | + |
| 103 | +```shell |
| 104 | + $ pip install connexion[swagger-ui] |
| 105 | + $ pip install connexion[swagger-ui,uvicorn] |
| 106 | +``` |
| 107 | + |
| 108 | +<p align="right">(<a href="#top">back to top</a>)</p> |
| 109 | + |
| 110 | +### Creating your application |
| 111 | + |
| 112 | +Connexion can be used either as a standalone application or as a middleware wrapping an existing |
| 113 | +ASGI (or WSGI) application written using a different framework. The standalone application can be |
| 114 | +built using either the `AsyncApp` or `FlaskApp`. |
| 115 | + |
| 116 | +- The `AsyncApp` is a lightweight application with native asynchronous support. Use it if you |
| 117 | + are starting a new project and have no specific reason to use one of the other options. |
| 118 | + |
| 119 | + ```Python |
| 120 | + from connexion import AsyncApp |
| 121 | + |
| 122 | + app = AsyncApp(__name__) |
| 123 | + ``` |
| 124 | + |
| 125 | +- The `FlaskApp` leverages the `Flask` framework, which is useful if you're migrating from |
| 126 | + connexion 2.X or you want to leverage the `Flask` ecosystem. |
| 127 | + |
| 128 | + ```python |
| 129 | + from connexion import FlaskApp |
| 130 | + |
| 131 | + app = FlaskApp(__name__) |
| 132 | + ``` |
| 133 | + |
| 134 | +- The `ConnexionMiddleware` can be wrapped around any existing ASGI or WSGI application. |
| 135 | + Use it if you already have an application written in a different framework and want to add |
| 136 | + functionality provided by connexion |
| 137 | + |
| 138 | + ```python |
| 139 | + from asgi_framework import App |
| 140 | + from connexion import ConnexionMiddleware |
| 141 | + |
| 142 | + app = App(__name__) |
| 143 | + app = ConnexionMiddleware(app) |
| 144 | + ``` |
| 145 | + |
| 146 | +<p align="right">(<a href="#top">back to top</a>)</p> |
| 147 | + |
| 148 | +### Registering an API |
| 149 | + |
| 150 | +While you can register individual routes on your application, Connexion really shines when you |
| 151 | +register an API defined by an OpenAPI (or Swagger) specification. |
| 152 | +The operation described in your specification is automatically linked to your Python view function via the ``operationId`` |
| 153 | + |
| 154 | +**run.py** |
| 155 | + |
| 156 | +```python |
| 157 | + def post_greeting(name: str, greeting: str): # Paramaeters are automatically unpacked |
| 158 | + return f"{greeting} {name}", 200 # Responses are automatically serialized |
| 159 | + |
| 160 | + app.add_api("openapi.yaml") |
| 161 | +``` |
| 162 | + |
| 163 | +**openapi.yaml** |
| 164 | + |
| 165 | +```yaml |
| 166 | + ... |
| 167 | + paths: |
| 168 | + /greeting/{name}: |
| 169 | + post: |
| 170 | + operationId: run.post_greeting |
| 171 | + responses: |
| 172 | + 200: |
| 173 | + content: |
| 174 | + text/plain: |
| 175 | + schema: |
| 176 | + type: string |
| 177 | + parameters: |
| 178 | + - name: name |
| 179 | + in: path |
| 180 | + required: true |
| 181 | + schema: |
| 182 | + type: string |
| 183 | + - name: greeting |
| 184 | + in: query |
| 185 | + required: true |
| 186 | + schema: |
| 187 | + type: string |
| 188 | +``` |
| 189 | +
|
| 190 | +<p align="right">(<a href="#top">back to top</a>)</p> |
| 191 | +
|
| 192 | +### Running your application |
| 193 | +
|
| 194 | +If you installed connexion using `connexion[uvicorn]`, you can run it using the |
| 195 | +`run` method. This is only recommended for development: |
| 196 | + |
| 197 | +```python |
| 198 | + app.run() |
| 199 | +``` |
| 200 | + |
| 201 | +In production, run your application using an ASGI server such as `uvicorn`. If you defined your |
| 202 | +`app` in a python module called `run.py`, you can run it as follows: |
| 203 | + |
| 204 | +```shell |
| 205 | + $ uvicorn run:app |
| 206 | +``` |
| 207 | + |
| 208 | +Or with gunicorn: |
| 209 | + |
| 210 | +```shell |
| 211 | + $ gunicorn -k uvicorn.workers.UvicornWorker run:app |
| 212 | +``` |
| 213 | + |
| 214 | +---- |
| 215 | + |
| 216 | +Now you're able to run and use Connexion! |
| 217 | + |
| 218 | +See the [examples][examples] folder for more examples. |
| 219 | + |
| 220 | +<p align="right">(<a href="#top">back to top</a>)</p> |
| 221 | + |
| 222 | +## 📜 Changes |
| 223 | + |
| 224 | +A full changelog is maintained on the [GitHub releases page][Releases]. |
| 225 | + |
| 226 | +<p align="right">(<a href="#top">back to top</a>)</p> |
| 227 | + |
| 228 | +## 🤲 Contributing |
| 229 | + |
| 230 | +We welcome your ideas, issues, and pull requests. Just follow the |
| 231 | +usual/standard GitHub practices. |
| 232 | + |
| 233 | +For easy development, install connexion using poetry with all extras, and |
| 234 | +install the pre-commit hooks to automatically run black formatting and static analysis checks. |
| 235 | + |
| 236 | +```shell |
| 237 | + pip install poetry |
| 238 | + poetry install --all-extras |
| 239 | + pre-commit install |
| 240 | +``` |
| 241 | + |
| 242 | +You can find out more about how Connexion works and where to apply your changes by having a look |
| 243 | +at our [architecture][Architecture]. |
| 244 | + |
| 245 | +Unless you explicitly state otherwise in advance, any non trivial |
| 246 | +contribution intentionally submitted for inclusion in this project by you |
| 247 | +to the steward of this repository shall be under the |
| 248 | +terms and conditions of Apache License 2.0 written below, without any |
| 249 | +additional copyright information, terms or conditions. |
| 250 | + |
| 251 | +<p align="right">(<a href="#top">back to top</a>)</p> |
| 252 | + |
| 253 | +## 🙏 Thanks |
| 254 | + |
| 255 | +We'd like to thank all of Connexion's contributors for working on this |
| 256 | +project, Swagger/OpenAPI for their support, and Zalando for originally developing and releasing Connexion. |
| 257 | + |
| 258 | +## 📚 Recommended Resources |
| 259 | + |
| 260 | +About the advantages of working spec-first: |
| 261 | + |
| 262 | +* [Blog Atlassian][Blog Atlassian] |
| 263 | +* [API guidelines Zalando][API guidelines Zalando] |
| 264 | +* [Blog ML6][Blog ML6] |
| 265 | +* [Blog Zalando][Blog Zalando] |
| 266 | + |
| 267 | +Tools to help you work spec-first: |
| 268 | + |
| 269 | +* [Online swagger editor][Online swagger editor] |
| 270 | +* [VS Code plugin][VS Code plugin] |
| 271 | +* [Pycharm plugin][Pycharm plugin] |
| 272 | + |
| 273 | +[OpenAPI]: https://openapis.org/ |
| 274 | +[Swagger]: http://swagger.io/open-source-integrations/ |
| 275 | +[Blog atlassian]: https://www.atlassian.com/blog/technology/spec-first-api-development |
| 276 | +[Blog ML6]: https://blog.ml6.eu/why-we-decided-to-help-maintain-connexion-c9f449877083 |
| 277 | +[Blog Zalando]: https://engineering.zalando.com/posts/2016/12/crafting-effective-microservices-in-python.html |
| 278 | +[API guidelines Zalando]: https://opensource.zalando.com/restful-api-guidelines/#api-first |
| 279 | +[Online swagger editor]: https://editor.swagger.io/ |
| 280 | +[VS Code plugin]: https://marketplace.visualstudio.com/items?itemName=42Crunch.vscode-openapi |
| 281 | +[Pycharm plugin]: https://plugins.jetbrains.com/plugin/14837-openapi-swagger-editor |
| 282 | +[examples]: https://github.com/spec-first/connexion/blob/main/examples |
| 283 | +[Releases]: https://github.com/spec-first/connexion/releases |
| 284 | +[Architecture]: https://github.com/spec-first/connexion/blob/main/docs/images/architecture.png |
0 commit comments