A framework to quickly compose and use multiple functionalities as endpoints.
Project description
A simple framework to quickly compose and use multiple functionalities as endpoints.
LogicLayer is built upon FastAPI to provide a simple way to group functionalities into reusable modules.
Getting started
LogicLayer allows to group multiple endpoints with related functionality into a single module, which can be installed in a single step, and with the option to share external objects and make them available to the routes.
The unit of functionality is a Module, which must be a subclass of the LogicLayerModule
class. Then you can mark its methods as module routes using the route
decorator:
# echo.py
import logiclayer as ll
import platform
class EchoModule(ll.LogicLayerModule):
def get_python_version():
return platform.python_version()
@ll.route("GET", "/")
def route_status(self):
return {
"module": "echo",
"version": "0.1.0",
"python": self.get_python_version(),
}
[...more methods]
You can setup multiple methods in your module class, and only the decorated ones will be setup as routes in your module. The ll.route
method accepts the same parameters as FastAPI's app.get/head/post/put
methods, with the difference you can set multiple methods at once passing a list instead of the HTTP method as string:
ll.route("GET", "/")
# is the same as
ll.route(["GET"], "/")
# so this also works
ll.route(["GET", "HEAD"], "/")
# (...just be careful to leave the answer empty when needed)
Then just create a new LogicLayer
instance and add the module using the add_module()
method. The first argument is the prefix to the paths of all URLs for this module, and the second is the instance of the LogicLayerModule subclass:
# example.py
import requests
import logiclayer as ll
from .echo import EchoModule
layer = LogicLayer()
# this will work as a healthcheck for the app
def is_online() -> bool:
"""Checks if the machine is online."""
res = requests.get("http://clients3.google.com/generate_204")
return (res.status_code == 204) and (res.headers.get("Content-Length") == "0")
# healthchecks are set to run in the root `/_health` path
layer.add_check(is_online)
echo = EchoModule()
layer.add_module("/demo", echo)
The layer
object is an ASGI-compatible application, that can be used with uvicorn/gunicorn to run a server, the same way as you would with a FastAPI instance.
$ pip install uvicorn[standard]
$ uvicorn example:layer
Note the example:layer
parameter is the reference to the layer
variable in the example
module/file, which points to the ASGI app instance.
Optionally, you can also install a module in a common FastAPI instance, using the internal APIRouter
instance:
app = FastAPI()
echo = EchoModule()
app.include_router(echo.router, prefix="/demo")
© 2022 Datawheel, LLC.
This project is licensed under MIT.
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 logiclayer-0.4.1.tar.gz
.
File metadata
- Download URL: logiclayer-0.4.1.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 581c233ed8bfb414ac631b96358ed1ceb17352e18b9848b19a3cb7e2fc36e1af |
|
MD5 | 02b339b3951b42b1299e9eff70d7d30d |
|
BLAKE2b-256 | 5b4e1b0d6c2165993de5e8cb087bceab1fc5566c6afda2dcc59d6aae7387c743 |
File details
Details for the file logiclayer-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: logiclayer-0.4.1-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0437c4c24034573059c33ae6507f6fa22f475c86ea19b2c8c568214f11b29a4a |
|
MD5 | 402e50d0931bd2738d874e829134432e |
|
BLAKE2b-256 | f6154320e1ceb2d975fe561211f5c09ec412120107063c798da377beb30a320d |