Skip to main content

Asynchronous library for accessing Swagger-1.1-enabled APIs

Project description

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.

$ sudo pip install asyncswagger11

Or install from source using the setup.py script.

$ sudo ./setup.py install

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():
    ari = SwaggerClient(
        "http://localhost:8088/ari/api-docs/resources.json",
        http_client=http_client)

    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.

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

asyncswagger11-0.12.4.tar.gz (27.1 kB view details)

Uploaded Source

Built Distribution

asyncswagger11-0.12.4-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file asyncswagger11-0.12.4.tar.gz.

File metadata

  • Download URL: asyncswagger11-0.12.4.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.21.0 setuptools/52.0.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.9.5

File hashes

Hashes for asyncswagger11-0.12.4.tar.gz
Algorithm Hash digest
SHA256 1d0d18086351a01e82df74315828101338e018f1087fcb12bb63b2152afeb6e3
MD5 c88390f58f7557cb87151f025a2aa02e
BLAKE2b-256 9b45bb36cb4bed0923996e4999257ce9afab8ebab4e09535d9311bf1c184da09

See more details on using hashes here.

File details

Details for the file asyncswagger11-0.12.4-py3-none-any.whl.

File metadata

File hashes

Hashes for asyncswagger11-0.12.4-py3-none-any.whl
Algorithm Hash digest
SHA256 83ba9ae4e236f185bb73d705be987dc9861eca170eaba7f19f0dadf3f9163899
MD5 d5798ec4f474e27f5b91b1e8f5242402
BLAKE2b-256 efeb25fca88bc9ee109cc16c6584b67d60548aa5a0ae3e77cc4e6663d47ee8a8

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