Skip to main content

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

You can run the above examples and open the OpenAPI document in your browser:

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())

# serve as a HTTP server
openapi.serve_as_http_daemon(port=8000, run_in_background=True)

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

defspec-0.1.1.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

defspec-0.1.1-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file defspec-0.1.1.tar.gz.

File metadata

  • Download URL: defspec-0.1.1.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for defspec-0.1.1.tar.gz
Algorithm Hash digest
SHA256 147cc6533f2839888d5c3012bf3b77487f6902be2e0abf0f2e51d1dd89d2e8e7
MD5 9e275bbddd7ca09cf03d463540b4e3a0
BLAKE2b-256 05716115e09276aa27039ac45c0996a53c650701f5a8b93e36c28ab0189cc119

See more details on using hashes here.

File details

Details for the file defspec-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: defspec-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for defspec-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 64fa521346058db577d4e82ff64e9d68bef04a0de236bc6eee342fc7ea8ae12b
MD5 28e625448554861f49525d32888f1859
BLAKE2b-256 f9497befd7a1a6c5bbf0ff4cd5286d63aeaf8addbd676386f5d70378e7bc8454

See more details on using hashes here.

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