Skip to main content

OpenAPI spec generation and web page rendering.

Project description

DefSpec

Python Check PyPI

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
# to enable the offline feature
pip install defspec[offline]

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.3.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

defspec-0.3.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: defspec-0.3.0.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for defspec-0.3.0.tar.gz
Algorithm Hash digest
SHA256 3809227eae1af54fe36159b726fb1b7c7531f5f5f73cc9676258f477f4ec8e53
MD5 64fef40a185878370f31143db5f0be60
BLAKE2b-256 c23a7b263e6edc9fcf40e456df10ad3b3162d0a14709f42503ee8326b70e5cf2

See more details on using hashes here.

Provenance

The following attestation bundles were made for defspec-0.3.0.tar.gz:

Publisher: release.yml on kemingy/defspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: defspec-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for defspec-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fb4095917c62119ce0343d68063cce737fb81970634c02de960500a8092d6450
MD5 e76792473eeffa4840a9d1ddc8dc5b31
BLAKE2b-256 4bed6c391eb8fdf87a859245eea8f3fadd428a19dad281c0f29e6ece1a8e7afc

See more details on using hashes here.

Provenance

The following attestation bundles were made for defspec-0.3.0-py3-none-any.whl:

Publisher: release.yml on kemingy/defspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page