Python 3

AioHTTP

Python

The tiny framework based on AIOHTTP

Build your APIs easily

Let me introduce our project, I think he's already moved on to a stage when it can be called a framework. Although its name says that it is boilerplate (maybe in the future, we will change the name). The project is still quite raw, but you can easily build your APIs.

So let's try to show everything by example and build a minimal application that will give the list of posts.

First, let's create a folder where be our project and then set up a virtual environment and install our framework

bash
mkdir example
cd example
python -m venv venv
source venv/bin/activate
pip install aiohttp_boilerplate

Let's create a folder for organizing project files

bash
mkdir app

Then we begin to describe the data model from which validate, deserialize and serialize. For that we use marshmallow.

Create a file schemas.py

py
from marshmallow import Schema, fields

class PostSchema(Schema):
  id = fields.Int()
  name = fields.Str()
  content = fields.Str()

Create a file models.py where will be a class which helps us communicate with a database without writing any SQL queries

py
from aiohttp_boilerplate.models import Manager

class Page(Manager):
  table = "post_post"

This set will be enough to create an endpoint through which we can get posts

Create a file views.py

py
from aiohttp_boilerplate.views.list import ListView

from .models import Post
from .schemas import PostSchema


class PostListView(ListView):
    def get_model(self):
          return Post

    def get_schema(self):
        return PostSchema

We also need to specify the path the class to handle a request

Create a file routes.py

py
from . import views

def setup_routes(app):
    app.router.add_route("GET", "/", views.PostListView)

Then we create a file 'main.py' to run the service

py
from aiohttp_boilerplate.bootstrap import web_app

if __name__ == “__main__”:
    web_app()

Since this service communicates with the database, we need to add variables to the environment to connect to the database.

bash
export DB_DATABASE=test
export DB_USER=postgres
export DB_PASSWORD=password
export DB_HOST=localhost
export DB_PORT=5432

If you don't have your own database running, you can run a test database into a docker container.

bash
docker run --rm --name test-postgres -e POSTGRES_PASSWORD=password -e POSTGRES_DB=test -p 5432:5432 -d postgres

Attach to the database concole

bash
docker exec -it some-postgres psql -Upostgres

Create a table

bash
create table post_post
(
    id serial
        constraint post_post_pk
        primary key,
    name varchar(50) default '' not null,
    content text default '' not null
);

Fill it with data

bash
insert into post_post (name,content) values ('Post 1', 'Content');
insert into post_post (name,content) values ('Post 2', 'Blabla');

All files are ready to launch our service. The structure of the project looks like this.

├── app
│   ├── __init__.py
│   ├── __main__.py
│   ├── models.py
│   ├── routes.py
│   ├── schemas.py
│   └── views.py
└──

Run the application

bash
python -m app

Make a request

bash
 http get http://localhost:8080/

{
    "count": 2,
    "data": [
        {
            "content": "Content",
            "id": 1,
            "name": "Post 1"
        },
        {
            "content": "Blabla",
            "id": 2,
            "name": "Post 2"
        }
    ]
}

The source code of this example you can find here

20 JUL, 2023

CI/CD to release high-quality products at speed

Nowadays many companies about to use or already using Continuous Integration (CI) and Continuous Delivery (CD) to release high-quality products at speed. Stability and reliability of the platform is a foundation for any technical product or service. The CI/CD pipeline approach provide you more control over what should and what should not be deployed.

16 OCT, 2023

6 keys for an effective onboarding in small distributive team

How to build effective onboarding in terms of remote culture, rapid team growing and changing without the ability to delegate to personal onboarding buddies?

28 APR, 2020

Smooth links transitions on dynamic content in Vuejs Application

Usually links inside Vue.js app handle by vue-router and router-link component. It makes a transition between routes without reloading page. The smooth and slick user interface is one of the advantages of Single Page Applications. In the case with CMS content, links are inside content and can’t be processing separately but inserting as one HTML part with `v-html` directive. Such links on click will reload page even if they follow the same app page.

Help Ukraine to stop russian aggression