Bravado
Bravado is a Python client library for accessing OpenAPI Specification v2.0 (formerly Swagger) services. It aims to replace code generation by providing a dynamically generated client, supporting both synchronous and asynchronous HTTP requests. The library performs strict validations against the OpenAPI schema for both requests and responses. The current version is 12.0.1, requiring Python 3.8 or newer, and it maintains an active development status with irregular but significant updates.
Warnings
- deprecated The `HttpFuture.result()` method is deprecated. Users should now use `HttpFuture.response().result` to access the unmarshalled Swagger result, or `HttpFuture.response()` to get the full `BravadoResponse` instance which includes both the result and HTTP metadata.
- gotcha Bravado performs strict validation by default on the Swagger/OpenAPI specification itself, as well as on outgoing requests and incoming responses. This can lead to validation errors if the spec is malformed or if requests/responses don't strictly adhere to the schema.
- gotcha Docstrings for operations in Bravado do not behave like standard Python function docstrings and the built-in `help()` function might not work as expected. The docstrings are structured more like class docstrings.
- gotcha By default, network or server errors will raise exceptions (e.g., `BravadoTimeoutError`, `HTTPServerError`). For more graceful error handling, `HttpFuture.response()` supports a `fallback_result` argument.
Install
-
pip install bravado -
pip install bravado[fido] -
pip install bravado-asyncio
Imports
- SwaggerClient
from bravado.client import SwaggerClient
- RequestsClient
from bravado.requests_client import RequestsClient
- FidoClient
from bravado.fido_client import FidoClient
- AsyncioClient
from bravado_asyncio.http_client import AsyncioClient
Quickstart
from bravado.client import SwaggerClient
# Using a publicly available Petstore Swagger/OpenAPI spec
client = SwaggerClient.from_url('http://petstore.swagger.io/v2/swagger.json')
# Make a synchronous API call and get the result
try:
# Using .response().result is the recommended way to get the data
pet = client.pet.getPetById(petId=42).response().result
print(f"Found pet: {pet.name} (ID: {pet.id})")
# Example of a POST call
Pet = client.get_model('Pet')
Category = client.get_model('Category')
new_pet = Pet(id=100, name="Buddy", category=Category(id=1, name="Dogs"), photoUrls=[])
add_result = client.pet.addPet(body=new_pet).response().result
print(f"Added pet: {add_result.name} (ID: {add_result.id})")
except Exception as e:
print(f"An error occurred: {e}")
# Example of disabling validation for a request/response (not recommended for production)
# client_config = {
# 'validate_requests': False,
# 'validate_responses': False,
# 'use_models': False # Get dicts instead of dynamic models
# }
# client_relaxed = SwaggerClient.from_url(
# 'http://petstore.swagger.io/v2/swagger.json',
# config=client_config
# )
# pet_dict = client_relaxed.pet.getPetById(petId=42).response().result
# print(f"Found pet as dict: {pet_dict.get('name')}")