Giter Club home page Giter Club logo

swagger-py's Introduction

About

Swagger.py is a Python library for using Swagger defined API's.

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 API's may be described using Swagger.

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

Usage

Install the latest release from PyPI.

$ sudo pip install swaggerpy

Or install from source using the setup.py script.

$ sudo ./setup.py install

API

Swagger.py 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 python

import json

from swaggerpy.client import SwaggerClient
from swaggerpy.http_client import SynchronousHttpClient

http_client = SynchronousHttpClient()
http_client.set_basic_auth('localhost', 'hey', 'peekaboo')

ari = SwaggerClient(
    "http://localhost:8088/ari/api-docs/resources.json",
    http_client=http_client)

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

for msg_str in iter(lambda: ws.recv(), None):
    msg_json = json.loads(msg_str)
    if msg_json['type'] == 'StasisStart':
        channelId = msg_json['channel']['id']
        ari.channels.answer(channelId=channelId)
        ari.channels.play(channelId=channelId,
                          media='sound:hello-world')
        ari.channels.continueInDialplan(channelId=channelId)

swagger-codegen

There are the beginnings of a Mustache-based code generator, but it's not functional... yet.

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. Nose 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 nosetests # run unit tests
$ ./setup.py bdist_egg # build distributable

License

Copyright (c) 2013, Digium, Inc. All rights reserved.

Swagger.py is licensed with a BSD 3-Clause License.

swagger-py's People

Contributors

darrensessions avatar leedm777 avatar matt-jordan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.