bravado Package¶
bravado
Package¶
client
Module¶
The SwaggerClient
provides an interface for making API calls based on
a swagger spec, and returns responses of python objects which build from the
API response.
Structure Diagram:
+---------------------+
| |
| SwaggerClient |
| |
+------+--------------+
|
| has many
|
+------v--------------+
| |
| Resource +------------------+
| | |
+------+--------------+ has many |
| |
| has many |
| |
+------v--------------+ +------v--------------+
| | | |
| Operation | | SwaggerModel |
| | | |
+------+--------------+ +---------------------+
|
| uses
|
+------v--------------+
| |
| HttpClient |
| |
+---------------------+
To get a client
client = bravado.client.SwaggerClient.from_url(swagger_spec_url)
-
class
bravado.client.
ResourceDecorator
(resource, also_return_response=False)¶ Bases:
object
Wraps
bravado_core.resource.Resource
so that accesses to contained operations can be instrumented.
-
class
bravado.client.
SwaggerClient
(swagger_spec, also_return_response=False)¶ Bases:
object
A client for accessing a Swagger-documented RESTful service.
-
classmethod
from_spec
(spec_dict, origin_url=None, http_client=None, config=None)¶ Build a
SwaggerClient
from a Swagger spec in dict form.Parameters: - spec_dict – a dict with a Swagger spec in json-like form
- origin_url (str) – the url used to retrieve the spec_dict
- config – Configuration dict - see spec.CONFIG_DEFAULTS
Return type: bravado_core.spec.Spec
-
classmethod
from_url
(spec_url, http_client=None, request_headers=None, config=None)¶ Build a
SwaggerClient
from a url to the Swagger specification for a RESTful API.Parameters: - spec_url (str) – url pointing at the swagger API specification
- http_client (
bravado.http_client.HttpClient
) – an HTTP client used to perform requests - request_headers (dict) – Headers to pass with http requests
- config – Config dict for bravado and bravado_core. See CONFIG_DEFAULTS in :module:`bravado_core.spec`. See CONFIG_DEFAULTS in :module:`bravado.client`.
Return type: bravado_core.spec.Spec
-
get_model
(model_name)¶
-
classmethod
-
bravado.client.
construct_params
(operation, request, op_kwargs)¶ Given the parameters passed to the operation invocation, validates and marshals the parameters into the provided request dict.
Parameters: op_kwargs – the kwargs passed to the operation invocation Raises: SwaggerMappingError on extra parameters or when a required parameter is not supplied.
-
bravado.client.
construct_request
(operation, request_options, **op_kwargs)¶ Construct the outgoing request dict.
Parameters: - request_options – _request_options passed into the operation invocation.
- op_kwargs – parameter name/value pairs to passed to the invocation of the operation.
Returns: request in dict form
-
bravado.client.
inject_headers_for_remote_refs
(request_callable, request_headers)¶ Inject request_headers only when the request is to retrieve the remote refs in the swagger spec (vs being a request for a service call).
Parameters: - request_callable – method on http_client to make a http request
- request_headers – headers to inject when retrieving remote refs
config
Module¶
-
class
bravado.config.
BravadoConfig
¶ Bases:
bravado.config.BravadoConfig
-
static
from_config_dict
(config)¶
-
static
requests_client
Module¶
-
class
bravado.requests_client.
ApiKeyAuthenticator
(host, api_key, param_name=u'api_key', param_in=u'query')¶ Bases:
bravado.requests_client.Authenticator
?api_key authenticator.
This authenticator adds an API key via query parameter or header.
Parameters: - host – Host to authenticate for.
- api_key – API key.
- param_name – Query parameter specifying the API key.
- param_in – How to send the API key. Can be ‘query’ or ‘header’.
-
apply
(request)¶ Apply authentication to a request.
Parameters: request – Request to add authentication information to.
-
class
bravado.requests_client.
Authenticator
(host)¶ Bases:
object
Authenticates requests.
Parameters: host – Host to authenticate for. -
apply
(request)¶ Apply authentication to a request.
Parameters: request – Request to add authentication information to.
-
matches
(url)¶ Returns true if this authenticator applies to the given url.
Parameters: url – URL to check. Returns: True if matches host, port and scheme, False otherwise.
-
-
class
bravado.requests_client.
BasicAuthenticator
(host, username, password)¶ Bases:
bravado.requests_client.Authenticator
HTTP Basic authenticator.
Parameters: - host – Host to authenticate for.
- username – Username.
- password – Password
-
apply
(request)¶ Apply authentication to a request.
Parameters: request – Request to add authentication information to.
-
class
bravado.requests_client.
RequestsClient
¶ Bases:
bravado.http_client.HttpClient
Synchronous HTTP client implementation.
-
apply_authentication
(request)¶
-
authenticated_request
(request_params)¶
-
request
(request_params, operation=None, request_config=None)¶ Parameters: - request_params (dict) – complete request data.
- operation (
bravado_core.operation.Operation
) – operation that this http request is for. Defaults to None - in which case, we’re obviously just retrieving a Swagger Spec. - request_config (RequestConfig) – per-request configuration
Returns: HTTP Future object
Return type: class: bravado_core.http_future.HttpFuture
-
static
separate_params
(request_params)¶ Splits the passed in dict of request_params into two buckets.
- sanitized_params are valid kwargs for constructing a requests.Request(..)
- misc_options are things like timeouts which can’t be communicated to the Requests library via the requests.Request(…) constructor.
Parameters: request_params – kitchen sink of request params. Treated as a read-only dict. Returns: tuple(sanitized_params, misc_options)
-
set_api_key
(host, api_key, param_name=u'api_key', param_in=u'query')¶
-
set_basic_auth
(host, username, password)¶
-
-
class
bravado.requests_client.
RequestsFutureAdapter
(session, request, misc_options)¶ Bases:
bravado.http_future.FutureAdapter
Mimics a
concurrent.futures.Future
for the purposes of making HTTP calls with the Requests library in a future-y sort of way.-
build_timeout
(result_timeout)¶ Build the appropriate timeout object to pass to session.send(…) based on connect_timeout, the timeout passed to the service call, and the timeout passed to the result call.
Parameters: result_timeout – timeout that was passed into future.result(..) Returns: timeout Return type: float or tuple(connect_timeout, timeout)
-
result
(timeout=None)¶ Blocking call to wait for API response
Parameters: timeout (float) – timeout in seconds to wait for response. Defaults to None to wait indefinitely. Returns: raw response from the server Return type: dict
-
timeout_errors
= [<class 'requests.exceptions.ReadTimeout'>]¶
-
-
class
bravado.requests_client.
RequestsResponseAdapter
(requests_lib_response)¶ Bases:
bravado_core.response.IncomingResponse
Wraps a requests.models.Response object to provide a uniform interface to the response innards.
-
headers
¶
-
json
(**kwargs)¶ Returns: response content in a json-like form Return type: int, float, double, string, unicode, list, dict
-
raw_bytes
¶
-
reason
¶
-
status_code
¶
-
text
¶
-
fido_client
Module¶
http_future
Module¶
-
class
bravado.http_future.
FutureAdapter
¶ Bases:
object
Mimics a
concurrent.futures.Future
regardless of which client is performing the request, whether it is synchronous or actually asynchronous.This adapter must be implemented by all bravado clients such as FidoClient or RequestsClient to wrap the object returned by their ‘request’ method.
-
result
(timeout=None)¶ Must implement a result method which blocks on result retrieval.
Parameters: timeout – maximum time to wait on result retrieval. Defaults to None which means blocking undefinitely.
-
timeout_errors
= []¶
-
-
class
bravado.http_future.
HttpFuture
(future, response_adapter, operation=None, request_config=None)¶ Bases:
object
Wrapper for a
FutureAdapter
that returns an HTTP response.Parameters: - future – The future object to wrap.
- response_adapter (type that is a subclass of
bravado_core.response.IncomingResponse
.) – Adapter type which exposes the innards of the HTTP response in a non-http client specific way. - request_config (RequestConfig) – See
bravado.config.RequestConfig
andbravado.client.REQUEST_OPTIONS_DEFAULTS
-
response
(timeout=None, fallback_result=None, exceptions_to_catch=(<class 'bravado.exception.BravadoTimeoutError'>, <class 'bravado.exception.HTTPServerError'>))¶ Blocking call to wait for the HTTP response.
Parameters: - timeout (float) – Number of seconds to wait for a response. Defaults to None which means wait indefinitely.
- fallback_result (callable that takes an exception and returns a fallback swagger result) – callable that accepts an exception as argument and returns the swagger result to use in case of errors
- exceptions_to_catch (List/Tuple of Exception classes.) – Exception classes to catch and call fallback_result with. Has no effect if fallback_result is not provided. By default, fallback_result will be called for read timeout and server errors (HTTP 5XX).
Returns: A BravadoResponse instance containing the swagger result and response metadata.
WARNING: This interface is considered UNSTABLE. Backwards-incompatible API changes may occur; use at your own risk.
-
result
(timeout=None)¶ DEPRECATED: please use the response() method instead.
Blocking call to wait for and return the unmarshalled swagger result.
Parameters: timeout (float) – Number of seconds to wait for a response. Defaults to None which means wait indefinitely. Returns: Depends on the value of also_return_response sent in to the constructor.
-
bravado.http_future.
raise_on_expected
(http_response)¶ Raise an HTTPError if the response is non-2XX and matches a response in the swagger spec.
Parameters: http_response – bravado_core.response.IncomingResponse
Raises: HTTPError
-
bravado.http_future.
raise_on_unexpected
(http_response)¶ Raise an HTTPError if the response is 5XX.
Parameters: http_response – bravado_core.response.IncomingResponse
Raises: HTTPError
-
bravado.http_future.
reraise_errors
(func)¶
-
bravado.http_future.
unmarshal_response
(incoming_response, operation, response_callbacks=None)¶ So the http_client is finished with its part of processing the response. This hands the response over to bravado_core for validation and unmarshalling and then runs any response callbacks. On success, the swagger_result is available as
incoming_response.swagger_result
. :type incoming_response:bravado_core.response.IncomingResponse
:type operation:bravado_core.operation.Operation
:type response_callbacks: list of callable. Seebravado_core.client.REQUEST_OPTIONS_DEFAULTS.Raises: HTTPError - On 5XX status code, the HTTPError has minimal information. - On non-2XX status code with no matching response, the HTTPError
contains a detailed error message.
- On non-2XX status code with a matching response, the HTTPError
- contains the return value.
-
bravado.http_future.
unmarshal_response_inner
(response, op)¶ Unmarshal incoming http response into a value based on the response specification. :type response:
bravado_core.response.IncomingResponse
:type op:bravado_core.operation.Operation
:returns: value where type(value) matches response_spec[‘schema’][‘type’]if it exists, None otherwise.
response
Module¶
-
class
bravado.response.
BravadoResponse
(result, metadata)¶ Bases:
object
Bravado response object containing the swagger result as well as response metadata.
WARNING: This interface is considered UNSTABLE. Backwards-incompatible API changes may occur; use at your own risk.
Variables: - result – Swagger result from the server
- metadata (BravadoResponseMetadata) – metadata for this response including HTTP response
-
incoming_response
¶
-
class
bravado.response.
BravadoResponseMetadata
(incoming_response, swagger_result, start_time, request_end_time, handled_exception_info, request_config)¶ Bases:
object
HTTP response metadata.
NOTE: The elapsed_time attribute might be slightly lower than the actual time spent since calling the operation object, as we only start measuring once the call to HTTPClient.request returns. Nevertheless, it should be accurate enough for logging and debugging, i.e. determining what went on and how much time was spent waiting for the response.
WARNING: This interface is considered UNSTABLE. Backwards-incompatible API changes may occur; use at your own risk.
Variables: - start_time (float) – monotonic timestamp at which the future was created
- request_end_time (float) – monotonic timestamp at which we received the HTTP response
- processing_end_time (float) – monotonic timestamp at which processing the response ended
- handled_exception_info (tuple) – 3-tuple of exception class, exception instance and string representation of the traceback in case an exception was caught during request processing.
-
elapsed_time
¶
-
headers
¶
-
incoming_response
¶
-
is_fallback_result
¶
-
request_elapsed_time
¶
-
status_code
¶
exception
Module¶
-
exception
bravado.exception.
BravadoTimeoutError
¶ Bases:
exceptions.Exception
-
exception
bravado.exception.
ForcedFallbackResultError
¶ Bases:
exceptions.Exception
This exception will be handled if the option to force returning a fallback result is used.
-
exception
bravado.exception.
HTTPBadGateway
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPServerError
HTTP/502 - Bad Gateway
-
status_code
= 502¶
-
-
exception
bravado.exception.
HTTPBadRequest
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/400 - Bad Request
-
status_code
= 400¶
-
-
exception
bravado.exception.
HTTPClientError
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPError
4xx responses.
-
exception
bravado.exception.
HTTPConflict
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/409 - Conflict
-
status_code
= 409¶
-
-
exception
bravado.exception.
HTTPError
(response, message=None, swagger_result=None)¶ Bases:
exceptions.IOError
Unified HTTPError used across all http_client implementations.
-
class
bravado.exception.
HTTPErrorType
¶ Bases:
type
A metaclass for registering HTTPError subclasses.
-
exception
bravado.exception.
HTTPExpectationFailed
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/417 - Expectation Failed
-
status_code
= 417¶
-
-
exception
bravado.exception.
HTTPFailedDependency
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/424 - Failed Dependency
-
status_code
= 424¶
-
-
exception
bravado.exception.
HTTPForbidden
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/403 - Forbidden
-
status_code
= 403¶
-
-
exception
bravado.exception.
HTTPFound
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPRedirection
HTTP/302 - Found
-
status_code
= 302¶
-
-
exception
bravado.exception.
HTTPGatewayTimeout
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPServerError
HTTP/504 - Gateway Timeout
-
status_code
= 504¶
-
-
exception
bravado.exception.
HTTPGone
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/410 - Gone
-
status_code
= 410¶
-
-
exception
bravado.exception.
HTTPHTTPVersionNotSupported
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPServerError
HTTP/505 - HTTP Version Not Supported
-
status_code
= 505¶
-
-
exception
bravado.exception.
HTTPInsufficientStorage
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPServerError
HTTP/507 - Insufficient Storage
-
status_code
= 507¶
-
-
exception
bravado.exception.
HTTPInternalServerError
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPServerError
HTTP/500 - Internal Server Error
-
status_code
= 500¶
-
-
exception
bravado.exception.
HTTPLengthRequired
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/411 - Length Required
-
status_code
= 411¶
-
-
exception
bravado.exception.
HTTPLocked
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/423 - Locked
-
status_code
= 423¶
-
-
exception
bravado.exception.
HTTPLoopDetected
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPServerError
HTTP/508 - Loop Detected
-
status_code
= 508¶
-
-
exception
bravado.exception.
HTTPMethodNotAllowed
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/405 - Method Not Allowed
-
status_code
= 405¶
-
-
exception
bravado.exception.
HTTPMisdirectedRequest
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/421 - Misdirected Request
-
status_code
= 421¶
-
-
exception
bravado.exception.
HTTPMovedPermanently
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPRedirection
HTTP/301 - Moved Permanently
-
status_code
= 301¶
-
-
exception
bravado.exception.
HTTPMultipleChoices
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPRedirection
HTTP/300 - Multiple Choices
-
status_code
= 300¶
-
-
exception
bravado.exception.
HTTPNetworkAuthenticationRequired
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPServerError
HTTP/511 - Network Authentication Required
-
status_code
= 511¶
-
-
exception
bravado.exception.
HTTPNotAcceptable
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/406 - Not Acceptable
-
status_code
= 406¶
-
-
exception
bravado.exception.
HTTPNotExtended
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPServerError
HTTP/510 - Not Extended
-
status_code
= 510¶
-
-
exception
bravado.exception.
HTTPNotFound
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/404 - Not Found
-
status_code
= 404¶
-
-
exception
bravado.exception.
HTTPNotImplemented
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPServerError
HTTP/501 - Not Implemented
-
status_code
= 501¶
-
-
exception
bravado.exception.
HTTPNotModified
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPRedirection
HTTP/304 - Not Modified
-
status_code
= 304¶
-
-
exception
bravado.exception.
HTTPPayloadTooLarge
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/413 - Payload Too Large
-
status_code
= 413¶
-
-
exception
bravado.exception.
HTTPPaymentRequired
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/402 - Payment Required
-
status_code
= 402¶
-
-
exception
bravado.exception.
HTTPPermanentRedirect
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPRedirection
HTTP/308 - Permanent Redirect
-
status_code
= 308¶
-
-
exception
bravado.exception.
HTTPPreconditionFailed
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/412 - Precondition Failed
-
status_code
= 412¶
-
-
exception
bravado.exception.
HTTPPreconditionRequired
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/428 - Precondition Required
-
status_code
= 428¶
-
-
exception
bravado.exception.
HTTPProxyAuthenticationRequired
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/407 - Proxy Authentication Required
-
status_code
= 407¶
-
-
exception
bravado.exception.
HTTPRangeNotSatisfiable
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/416 - Range Not Satisfiable
-
status_code
= 416¶
-
-
exception
bravado.exception.
HTTPRedirection
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPError
3xx responses.
-
exception
bravado.exception.
HTTPRequestHeaderFieldsTooLarge
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/431 - Request Header Fields Too Large
-
status_code
= 431¶
-
-
exception
bravado.exception.
HTTPRequestTimeout
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/408 - Request Timeout
-
status_code
= 408¶
-
-
exception
bravado.exception.
HTTPSeeOther
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPRedirection
HTTP/303 - See Other
-
status_code
= 303¶
-
-
exception
bravado.exception.
HTTPServerError
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPError
5xx responses.
Bases:
bravado.exception.HTTPServerError
HTTP/503 - Service Unavailable
-
exception
bravado.exception.
HTTPTemporaryRedirect
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPRedirection
HTTP/307 - Temporary Redirect
-
status_code
= 307¶
-
-
exception
bravado.exception.
HTTPTooManyRequests
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/429 - Too Many Requests
-
status_code
= 429¶
-
-
exception
bravado.exception.
HTTPURITooLong
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/414 - URI Too Long
-
status_code
= 414¶
-
Bases:
bravado.exception.HTTPClientError
HTTP/401 - Unauthorized
Bases:
bravado.exception.HTTPClientError
HTTP/451 - Unavailable For Legal Reasons
-
exception
bravado.exception.
HTTPUnprocessableEntity
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/422 - Unprocessable Entity
-
status_code
= 422¶
-
-
exception
bravado.exception.
HTTPUnsupportedMediaType
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/415 - Unsupported Media Type
-
status_code
= 415¶
-
-
exception
bravado.exception.
HTTPUpgradeRequired
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPClientError
HTTP/426 - Upgrade Required
-
status_code
= 426¶
-
-
exception
bravado.exception.
HTTPUseProxy
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPRedirection
HTTP/305 - Use Proxy
-
status_code
= 305¶
-
-
exception
bravado.exception.
HTTPVariantAlsoNegotiates
(response, message=None, swagger_result=None)¶ Bases:
bravado.exception.HTTPServerError
HTTP/506 - Variant Also Negotiates
-
status_code
= 506¶
-
-
bravado.exception.
make_http_exception
(response, message=None, swagger_result=None)¶ Return an HTTP exception class based on the response. If a specific class doesn’t exist for a particular HTTP status code, a more general
HTTPError
class will be returned. :type response:bravado_core.response.IncomingResponse
:param message: Optional string message :param swagger_result: If the response for this HTTPError isdocumented in the swagger spec, then this should be the result value of the response.Returns: An HTTP exception class that can be raised
testing
Module¶
-
class
bravado.testing.response_mocks.
BravadoResponseMock
(result, metadata=None)¶ Bases:
object
Class that behaves like the
HttpFuture.response()
method as well as aBravadoResponse
. Please check the documentation for further information.-
metadata
¶
-
result
¶
-
-
class
bravado.testing.response_mocks.
FallbackResultBravadoResponseMock
(exception=BravadoTimeoutError(), metadata=None)¶ Bases:
object
Class that behaves like the
HttpFuture.response()
method as well as aBravadoResponse
. It will always call thefallback_result
callback that’s passed to theresponse()
method. Please check the documentation for further information.-
metadata
¶
-
result
¶
-
-
class
bravado.testing.response_mocks.
IncomingResponseMock
(status_code, **kwargs)¶ Bases:
bravado_core.response.IncomingResponse