Bravado

12.0.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

Initializes a SwaggerClient from a public OpenAPI/Swagger specification URL and demonstrates a simple GET request. It also shows how to perform a POST request and mentions optional configuration to disable validation or receive responses as dictionaries instead of dynamic models.

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')}")

view raw JSON →