Skip to content

aamalev/aiohttp_apiset

This branch is 6 commits ahead of, 247 commits behind master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

4ca9c49 · Jul 20, 2023
Jul 18, 2023
Jun 16, 2023
Apr 28, 2017
Nov 23, 2021
Jun 16, 2023
Oct 5, 2015
Mar 4, 2018
Nov 19, 2016
Sep 27, 2017
Nov 22, 2020
Oct 5, 2015
Oct 28, 2016
Apr 3, 2022
Jul 20, 2023
Jun 16, 2023
Jun 16, 2023
Aug 23, 2020
Apr 3, 2022
Apr 3, 2022

Repository files navigation

aiohttp-apiset

Coverage Code style: ruff Documentation Status Hatch project

Package to build routes and validate request using swagger specification 2.0.

Features

  • Building of the routing from specification swagger
  • Using inclusions other specifications with concatenate url
  • Optional output of the resulting specification and view embed swagger-ui
  • Advanced router with TreeResource
  • Extract specify parameters from request and validate with jsonschema
  • Serialize data as response with middleware

Usecase

Package aiohttp_apiset allows supports several strategies:

  • The foreign specification. When the specification is made and maintained by another team.
  • The specification in the code. When the fragments of specification are placed in the docstrings.
  • Mixed strategy. When routing are located in the specification files and operations are described in the docstrings.

Example

async def handler(request, pet_id):
    """
    ---
    tags: [Pet]
    description: Info about pet
    parameters:
      - name: pet_id
        in: path
        type: integer
        minimum: 0
    responses:
      200:
        description: OK
      400:
        description: Validation error
      404:
        description: Not found
    """
    pet = await db.pets.find(pet_id)

    if not pet:
        return {'status': 404, 'msg': 'Not Found'}

    return {
        'pet': pet,  # dict serialized inside jsonify
    }


def main():
    router = SwaggerRouter(
        swagger_ui='/swagger/',
        version_ui=2,
    )
    router.add_get('/pets/{pet_id}', handler=handler)

    app = web.Application(
        router=router,
        middlewares=[jsonify],
    )

    web.run_app(app)

Is now available in the swagger-ui to the address http://localhost:8080/swagger/. Available both branch swagger-ui. For use branch 3.x visit http://localhost:8080/swagger/?version=3

Examples: examples

Development

Check code:

hatch run lint:all

Format code:

hatch run lint:fmt

Run tests:

hatch run pytest

Run tests with coverage:

hatch run cov