Package: aioswagger11
Aboutaioswagger11 is an asyncio-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.Aioswagger11 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 usingSwaggerdefined 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.TheSwagger
specificationdefines
how APIs may be described using Swagger.UsageInstall the latest release from PyPI.$ sudo pip install aioswagger11Or install from source using thesetup.pyscript.$ sudo ./setup.py installAPIaioswagger11 will dynamically build an object model from a Swagger-enabled
RESTful API.Here is a simple example using theAsterisk REST
Interface#!/usr/bin/env python3importjsonimportasyncioimportaiohttpfromaioswagger11.clientimportSwaggerClientfromaioswagger11.http_clientimportAsynchronousHttpClienthttp_client=AsynchronousHttpClient()http_client.set_api_key('localhost','hey:peekaboo')asyncdefrun(ari,msg_json):channelId=msg_json['channel']['id']awaitari.channels.answer(channelId=channelId)awaitari.channels.play(channelId=channelId,media='sound:hello-world')# In a real program you should wait for the PlaybackFinished event insteadawaitasyncio.sleep(3)awaitari.channels.continueInDialplan(channelId=channelId)asyncdefmain():ari=SwaggerClient("http://localhost:8088/ari/api-docs/resources.json",http_client=http_client)ws=ari.events.eventWebsocket(app='hello')asyncformsg_strinws:ifmsg.typein{aiohttp.WSMsgType.CLOSED,aiohttp.WSMsgType.CLOSING}:breakelifmsg.type!=aiohttp.WSMsgType.TEXT:continue# ignoremsg_json=json.loads(msg_str)ifmsg_json['type']=='StasisStart':asyncio.ensure_future(run(ari,msg_json))if__name__=="__main__":loop=asyncio.get_event_loop()loop.run_until_complete(main())Data modelThe data model presented by theswagger_modelmodule 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_authoror_copyrightfield), 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 listingThefileandbase_dirfields have been added, referencing the
original.jsonfile.The objects in aresource_listing’sapiarray contains a
fieldapi_declaration, which is the processed result from the
referenced API doc.API declarationAfilefield has been added, referencing the original.jsonfile.DevelopmentThe code is documented usingSphinx, which
allowsIntelliJ IDEAto 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/activateSetuptoolsis used for
building.Pytestis used
for unit testing, with thecoverageplugin installed to
generated code coverage reports. Pass--with-coverageto generate
the code coverage report. HTML versions of the reports are put incover/index.html.$ ./setup.py develop   # prep for development (install deps, launchers, etc.)
$ ./setup.py pytest    # run unit tests
$ ./setup.py bdist_egg # build distributableTestingSimply runpython3 setup.py pytest.Note that testing this module requires a version of httpretty that’s been
fixed to work with aiohttp.LicenseCopyright (c) 2013, Digium, Inc.
Copyright (c) 2018, Matthias Urlichsaioswagger11 is licensed with aBSD 3-Clause
License.The current author humbly requests that you share any further bug fixes or
enhancements to this code.