connexion

Connexion is a framework that automagically handles HTTP requests based on OpenAPI Specification (formerly known as Swagger Spec) of your API described in YAML format. Connexion allows you to write an OpenAPI specification, then maps the endpoints to your Python functions; this makes it unique, as many tools generate the specification based on your Python code. You can describe your REST API in as much detail as you want; then Connexion guarantees that it will work as you specified.

Subpackages

Submodules

Package Contents

Classes

AbstractAPI

Defines an abstract interface for a Swagger API

AbstractApp

param import_name:

the name of the application package

Resolution

Represents the result of operation resolution

Resolver

Standard resolver

RestyResolver

Resolves endpoint functions using REST semantics (unless overridden by specifying operationId)

Functions

problem(status, title, detail, type=None, instance=None, headers=None, ext=None)

Returns a Problem Details error response.

not_installed_error(exc)

Raises the ImportError when the module/object is actually called.

Attributes

NoContent

App

Api

__version__

class connexion.AbstractAPI(specification, base_path=None, arguments=None, validate_responses=False, strict_validation=False, resolver=None, debug=False, resolver_error_handler=None, validator_map=None, pythonic_params=False, options=None, **kwargs)

Bases: AbstractRoutingAPI

Defines an abstract interface for a Swagger API

Parameters:
  • validator_map (dict) – Custom validators for the types “parameter”, “body” and “response”.

  • pythonic_params (bool) – When True CamelCase parameters are converted to snake_case and an underscore is appended to any shadowed built-ins

add_operation(self, path, method)

Adds one operation to the api.

This method uses the OperationID identify the module and function that will handle the operation

From Swagger Specification:

OperationID

A friendly name for the operation. The id MUST be unique among all operations described in the API. Tools and libraries MAY use the operation id to uniquely identify an operation.

abstract classmethod get_request(self, *args, **kwargs)

This method converts the user framework request to a ConnexionRequest.

abstract classmethod get_response(self, response, mimetype=None, request=None)

This method converts a handler response to a framework response. This method should just retrieve response from handler then call cls._get_response. :param response: A response to cast (tuple, framework response, etc). :param mimetype: The response mimetype. :type mimetype: Union[None, str] :param request: The request associated with this response (the user framework request).

classmethod get_connexion_response(cls, response, mimetype=None)

Cast framework dependent response to ConnexionResponse used for schema validation

json_loads(self, data)
add_paths(self, paths: Optional[dict] = None) None

Adds the paths defined in the specification as endpoints

class connexion.AbstractApp(import_name, api_cls, port=None, specification_dir='', host=None, server=None, server_args=None, arguments=None, auth_all_paths=False, debug=None, resolver=None, options=None, skip_error_handlers=False, middlewares=None)
Parameters:
  • import_name (str) – the name of the application package

  • host (str) – the host interface to bind on.

  • port (int) – port to listen to

  • specification_dir (pathlib.Path | str) – directory where to look for specifications

  • server (str | None) – which wsgi server to use

  • server_args (dict | None) – dictionary of arguments which are then passed to appropriate http server (Flask or aio_http)

  • arguments (dict | None) – arguments to replace on the specification

  • auth_all_paths (bool) – whether to authenticate not defined paths

  • debug (bool) – include debugging information

  • resolver – Callable that maps operationID to a function

  • middlewares (list | None) – Callable that maps operationID to a function

abstract create_app(self)

Creates the user framework application

abstract get_root_path(self)

Gets the root path of the user framework application

abstract set_errors_handlers(self)

Sets all errors handlers of the user framework application

add_api(self, specification, base_path=None, arguments=None, auth_all_paths=None, validate_responses=False, strict_validation=False, resolver=None, resolver_error=None, pythonic_params=False, options=None, validator_map=None)

Adds an API to the application based on a swagger file or API dict

Parameters:
  • specification (pathlib.Path or str or dict) – swagger file with the specification | specification dict

  • base_path (str | None) – base path where to add this api

  • arguments (dict | None) – api version specific arguments to replace on the specification

  • auth_all_paths (bool) – whether to authenticate not defined paths

  • validate_responses (bool) – True enables validation. Validation errors generate HTTP 500 responses.

  • strict_validation (bool) – True enables validation on invalid request parameters

  • resolver (Resolver | types.FunctionType) – Operation resolver.

  • resolver_error (int | None) – If specified, turns ResolverError into error responses with the given status code.

  • pythonic_params (bool) – When True CamelCase parameters are converted to snake_case

  • options (dict | None) – New style options dictionary.

  • validator_map (dict) – map of validators

Return type:

AbstractAPI

add_url_rule(self, rule, endpoint=None, view_func=None, **options)

Connects a URL rule. Works exactly like the route decorator. If a view_func is provided it will be registered with the endpoint.

Basically this example:

@app.route('/')
def index():
    pass

