swaggerpy package

Submodules

Module contents

Swagger processing libraries.

More information on Swagger can be found on the Swagger website <https://developers.helloreverb.com/swagger/>

class swaggerpy.Loader(http_client, processors=None)[source]

Bases: object

Abstraction for loading Swagger API’s.

Parameters
load_api_declaration(base_url, api_dict)[source]

Load an API declaration file.

api_dict is modified with the results of the load:
  • [‘url’] = URL api declaration was loaded from

  • [‘api_declaration’] = Parsed results of the load

Parameters
  • base_url – Base URL to load from

  • api_dict – api object from resource listing.

load_resource_listing(resources_url, base_url=None)[source]

Load a resource listing, loading referenced API declarations.

The following fields are added to the resource listing object model.
  • [‘url’] = URL resource listing was loaded from

  • The [‘apis’] array is modified according to load_api_declaration()

The Loader’s processors are applied to the fully loaded resource listing.

Parameters
  • resources_url – File name for resources.json

  • base_url – Optional URL to be the base URL for finding API declarations. If not specified, ‘basePath’ from the resource listing is used.

process_resource_listing(resources)[source]

Apply processors to a resource listing.

Parameters

resources – Resource listing to process.

class swaggerpy.SwaggerClient(url_or_resource, http_client=None)[source]

Bases: object

Client object for accessing a Swagger-documented RESTful service.

Parameters
  • url_or_resource (dict or str) – Either the parsed resource listing+API decls, or its URL.

  • http_client (HttpClient) – HTTP client API

close()[source]

Close the SwaggerClient, and underlying resources.

get_resource(name)[source]

Gets a Swagger resource by name.

Parameters

name – Name of the resource to get

Return type

Resource

Returns

Resource, or None if not found.

exception swaggerpy.SwaggerError(msg, context, cause=None)[source]

Bases: Exception

Raised when an error is encountered mapping the JSON objects into the model.

class swaggerpy.SwaggerProcessor[source]

Bases: object

Post processing interface for Swagger API’s.

This processor can add fields to model objects for additional information to use in the templates.

apply(resources)[source]

Apply this processor to a loaded Swagger definition.

Parameters

resources (dict) – Top level Swagger definition.

process_api_declaration(resources, resource, context)[source]

Post process a resource object.

This is parsed from a .json file reference by a resource listing’s ‘api’ array.

Parameters
  • resources – Resource listing object

  • resource – resource object.

  • context (ParsingContext) – Current context in the API.

process_error_response(resources, resource, api, operation, error_response, context)[source]

Post process an errorResponse on an operation.

Parameters
  • resources – Resource listing object

  • resource – resource object.

  • api – API object

  • operation – Operation object.

  • error_response – Response object.

  • context (ParsingContext) – Current context in the API.

process_model(resources, resource, model, context)[source]

Post process a model from a resources model dictionary.

Parameters
  • resources – Resource listing object

  • resource – resource object.

  • model – Model object.

  • context (ParsingContext) – Current context in the API.

process_operation(resources, resource, api, operation, context)[source]

Post process an operation on an api.

Parameters
  • resources – Resource listing object

  • resource – resource object.

  • api – API object

  • operation – Operation object.

  • context (ParsingContext) – Current context in the API.

process_parameter(resources, resource, api, operation, parameter, context)[source]

Post process a parameter on an operation.

Parameters
  • resources – Resource listing object

  • resource – resource object.

  • api – API object

  • operation – Operation object.

  • parameter – Parameter object.

  • context (ParsingContext) – Current context in the API.

process_property(resources, resource, model, prop, context)[source]

Post process a property from a model.

Parameters
  • resources – Resource listing object

  • resource – resource object.

  • model – Model object.

  • prop – Property object.

  • context (ParsingContext) – Current context in the API.

process_resource_api(resources, resource, api, context)[source]

Post process entries in a resource’s api array

Parameters
  • resources – Resource listing object

  • resource – resource object.

  • api – API object

  • context (ParsingContext) – Current context in the API.

process_resource_listing(resources, context)[source]

Post process a resources.json object.

Parameters
  • resources – ResourceApi object.

  • context (ParsingContext) – Current context in the API.

process_resource_listing_api(resources, listing_api, context)[source]

Post process entries in a resource.json’s api array.

Parameters
  • resources – Resource listing object

  • listing_api – ResourceApi object.

  • context (ParsingContext) – Current context in the API.

swaggerpy.load_file(resource_listing_file, http_client=None, processors=None)[source]

Loads a resource listing file, applying the given processors.

Parameters
  • http_client – HTTP client interface.

  • resource_listing_file – File name for a resource listing.

  • processors – List of SwaggerProcessors to apply to the resulting resource.

Returns

Processed object model from

Raise

IOError: On error reading api-docs.

swaggerpy.load_json(resource_listing, http_client=None, processors=None)[source]

Process a resource listing that has already been parsed.

Parameters
  • resource_listing (dict) – Parsed resource listing.

  • http_client

  • processors

Returns

Processed resource listing.

swaggerpy.load_url(resource_listing_url, http_client=None, processors=None, base_url=None)[source]

Loads a resource listing, applying the given processors.

Parameters
  • resource_listing_url – URL for a resource listing.

  • http_client – HTTP client interface.

  • processors – List of SwaggerProcessors to apply to the resulting resource.

  • base_url – Optional URL to be the base URL for finding API declarations. If not specified, ‘basePath’ from the resource listing is used.

Returns

Processed object model from

Raise

IOError, URLError: On error reading api-docs.