Argument parsing with structured class.
Project description
defspec
Create the OpenAPI spec and document from dataclass
, attrs
, msgspec
, etc.
Why not ...
[!NOTE] There are also lots of other projects can generate the OpenAPI document or even validate the data. This project is not intended to replace them.
This project is a legacy of a private initiative. During the development, I discovered that using msgspec
could elegantly define and generate the API schema. The OpenAPI component can be utilized to generate the API documentation for various projects. As a result, I made the decision to extract it and transform it into a public project.
You can use this project as a low-level component or a drop-in module when you don't want to introduce too many other dependencies.
Installation
pip install defspec
Examples
flask
: examples/flaskfalcon
: examples/falcon
You can run the above examples and open the OpenAPI document in your browser:
swagger
: http://127.0.0.1:8000/openapi/swaggerredoc
: http://127.0.0.1:8000/openapi/redocscalar
: http://127.0.0.1:8000/openapi/scalar
Usage
from dataclasses import dataclass
from typing import List
from defspec import OpenAPI
@dataclass
class User:
name: str
age: int
openapi = OpenAPI()
openapi.register_route("/", method="get", summary="Hello World")
openapi.register_route(
"/users", method="post", summary="Get all the user info", response_type=List[User]
)
# get the OpenAPI spec
print(openapi.to_dict())
# get the OpenAPI spec bytes
with open("openapi.json", "wb") as f:
f.write(openapi.to_json())
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file defspec-0.1.0.tar.gz
.
File metadata
- Download URL: defspec-0.1.0.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae50803e97041efe8ee5b580314611ffa079c6ccf767992302624b10163f8551 |
|
MD5 | ca5bb9467cc9fa11a9f33d1ccd46fb08 |
|
BLAKE2b-256 | 70d0363edc6bfb076b9a3d18497551b3a890a197853de593442e5882cff1eb20 |
File details
Details for the file defspec-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: defspec-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f68a6769030f466e5520a497f5ed9e0e0baeeff36b0bb4e5e9b052174eab68e1 |
|
MD5 | d497db0852386728283a7cb077c50b2f |
|
BLAKE2b-256 | 34e52e93d90a1f64d993fbc871aab8ea09f8505564324d6f297386efffdc86be |