mentortools/libs/: http-tools-abm-6.3.76521 metadata and description

Simple index

author Mike Orlov
author_email m.orlov@technokert.ru
classifiers
  • Programming Language :: Python :: 3
  • Programming Language :: Python :: 3.11
description_content_type text/markdown
requires_dist
  • aiohttp (>=3.11.13,<4.0.0)
  • async-tools-abm (>=2.1.61556,<3.0.0)
  • dynamic-types-abm (>=1.1.69079,<2.0.0)
  • init-helpers-abm (>=3.1.69073,<4.0.0)
  • multidict (>=6.0.4,<7.0.0)
  • yarl (>=1.18.3,<2.0.0)
requires_python >=3.11,<4.0
File Tox results History
http_tools_abm-6.3.76521-py3-none-any.whl
Size
31 KB
Type
Python Wheel
Python
3
http_tools_abm-6.3.76521.tar.gz
Size
20 KB
Type
Source

Http tools

Async http server as component

Копирование кода для тестов
cp -r $ORIGIN_DIR/http_tools ./http_tools

Basic example

Echo server

import asyncio

from http_tools import HttpServer, JsonableAnswer

async def amain():
    server = HttpServer(HttpServer.Config(port=7777), HttpServer.Context(instance_id="readme"))
    server.register_handler('/echo', lambda x: JsonableAnswer(x.key_value_arguments))
    async with server:
        print("Server started")
        await asyncio.sleep(10 ** 10)

asyncio.run(amain())

Http server started

Server started

Example request

curl --location "http://0.0.0.0:7777/echo?one=1&two=2"

Expected answer

{"one": "1", "two":  "2"}

Form data example

Server with endpoint accepting two id collections

import asyncio

from http_tools import HttpServer, JsonableAnswer, IncomingRequest

def merge_collections(request: IncomingRequest) -> JsonableAnswer:
    assert (token := request.parsed_body['token'].parse()) == 'secret', f'Expected "secret", got {token}'
    result = set(request.parsed_body['ids'].parse()) & set(request.parsed_body['other_ids'].parse())
    return JsonableAnswer(result)

async def amain():
    server = HttpServer(HttpServer.Config(port=7777), HttpServer.Context(instance_id="readme"))
    server.register_handler('/merge', merge_collections)
    async with server:
        print("Server started")
        await asyncio.sleep(10 ** 10)

asyncio.run(amain())

After start this code will print:

Server started

Example request

curl -X POST --location "http://127.0.0.1:7777/merge" \
    -H "authorization: Bearer GGKYP6MBYQ" \
    -F "ids=[1,2,3];filename=ids.json;type=application/json" \
    -F "other_ids=[2,3,4];type=application/json" \
    -F "token=secret"

Expected answer

[2, 3]