Skip to main content

Steroid

Project description

steroid

Steroid is a Python library built on top of FastAPI, designed to provide developers with a framework for building high-performance APIs using a structured architecture inspired by NestJs.

Installation

$ pip install steroid

Usage

import pydantic
from fastapi import Header, HTTPException, Request

from steroid import Controller, CreateApp, Get, Middleware, Post, UseMiddlewares


class Salute(pydantic.BaseModel):
    aye: str


class AuthMiddleware(Middleware):
    HOOK = "POST"

    def action(request: Request):
        authToken = request.headers.get("Authorization")
        if authToken is None or not authToken:
            raise HTTPException(status_code=401, detail="Unauthorized")


class PreMiddleware(Middleware):
    HOOK = "PRE"

    def action(request: Request):
        print("hello")


class PostMiddleware(Middleware):
    HOOK = "POST"

    def action(request: Request):
        print("bye")


@Controller("greetings")
class GreetController:
    @Get("greet/{pk}/{gID}")
    def Greet(
        pk: str,
        gID: int,
        authorization: str = Header(alias="Authorization", default=None),
    ):
        return {"hello": {"pk": pk, "gID": gID, "XToken": authorization}}

    @UseMiddlewares(AuthMiddleware)
    @Post("no-greet")
    def NoGreet(salute: Salute):
        return {"noHello": salute}


@Controller("clutch")
class CluthController:
    @UseMiddlewares(PreMiddleware, PostMiddleware)
    @Get("savage")
    def Savage():
        return {"hello": "clutchers, savage, and everything else"}

    @Get("not-a-savage")
    def NotASavage():
        return {"hello": "clutchers, not a savage, and everything else"}


def main():
    app = CreateApp()

    app.addController(GreetController)
    app.addController(CluthController)
    app.start()


if __name__ == "__main__":
    main()

Start server

$ python example.py
[23/07/2023, 19:19:57] INFO [Controller] GreetController {/greetings}
[23/07/2023, 19:19:57] INFO [Method] Mapped {/greetings/greet/{pk}/{gID}, GET} route
[23/07/2023, 19:19:57] INFO [Method] Mapped {/greetings/no-greet, POST} route
[23/07/2023, 19:19:57] INFO [Controller] CluthController {/clutch}
[23/07/2023, 19:19:58] INFO [Method] Mapped {/clutch/savage, GET} route
[23/07/2023, 19:19:58] INFO [Method] Mapped {/clutch/not-a-savage, GET} route

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

steroid-0.16b0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

steroid-0.16b0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file steroid-0.16b0.tar.gz.

File metadata

  • Download URL: steroid-0.16b0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.7.17 Linux/6.5.0-1016-azure

File hashes

Hashes for steroid-0.16b0.tar.gz
Algorithm Hash digest
SHA256 691287d9f3ea0fadc7c9c406df02cf553a2f7cbdccce0a20e33460a5b3a59d54
MD5 5aa778315442c6de1b67dd6cdc5541d9
BLAKE2b-256 701ba1ab6a8c646d6c16fb9362bf17262007a6b984bf7f4e6a827e6bca84e53f

See more details on using hashes here.

File details

Details for the file steroid-0.16b0-py3-none-any.whl.

File metadata

  • Download URL: steroid-0.16b0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.7.17 Linux/6.5.0-1016-azure

File hashes

Hashes for steroid-0.16b0-py3-none-any.whl
Algorithm Hash digest
SHA256 8812d4b7e2849d55e1c2535ae5ee64ca4170723215aee67f0beebf3d522d00fa
MD5 611a02583fc05909ff4caa024a8f9710
BLAKE2b-256 71970155ac8e1554625f9d395655c1ceaf1b3bdc05d88f5a63b1080a044e69aa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page