Skip to main content

No project description provided

Project description

openapi-bridge

OpenAPI endpoint decorator with pydantic (>=2.0.0) integration.

Allows for almost seamless integration of pydantic models with OpenAPI, generating YAML for the endpoint from type hints.

Using the @endpoint decorator

Example:

@endpoint("/foo/{foo_id}")
def foo(*, foo_id: int, debug: Optional[bool] = False) -> pydantic_models.Foo:
   """
       Foo summary.

       Lengthy foo description.

       @param foo_id The Foo ID in the database.
       @param debug Set to True to include debug information.
   """
   result = _do_something(id, debug)
   return pydantic_models.Foo(**result)

As you can see from the example, the decorator takes a path (which may include a path parameter, in this case id). You can also give it an HTTP method, a path prefix (e.g. to distinguish between internal and external API functions), and security directives.

Information about the endpoint is gathered from both the type annotations of the decorated function and its docstring.

(!) Every parameter (except the optional user) must be keyword-only, have a type hint and a @param help text in the docstring. Un-annotated or undocumented parameters are considered to be a hard error and will raise an exception on startup.

Normally you can just return an instance of the annotated type, and the decorator will handle it correctly, adding an HTTP status 200. If you need to return something else, e.g. some redirect or a 204 ("no content"), you can to return a (raw_content, http_status) tuple instead, e.g.:

return None, 204

The docstring of an endpoint contains its summary, description, the section the documentation is listed under, and parameter help, as well as (optionally) its response in various circumstances.

The summary is the first paragraph of the docstring; the description is taken to be any further paragraphs until the first @keyword.

We recognize the following keywords to designate parts of the documentation:

  • @section <section name>: endpoint is listed in this section.
  • @param <param name> <help text>: explanation of the given parameter.
  • @example <param name> <example text>: example values of the parameter.
  • @response <http status> <JSON>: allows for non-standard responses.

YAML Generation

If you're building a Connexion app, you can use the collected endpoints in your create_app() function:

def create_app():
    # TODO: import all modules with @endpoints here!!
    api_specs = {
        "paths": openapi_bridge.PATHS["default"],
        **openapi_bridge.get_pydantic_schemata(pydantic_models),
    }
    connexion_app.add_api(api_specs)

(!) You need to import all the modules with endpoints here in order to register them. This is easy to forget! If you test a new endpoint and only ever get 404, you might have forgotten to import that module ;-)

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

openapi_bridge-0.5.1.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

openapi_bridge-0.5.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file openapi_bridge-0.5.1.tar.gz.

File metadata

  • Download URL: openapi_bridge-0.5.1.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.0 Darwin/24.6.0

File hashes

Hashes for openapi_bridge-0.5.1.tar.gz
Algorithm Hash digest
SHA256 b8e01666f7c3949a2f951b1f99cd213951f03015856332938c30bc78e72709f5
MD5 1a02789a718e2c6ee1d8e9970023e806
BLAKE2b-256 52010b62900bda8fc29d0e9240801f01f0c43a09f5e17405f4e4ab75277db96e

See more details on using hashes here.

File details

Details for the file openapi_bridge-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: openapi_bridge-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.0 Darwin/24.6.0

File hashes

Hashes for openapi_bridge-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 48e494aea987cab4aeb8ab8cdf9e2684e0e46216dd832fc23733d7fabc572461
MD5 9d448b7a1bbfe46f6ce57878936b2121
BLAKE2b-256 052e77bff7221e1d4e918e3d48ba9c41b9dae3649ec473c89d3982aea4be72dc

See more details on using hashes here.

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