Skip to main content

About

asyncswagger11 is an anyio-compatible clone of swagger.py, capable of understanding Swagger 1.1 definitions (only).

As swagger has been renamed to OpenAPI which by now has version 3.0 (and has an actual specification – unlike Swagger 1.1) this library is (mostly) only usable with Asterisk, which still uses Swagger 1.1 declarations.

Asyncswagger11 supports a WebSocket extension, allowing a WebSocket to be documented, and auto-generated WebSocket client code.

from swagger.py:

Swagger.py is a Python library for using Swagger defined APIs.

Swagger itself is best described on the Swagger home page:

Swagger is a specification and complete framework implementation for describing, producing, consuming, and visualizing RESTful web services.

The Swagger specification defines how APIs may be described using Swagger.

Usage

Install the latest release from PyPI.

$ pip install asyncswagger11

Or install from source:

$ python3 -mbuild

API

asyncswagger11 will dynamically build an object model from a Swagger-enabled RESTful API.

Here is a simple example using the Asterisk REST Interface

#!/usr/bin/env python3

import json
import anyio

from asyncswagger11.client import SwaggerClient
from asyncswagger11.http_client import AsynchronousHttpClient

http_client = AsynchronousHttpClient()
http_client.set_api_key('localhost', 'hey:peekaboo')

async def run(ari,msg_json):
    channelId = msg_json['channel']['id']
    await ari.channels.answer(channelId=channelId)
    await ari.channels.play(channelId=channelId,
                    media='sound:hello-world')
    # In a real program you should wait for the PlaybackFinished event instead
    await anyio.sleep(3)
    await ari.channels.continueInDialplan(channelId=channelId)

async def main():
    async with SwaggerClient(
        "http://localhost:8088/ari/api-docs/resources.json",
        http_client=http_client) as ari:

        ws = ari.events.eventWebsocket(app='hello')

        async for msg in ws:
            if not isinstance(msg, WebsocketDataMessage):
                break
            elif not isinstance(msg, WebsocketTextMessage):
                continue # ignore bytes

            msg_json = json.loads(msg.data)
            if msg_json['type'] == 'StasisStart':
                await nursery.start_soon(run,ari,msg_json)

if __name__ == "__main__":
    anyio.run(main)

Data model

The data model presented by the swagger_model module is nearly identical to the original Swagger API resource listing and API declaration. This means that if you add extra custom metadata to your docs (such as a _author or _copyright field), they will carry forward into the object model. I recommend prefixing custom fields with an underscore, to avoid collisions with future versions of Swagger.

There are a few meaningful differences.

  • Resource listing

  • The file and base_dir fields have been added, referencing the original .json file.

  • The objects in a resource_listing’s api array contains a field api_declaration, which is the processed result from the referenced API doc.

  • API declaration

  • A file field has been added, referencing the original .json file.

Development

The code is documented using Sphinx, which allows IntelliJ IDEA to do a better job at inferring types for autocompletion.

To keep things isolated, I also recommend installing (and using) virtualenv.

$ sudo pip install virtualenv
$ mkdir -p ~/virtualenv
$ virtualenv ~/virtualenv/swagger
$ . ~/virtualenv/swagger/bin/activate

Setuptools is used for building. Pytest is used for unit testing, with the coverage plugin installed to generated code coverage reports. Pass --with-coverage to generate the code coverage report. HTML versions of the reports are put in cover/index.html.

$ ./setup.py develop   # prep for development (install deps, launchers, etc.)
$ ./setup.py pytest    # run unit tests
$ ./setup.py bdist_egg # build distributable

Testing

Simply run python3 setup.py pytest.

Note that standalone-testing this module currently is not possible. Previous versions required a hacked version of httpretty.

TODO: use a local server instead.

License

Copyright (c) 2013, Digium, Inc. Copyright (c) 2018, Matthias Urlichs

asyncswagger11 is licensed with a BSD 3-Clause License.

The current author humbly requests that you share any further bug fixes or enhancements to this code.

Metadata

Release files for asyncswagger11 0.21.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asyncswagger11 0.21.0
File Size Uploaded Provenance
asyncswagger11-0.21.0.tar.gz 27.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for asyncswagger11 0.21.0
File Interpreter ABI Platform
asyncswagger11-0.21.0-py3-none-any.whl Python 3 none any Details

Total release size: 44.3 kB

Release files / asyncswagger11-0.21.0.tar.gz

Download URL asyncswagger11-0.21.0.tar.gz
Size 27.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4e170dbbacc7c8c7c7b537f4814676540bc4721fccab0514fdcbb684f4b004cc
BLAKE2b-256 checksum
How to use checksums
7962eec194e0f9d6898257f54d01d0fed394a295d8f73bd15bbede05552ddeb7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / asyncswagger11-0.21.0-py3-none-any.whl

Download URL asyncswagger11-0.21.0-py3-none-any.whl
Size 16.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
473c318fe8fb745652f51974a044d5f4fa6de4d4056daaeb1bc1b7c7ee04a2d8
BLAKE2b-256 checksum
How to use checksums
4234d91286bb4bed4dc48d05170d3e754b103ff9d04915b6fdfc995bb94bf3b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Security for asyncswagger11 0.21.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page