Skip to main content

Rest framework for aiohttp web server

Project description

aiohttp-rest-framework

codecov


Fully asynchronous rest framework for aiohttp web server, inspired by Django Rest Framework (DRF), powered by marshmallow and SQLAlchemy.

Currently supports only combination of postgres (thanks to aiopg) and sqlalchemy.

Installation

pip install aiohttp-rest-framework

Usage example

Consider we have the following sqlalchemy tables (models):

from uuid import uuid4

import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID

meta = sa.MetaData()

users = sa.Table(
    "users", meta,
    sa.Column("id", UUID(as_uuid=True), primary_key=True, default=uuid4),
    sa.Column("name", sa.Text),
    sa.Column("email", sa.Text, unique=True),
    sa.Column("phone", sa.Text),
    sa.Column("company_id", sa.ForeignKey("companies.id"), nullable=True),
)

companies = sa.Table(
    "companies", meta,
    sa.Column("id", UUID(as_uuid=True), primary_key=True, default=uuid4),
    sa.Column("name", sa.Text),
)

Now we can use very familiar to us from DRF serializer, built on top of marshmalow's Schema:

from aiohttp_rest_framework import serializers

from app.models import users


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = users
        fields = "__all__"

And, finally, now we can use our serializer in class based views:

from aiohttp_rest_framework import views

from app.serializers import UserSerializer


class UsersListCreateView(views.ListCreateAPIView):
    serializer_class = UserSerializer


class UsersRetrieveUpdateDestroyView(views.RetrieveUpdateDestroyAPIView):
    serializer_class = UserSerializer

Our simple app would look like this:

from aiohttp import web
from aiohttp_rest_framework import setup_rest_framework

from app import views


app = web.Application()
app.router.add_view("/users", views.UsersListCreateView)
app.router.add_view("/users/{id}", views.UsersRetrieveUpdateDestroyView)
setup_rest_framework(app)
web.run_app(app)

Mention setup_rest_framework() function, it is required to call it to configure framework to work with your app. For available rest framework's config options refer to documentation. For detailed aiohttp web app configuration please refer to their docs.

After starting the app, we can make a POST /users request to create a new user.

curl -H "Content-Type: application/json" 
     -d '{
            "name": "John Doe",
            "email": "john@mail.com",
            "phone": "+123456789"
         }'
     -X POST http://localhost:8080/users

And get the following HTTP 201 Response:

{
  "id": "aa392cc9-c734-44ff-9d7c-1602ecb4df2a",
  "name": "John Doe",
  "email": "john@mail.com",
  "phone": "+123456789",
  "company_id": null
}

Let's try to update user's company. Making PATCH /users/aa392cc9-c734-44ff-9d7c-1602ecb4df2a request

curl -H "Content-Type: application/json" 
     -d '{"company_id": "0413de74-d9fb-494b-ba56-b56599261fb0"}'
     -X PATCH http://localhost:8080/users/a392cc9-c734-44ff-9d7c-1602ecb4df2a

HTTP 200 Response:

{
  "id": "aa392cc9-c734-44ff-9d7c-1602ecb4df2a",
  "name": "John Doe",
  "email": "john@mail.com",
  "phone": "+123456789",
  "company_id": "0413de74-d9fb-494b-ba56-b56599261fb0"
}

For more examples and usages please refer to documentation.

Requirements

Python >= 3.6

Dependencies:

  • aiohttp
  • aiopg
  • sqlalchemy
  • marshmallow

Documentation

TBD

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp_rest_framework-0.0.1.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

aiohttp_rest_framework-0.0.1-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_rest_framework-0.0.1.tar.gz.

File metadata

  • Download URL: aiohttp_rest_framework-0.0.1.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for aiohttp_rest_framework-0.0.1.tar.gz
Algorithm Hash digest
SHA256 72aaa42e90cfa493b9d84cfca0877980582917351986b3ee41b5f9825faf147d
MD5 ca583b19f8245b32b0672697f55d62df
BLAKE2b-256 3c244b205fc9499c2f814f1c91fe5a588f84f40c964f16fe03e777f82146e02f

See more details on using hashes here.

File details

Details for the file aiohttp_rest_framework-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_rest_framework-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for aiohttp_rest_framework-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2598e1f6c50fa0bc3040542a05fefe66cf87c5e5a46d6e8a2b0258b6020cc316
MD5 a42c3dae210a832e11eb96ea52040a13
BLAKE2b-256 e329a5978ae6bc561868cfd284769d536d223ecd80f926c2e6833ed11a10171d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page