Is equivalent to the following:

def index():
    pass
app.add_url_rule('/', 'index', index)

If the view_func is not provided you will need to connect the endpoint to a view function like so:

app.view_functions['index'] = index

Internally`route` invokes add_url_rule so if you want to customize the behavior via subclassing you only need to change this method.

Parameters:
  • rule (str) – the URL rule as string

  • endpoint (str) – the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpoint

  • view_func (types.FunctionType) – the function to call when serving a request to the provided endpoint

  • options – the options to be forwarded to the underlying werkzeug.routing.Rule object. A change to Werkzeug is handling of method options. methods is a list of methods this rule should be limited to (GET, POST etc.). By default a rule just listens for GET (and implicitly HEAD).

route(self, rule, **options)

A decorator that is used to register a view function for a given URL rule. This does the same thing as add_url_rule but is intended for decorator usage:

@app.route('/')
def index():
    return 'Hello World'
Parameters:
  • rule (str) – the URL rule as string

  • endpoint – the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpoint

  • options – the options to be forwarded to the underlying werkzeug.routing.Rule object. A change to Werkzeug is handling of method options. methods is a list of methods this rule should be limited to (GET, POST etc.). By default a rule just listens for GET (and implicitly HEAD).

abstract run(self, port=None, server=None, debug=None, host=None, **options)

Runs the application on a local development server. :param host: the host interface to bind on. :type host: str :param port: port to listen to :type port: int :param server: which wsgi server to use :type server: str | None :param debug: include debugging information :type debug: bool :param options: options to be forwarded to the underlying server

connexion.NoContent
exception connexion.ProblemException(status=400, title=None, detail=None, type=None, instance=None, headers=None, ext=None)

Bases: ConnexionException

Common base class for all non-exit exceptions.

This exception holds arguments that are going to be passed to the connexion.problem function to generate a proper response.

class __cause__

exception cause

class __context__

exception context

class __suppress_context__
class __traceback__
class args
to_problem(self)
__delattr__()

Implement delattr(self, name).

__dir__()

Default dir() implementation.

__eq__()

Return self==value.

__format__()

Default object formatter.

__ge__()

Return self>=value.

__getattribute__()

Return getattr(self, name).

__gt__()

Return self>value.

__hash__()

Return hash(self).

__le__()

Return self<=value.

__lt__()

Return self<value.

__ne__()

Return self!=value.

__reduce__()

Helper for pickle.

__reduce_ex__()

Helper for pickle.

__repr__()

Return repr(self).

__setattr__()

Implement setattr(self, name, value).

__setstate__()
__sizeof__()

Size of object in memory, in bytes.

__str__()

Return str(self).

__subclasshook__()

Abstract classes can override this to customize issubclass().

This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

connexion.problem(status, title, detail, type=None, instance=None, headers=None, ext=None)

Returns a Problem Details error response.

Parameters:
  • status (int) – The HTTP status code generated by the origin server for this occurrence of the problem.

  • title (str) – A short, human-readable summary of the problem type. It SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localisation.

  • detail (str) – An human readable explanation specific to this occurrence of the problem.

  • type – An absolute URI that identifies the problem type. When dereferenced, it SHOULD provide human-readable documentation for the problem type (e.g., using HTML). When this member is not present its value is assumed to be “about:blank”.

  • instance (str) – An absolute URI that identifies the specific occurrence of the problem. It may or may not yield further information if dereferenced.

  • headers (dict | None) – HTTP headers to include in the response

  • ext (dict | None) – Extension members to include in the body

Type:

type: str

Returns:

error response

Return type:

ConnexionResponse

class connexion.Resolution(function, operation_id)

Represents the result of operation resolution

Parameters:

function (types.FunctionType) – The endpoint function

class connexion.Resolver(function_resolver=utils.get_function_from_name)

Standard resolver

Parameters:

function_resolver (types.FunctionType) – Function that resolves functions using an operationId

resolve(self, operation)

Default operation resolver

resolve_operation_id(self, operation)

Default operationId resolver

resolve_function_from_operation_id(self, operation_id)

Invokes the function_resolver

class connexion.RestyResolver(default_module_name, collection_endpoint_name='search')

Bases: Resolver

Resolves endpoint functions using REST semantics (unless overridden by specifying operationId)

Parameters:

default_module_name (str) – Default module name for operations

resolve_operation_id(self, operation)

Resolves the operationId using REST semantics unless explicitly configured in the spec

resolve_operation_id_using_rest_semantics(self, operation)

Resolves the operationId using REST semantics

resolve(self, operation)

Default operation resolver

resolve_function_from_operation_id(self, operation_id)

Invokes the function_resolver

connexion.not_installed_error(exc)

Raises the ImportError when the module/object is actually called.

connexion.App
connexion.Api
connexion.__version__ = 3.0.dev0