Otter - framework for microservices.
Project description
Otter
Otter is framework for creating microservices in Flask like fassion using RPC communication via message queue. Its built in top of kiwiPy library.
Application
Simple Otter application looks liks this.
from otter import Otter
import hashlib
import time
app = Otter()
@app.add_broadcast(["message.*"])
def example_broadcast(comm, body, sender, subject, correlation_id):
print(f"Received {body}")
@app.add_task("example.task")
def example_task(comm, delay):
time.sleep(delay)
print(f"Done waiting {delay} seconds")
@app.add_rpc("example.hash")
def example_hash(comm, data):
return {
"result": hashlib.sha256(
data.encode("utf-8")
).hexdigest()
}
app.run("amqp://guest:guest@127.0.0.1/", debug=True)
Client
You can communicate with Otter application using Client
.
from otter import Client
client = Client("amqp://guest:guest@127.0.0.1/")
if client.comm:
# Broadcast message to anyone who listens
client.broadcast("otter", subject="message.send")
# Send task to remote worker
client.task(5)
# Send rpc request to hash "otter" and wait for the result
data = client.rpc("example.hash", "otter").result()
client.close()
Blueprints
You can use Flask like blueprints in order to organize and split your application into different modules.
from otter import Blueprint
blueprint = Blueprint()
@blueprint.add_rpc("hello.world")
def example_hash(comm, data):
return {"result": "Hello world from blueprint!"}
After that blueprint can be initialized using register_blueprint
method.
app.register_blueprint(blueprint)
Decorators
In most cases you will use JSON to communicate between your microservices, thats why Otter has built in decorator @use_args
which uses Marshmallow for data validation.
from marshmallow import fields, validate
from otter.decorators import use_args
login_args = {
"password": fields.Str(required=True, validate=validate.Length(min=8)),
"email": fields.Email(required=True)
}
@app.add_rpc("auth.login")
@use_args(login_args)
def login(comm, data):
return {"args": data}
Foreword
This is experimental software, any contributions are welcome!
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
Built Distribution
File details
Details for the file otterpy-1.0.tar.gz
.
File metadata
- Download URL: otterpy-1.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09c98d990dfae6d98083ba4853db641d53a772422e07e0d3c24046bed3ac74ae |
|
MD5 | 3db869e1a3c152d0dedd7019586a3d8c |
|
BLAKE2b-256 | 080a6a9fb7965b85728d81dcd3c8c0aa818ad83d36561e4d9decd174089b8752 |
File details
Details for the file otterpy-1.0-py3-none-any.whl
.
File metadata
- Download URL: otterpy-1.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 655d82fcd56c695b8631e4c2c043437a67aa2abf910faec28d0501878e17957a |
|
MD5 | 1e3f783b049e36cd6fcbe413ec9c19d1 |
|
BLAKE2b-256 | ca03af7c0ffaebafd942fd672b1019896b60fe92c4b47acb8767081d25831e10 |