OpenAPI Codec for Core API

1.3.2 · abandoned · verified Thu Apr 16

OpenAPI-codec is a Python library that functions as a codec for the OpenAPI schema format (also known as Swagger) within the Core API framework. It enables Python clients to interact with services exposing OpenAPI schemas and automatically integrates with the Core API command-line client upon installation. The project's last update on GitHub was 7 years ago, and its latest release on PyPI was in July 2017, indicating that it is no longer actively maintained.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a Core API client with the `OpenAPICodec` to interact with services exposing OpenAPI schemas. It highlights the need to explicitly specify `format='openapi'` if the server does not correctly use the `application/openapi+json` content type.

from openapi_codec import OpenAPICodec
from coreapi.codecs import JSONCodec
from coreapi import Client

# Configure decoders for the Core API client
decoders = [OpenAPICodec(), JSONCodec()]
client = Client(decoders=decoders)

# Example of fetching a schema that might require explicit format if content-type is generic
# Use a public OpenAPI schema URL for demonstration, e.g., petstore.swagger.io
# Note: This URL might return 'application/json' so format='openapi' is crucial.
# Ensure COREAPI_FORMAT_OPENAPI is set in your environment or passed directly.
# For a live example, you might need a service that truly serves 'application/openapi+json'.
# This code assumes a valid OpenAPI JSON is returned at the URL.

try:
    # Simulate a fetch, if direct URL doesn't work, consider a local file or mock.
    # For a real API, replace with the actual URL and handle potential authentication.
    document = client.get('http://petstore.swagger.io/v2/swagger.json', format='openapi')
    print('Successfully decoded OpenAPI document:')
    print(document.title)
    print(f'Number of endpoints: {len(document.data)}')
except Exception as e:
    print(f'Error decoding document: {e}')
    print('Ensure the URL serves a valid OpenAPI schema and Core API/openapi-codec are installed.')

view raw JSON →