aiohttp-things’s documentation

Read The Docs build Release PyPI downloads count MIT License Python version support GitHub continuous integration codecov.io status for master branch

Modest utility collection for development with AIOHTTP framework.

Installation

Installing aiohttp-things with pip:

pip install aiohttp-things

Examples

Simple example

Example of AIOHTTP application
import json
import uuid
import aiohttp_things as ahth
from aiohttp import web


def safe_json_value(value):
    try:
        json.dumps(value)
        return value
    except (TypeError, OverflowError):
        return str(value)


class Base(web.View, ahth.JSONMixin, ahth.PrimaryKeyMixin):
    async def get(self):
        self.context['Type of primary key'] = safe_json_value(type(self.pk))
        self.context['Value of primary key'] = safe_json_value(self.pk)
        return await self.finalize_response()


class IntegerExample(Base):
    pk_adapter = int


class UUIDExample(Base):
    pk_adapter = uuid.UUID


UUID = '[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}'
ROUTES = [
    web.view('/integer/{pk:[0-9]+}', IntegerExample),
    web.view(f'/uuid/{{pk:{UUID}}}', UUIDExample),
]


async def app_factory():
    app = web.Application()
    app.add_routes(ROUTES)
    return app


if __name__ == '__main__':
    web.run_app(app_factory())
Examples HTTP requests and response
  • http://0.0.0.0:8080/integer/1

    {
      "Type of primary key": "<class 'int'>",
      "Value of primary key": 1
    }
    
  • http://0.0.0.0:8080/integer/9999999999999

    {
      "Type of primary key": "<class 'int'>",
      "Value of primary key": 9999999999999
    }
    
  • http://0.0.0.0:8080/integer/a352da04-c1af-4a44-8a94-c37f8f37b2bc

    404: Not Found
    
  • http://0.0.0.0:8080/integer/abc

    404: Not Found
    
  • http://0.0.0.0:8080/uuid/a352da04-c1af-4a44-8a94-c37f8f37b2bc

    {
      "Type of primary key": "<class 'uuid.UUID'>",
      "Value of primary key": "a352da04-c1af-4a44-8a94-c37f8f37b2bc"
    }
    
  • http://0.0.0.0:8080/uuid/13d1d0e0-4787-4feb-8684-b3da32609743

    {
      "Type of primary key": "<class 'uuid.UUID'>",
      "Value of primary key": "13d1d0e0-4787-4feb-8684-b3da32609743"
    }
    
  • http://0.0.0.0:8080/uuid/1

    404: Not Found
    
  • http://0.0.0.0:8080/uuid/abc

    404: Not Found
    

Reference

Web handlers

class aiohttp_things.AbstractHandler(request: aiohttp.web_request.Request)

Bases: aiohttp.abc.AbstractView

class aiohttp_things.ContextMixin(request: aiohttp.web_request.Request)

Bases: aiohttp.abc.AbstractView

class HTTPMethodMixin(request: aiohttp.web_request.Request)

Bases: aiohttp_things.web_handlers.AbstractHandler

class aiohttp_things.PaginationMixin(request: aiohttp.web_request.Request)

Bases: aiohttp.abc.AbstractView

class aiohttp_things.PrimaryKeyMixin(request: aiohttp.web_request.Request)

Bases: aiohttp.abc.AbstractView

class aiohttp_things.ItemMixin(request: aiohttp.web_request.Request)

Bases: aiohttp.abc.AbstractView

class aiohttp_things.ListMixin(request: aiohttp.web_request.Request)

Bases: aiohttp.abc.AbstractView

Response mixins
class aiohttp_things.Jinja2Mixin(request: aiohttp.web_request.Request)

Bases: aiohttp_things.web_handlers.ContextMixin

class aiohttp_things.JSONMixin(request: aiohttp.web_request.Request)

Bases: aiohttp_things.web_handlers.ContextMixin

class aiohttp_things.ResponseFormatMixin(request: aiohttp.web_request.Request)

Bases: aiohttp.abc.AbstractView

class ResponseAutoformatMixin(request: aiohttp.web_request.Request)
Members

Show-inheritance

Releases

Version 0.14

Added

  • Added page_key attribute instead page attribute;

  • Added page_key_adapter attribute instead page_adapter attribute.

Deprecated

  • page attribute is deprecated. Use page_key` attribute;

  • page_adapter is deprecated. Use page_key_adapter attribute.

Version 0.13

Added

  • paginator attribute to PaginationMixin.

Version 0.12

Added

  • ResponseAutoformatMixin.

Changed

  • Renamed handlers module to web_handlers.

Version 0.11

Added

  • HTTPMethodMixin.

Removed

  • finalize_response method in AbstractHandler.

Version 0.10

Added

  • AbstractHandler.

Changed

  • views module renamed to handlers;

  • classes from handlers temporarily imported to empty views module for backward compatibility.

Removed

** InstanceMixin a synonym for ItemMixin.

Version 0.9

Added

  • Added views.PaginationMixin;

  • Added ContextMixin, ItemMixin, Jinja2Mixin, JSONMixin, ListMixin, PaginationMixin, PrimaryKeyMixin to package namespace.

Removed

  • instance attribute removed from ItemMixin, use ItemMixin.item;

  • pk_factory attribute removed from PrimaryKeyMixin, use PrimaryKeyMixin.pk_adapter.

Version 0.8

Added * ResponseFormatMixin

Changed * views.InstanceMixin class renamed to views.ItemMixin; * views.ItemMixin synonym renamed to views.InstanceMixin.

Version 0.7

Add

  • views.PrimaryKeyMixin.pk_adapter instead views.PrimaryKeyMixin.pk_factory.

Deprecated

  • views.PrimaryKeyMixin.pk_factory.

Version 0.6

Changed

  • views.InstanceMixin and views.ListMixin inherited by aiohttp.abc.AbstractView;

  • views.ItemMixin renamed to views.InstanceMixin.

Version 0.5

Removed

  • prepare_context() method removed from views.ContextMixin and views.ListMixin.

Version 0.4

Added

** added views.Jinja2Mixin (need install aiohttp-jinja2); ** added views.JSONMxixin; ** added views.ListMixin;

Changed

  • views.PrimaryKeyMixin allocated in a separate mixin;

  • views.ItemMixin inherited by views.ContextMixin and views.PrimaryKeyMixin.

Version 0.3.0

Changed

  • views.PrimaryKeyMixin renamed to views.ItemMixin.

Added

  • Added views.PrimaryKeyMixin synonym for views.ItemMixin.

Version 0.2.0

Changed

  • Replace aiohttp.abc.AbstractView to views.ContextMixin in parent classes of views.PrimaryKeyMixin.