Skip to main content

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.

Release files for logiclayer 0.4.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for logiclayer 0.4.4
File Size Uploaded
logiclayer-0.4.4.tar.gz 7.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for logiclayer 0.4.4
File Interpreter ABI Platform
logiclayer-0.4.4-py3-none-any.whl Python 3 none any Details

Total release size: 17.7 kB

Release files / logiclayer-0.4.4.tar.gz

Download URL logiclayer-0.4.4.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
bb8b81c105578ee9d990244ea5f2ee6d9a4961c594cb53dd8a1bf5e597b3ff69
BLAKE2b-256 checksum
How to use checksums
51b4d04e90c81cb536b33cc51c423e7b6a8c4971dc530e050ea8fb4cbfcbc5eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.12.3 Linux/6.8.0-1021-azure

Release files / logiclayer-0.4.4-py3-none-any.whl

Download URL logiclayer-0.4.4-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
64ce30611e7fa1f058a53d9317308cf3b3b1d2b6cb223b97bbe0468d6ee54dcd
BLAKE2b-256 checksum
How to use checksums
a88c236d27c216c7fbdb05f68fccb9276f99a56fc623911a720eb928e57154e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.12.3 Linux/6.8.0-1021-azure

Release history Release notifications | RSS feed

This release

0.4.4 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page