Modest utility collection for development with AIOHTTP framework.
Project description
Modest utility collection for development with AIOHTTP framework.
Documentation
Installation
Installing aiohttp-things with pip:
pip install aiohttp-things
Simple example
Example of AIOHTTP application
import json
import uuid
from aiohttp import web
from aiohttp_things.views import ContextMixin, PrimaryKeyMixin
def safe_json_value(value):
try:
json.dumps(value)
return value
except (TypeError, OverflowError):
return str(value)
class Base(web.View, ContextMixin, PrimaryKeyMixin):
async def get(self):
pk_type = type(self.pk)
self.context['Type of primary key'] = safe_json_value(pk_type)
self.context['Value of primary key'] = safe_json_value(self.pk)
return web.json_response(self.context)
class IntegerExample(Base):
pk_factory = int
class UUIDExample(Base):
pk_factory = 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
aiohttp-things-0.2.0.tar.gz
(4.4 kB
view hashes)
Built Distribution
Close
Hashes for aiohttp_things-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f15d6c22d122704a0fcd04a83382dadc41363ab9634b3d07dcbb9aed907a72f0 |
|
MD5 | c6585e95814afcf4730893612c2416be |
|
BLAKE2b-256 | c4c04a124d4355da1fd388ad2890ab884c4125a52ac087ff1bff03752d5014f2 |