Skip to main content

aiohttp-apispec-plugin

PyPI License: MIT

Lightweight apispec plugin that generates OpenAPI specification for aiohttp web applications.

Installation

pip install aiohttp-apispec-plugin

Examples

With class based view

from aiohttp import web
from aiohttp_apispec_plugin import AioHttpPlugin
from apispec import APISpec

class UserView(web.View):
    async def get(self) -> web.Response:
        """User Detail View
        ---
        summary: Get User
        description: Get User Data For Given `user_id`
        parameters:
        - name: user_id
          in: path
          description: User ID
          required: true
          schema:
            type: string
        responses:
          200:
            description: Successfully retrieved user details
            content:
              application/json:
                schema:
                  properties:
                    id:
                      type: integer
                    username:
                      type: string
                    first_name:
                      type: string
                    last_name:
                      type: string
        """

app = web.Application()
app.router.add_view("/api/v1/users/{user_id}", UserView)

# Create an APISpec
spec = APISpec(
    title="AioHttp Application",
    version="1.0.0",
    openapi_version="3.0.3",
    plugins=[
        AioHttpPlugin(app),
    ],
)

spec.path(resource=UserView)
print(spec.to_yaml())
"""
info:
  title: AioHttp Application
  version: 1.0.0
openapi: 3.0.3
paths:
  /api/v1/users/{user_id}:
    get:
      description: Get User Data For Given `user_id`
      parameters:
      - description: User ID
        in: path
        name: user_id
        required: true
        schema:
          type: string
      responses:
        '200':
          content:
            application/json:
              schema:
                properties:
                  first_name:
                    type: string
                  id:
                    type: integer
                  last_name:
                    type: string
                  username:
                    type: string
          description: Successfully retrieved user details
      summary: Get User
"""

With function based view

from aiohttp import web
from aiohttp_apispec_plugin import AioHttpPlugin
from apispec import APISpec

async def get_user(request: web.Request) -> web.Response:
    """User Detail View
    ---
    summary: Get User
    description: Get User Data For Given `user_id`
    responses:
      200:
        description: Successfully retrieved user details
    """

app = web.Application()
app.router.add_get("/api/v1/users/{user_id}", get_user)

# Create an APISpec
spec = APISpec(
    title="AioHttp Application",
    version="1.0.0",
    openapi_version="3.0.3",
    plugins=[
        AioHttpPlugin(app),
    ],
)

spec.path(resource=get_user)
print(spec.to_yaml())  # same behavior

With dataclasses plugin

from dataclasses import dataclass
from aiohttp import web
from aiohttp_apispec_plugin import AioHttpPlugin
from apispec import APISpec
from dataclasses_jsonschema import JsonSchemaMixin
from dataclasses_jsonschema.apispec import DataclassesPlugin

@dataclass
class User(JsonSchemaMixin):
    """User Schema"""
    id: int
    username: str

async def get_user(request: web.Request) -> web.Response:
    """User Detail View
    ---
    summary: Get User
    description: Get User Data For Given `user_id`
    responses:
      200:
        description: Successfully retrieved user details
        content:
            application/json:
                schema: User
    """

app = web.Application()
app.router.add_get("/api/v1/users/{user_id}", get_user)

spec = APISpec(
    title="AioHttp Application",
    version="1.0.0",
    openapi_version="3.0.3",
    plugins=[
        AioHttpPlugin(app),
        DataclassesPlugin(),
    ],
)

spec.components.schema("User", schema=User)
spec.path(resource=get_user)

print(spec.to_yaml())
"""
components:
  schemas:
    User:
      description: User Schema
      properties:
        id:
          type: integer
        username:
          type: string
      required:
      - id
      - username
      type: object
info:
  title: AioHttp Application
  version: 1.0.0
openapi: 3.0.3
paths:
  /api/v1/users/{user_id}:
    get:
      description: Get User Data For Given `user_id`
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'
          description: Successfully retrieved user details
      summary: Get User
"""

Requirements

Python >= 3.6

Dependencies:

Other libs to check

Metadata

Release files for aiohttp-apispec-plugin 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiohttp-apispec-plugin 0.2
File Size Uploaded
aiohttp_apispec_plugin-0.2.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aiohttp-apispec-plugin 0.2
File Interpreter ABI Platform
aiohttp_apispec_plugin-0.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.2 kB

Release files / aiohttp_apispec_plugin-0.2.tar.gz

Download URL aiohttp_apispec_plugin-0.2.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
fd818078d917cce112ca37605bf1619a9b6159255d296352a063ad1aecf67155
BLAKE2b-256 checksum
How to use checksums
f8fb09dae567b8cc5045bbabd5fc7b098de2803c517b4038121b543425fddc6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.3

Release files / aiohttp_apispec_plugin-0.2-py3-none-any.whl

Download URL aiohttp_apispec_plugin-0.2-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b6b2839748f5e1d562854f8885996cf69722d3e6d8ac1b762ff7591d87c83f08
BLAKE2b-256 checksum
How to use checksums
a9b91566b880c06567f2adf13472f82e5a5cad74b1726955a53a133ee885b6d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.3

Release history Release notifications | RSS feed

This release

0.2 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page