Skip to main content

Python OpenAPI 2.0 (Swagger) object model

Project description

# openapi

Python OpenAPI 2.0 (Swagger) object model

There are any number of good libraries for using OpenAPI/Swagger in specific ways or
with specific frameworks. This isn't one of them.

Rather, this library provides a simple object model for producing and consuming
Swagger specifications.


## Models

Model types extend Python `dict`, `list`, or `string` to support reading from JSON,
writing to JSON, and validation against (the relevant portion of) the Swagger schema.

As a result, models are interchangeable with their primitive counterparts and can
be used as much (or as little) as desired to enhange readability or perform validation.

Models convert contained primitive types to models when accessed using keys or attributes
(for `dict`-based types) and when using indexing (for `list`-based access). Access to
model internals via iteration bypasses conversion.


## Usage

1. Either load a schema from json:

from openapi import load

with open("/path/to/swagger.json") as fileobj:
swagger = load(fileobj)

Or construct one explicitly from the model:

from openapi.model import Swagger, Info, Operation, PathItem, Paths, Response, Responses

swagger = Swagger(
swagger="2.0",
info=Info(
title="Example",
version="1.0.0",
),
basePath="/api",
paths=Paths({
"/hello": PathItem(
get=Operation(
responses=Responses({
"200": Response(
description="Returns hello",
)
})
),
),
}),
)

2. Access model internals using attributes:

print swagger.info
print swagger.basePath

Pythonic names are automically converted when using attribute access:

print swagger.base_path

Naturally, attribute names that are illegel or that shadow existing attributes
must be accessed using key syntax:

print swagger.paths["/hello"]["get"].responses["200"].description

3. Validate the model:

swagger.validate()

Internal models can be validated independently:

swagger.info.validate()

4. Dump the result to JSON:

print swagger.dumps()

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

Uploaded Source

File details

Details for the file openapi-0.5.0.tar.gz.

File metadata

  • Download URL: openapi-0.5.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for openapi-0.5.0.tar.gz
Algorithm Hash digest
SHA256 3e41bd2bde077878042f626acfc386fc84a099d5a9e392a5ef1405af001ba768
MD5 e2342dad7cb27ff5c0dc9036dd692433
BLAKE2b-256 1b97d3ce7b7d69c6b046da7c780cb8d731289e7974bb91eb195401807806dc6d

See more details on using hashes here.

Supported by

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