Skip to main content

Web API Controllers

Description

Simple Web API controller framework for FastAPI

Installation

To install this package, use pip:

pip install webapicontrollers

Example

from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse
from webapicontrollers import APIController, Get, Post, Patch, Delete, Put, RoutePrefix



@RoutePrefix('/test')
class TestController(APIController):

    def __init__(self, app: FastAPI) -> None:
        super().__init__(app, cors_origins=['*'])    
    
    @Get('/', name='Optional name for OpenAPI docs', description='Optional description for OpenAPI docs')
    async def get(self) -> dict:
        return {"method": "GET", "path": "/"}  
    
    @Get('/400')
    async def get_bad_request(self) -> dict:
        raise HTTPException(status_code=400, detail="Bad Request")
    
    @Get('/401')
    async def get_not_authorized(self) -> dict:
        raise HTTPException(status_code=401, detail="Not Authorized")
    
    @Get('/403')
    async def get_forbidden(self) -> dict:
        raise HTTPException(status_code=403, detail="Forbidden")
    
    @Get('/404')
    async def get_not_found(self) -> dict:
        raise HTTPException(status_code=404, detail="Not Found")
    
    @Get('/405')
    async def get_method_not_allowed(self) -> dict:
        raise HTTPException(status_code=405, detail="Method Not Allowed")
    
    @Get('/500')
    async def get_internal_server_error(self) -> dict:
        raise HTTPException(status_code=500, detail="Internal Server Error")
    
    @Get('/{arg}')
    async def get_with_arg(self, arg) -> dict:
        return {"method": "GET", "path": "/", "arg": arg}

    @Post('/')
    async def post(self) -> dict:
        return {"method": "POST", "path": "/"}

    @Post('/{arg}')
    async def post_with_arg(self, arg) -> dict:
        return {"method": "POST", "path": "/", "arg": arg}
    
    @Put('/')
    async def put(self) -> dict:
        return {"method": "PUT", "path": "/"}
    
    @Put('/{arg}')
    async def put_with_arg(self, arg) -> dict:
        return {"method": "PUT", "path": "/", "arg": arg}
    
    @Patch('/')
    async def patch(self) -> dict:
        return {"method": "PATCH", "path": "/"}
    
    @Patch('/{arg}')
    async def patch_with_arg(self, arg) -> dict:
        return {"method": "PATCH", "path": "/", "arg": arg}
    
    @Delete('/')
    async def delete(self) -> dict:
        return {"method": "DELETE", "path": "/"}
    
    @Delete('/{arg}')
    async def delete_with_arg(self, arg) -> dict:
        return {"method": "DELETE", "path": "/", "arg": arg}
    
    def bad_request(self, request: Request, exc: HTTPException) -> JSONResponse:
        # Custom handling code
        return super().bad_request(request, exc)
    
    def not_authorized(self, request: Request, exc: HTTPException) -> JSONResponse:
        # Custom handling code
        return super().not_authorized(request, exc)
    
    def forbidden(self, request: Request, exc: HTTPException) -> JSONResponse:
        # Custom handling code
        return super().forbidden(request, exc)
    
    def not_found(self, request: Request, exc: HTTPException) -> JSONResponse:
        # Custom handling code
        return super().not_found(request, exc)
    
    def method_not_allowed(self, request: Request, exc: HTTPException) -> JSONResponse:
        # Custom handling code
        return super().method_not_allowed(request, exc)
    
    def internal_server_error(self, request: Request, exc: HTTPException) -> JSONResponse:
        # Custom handling code
        return super().internal_server_error(request, exc)


app = FastAPI()

TestController(app)

Known Issues

If you overide the handler methods such as not_found etc. in more than one cotnroller only one handler will be registered on a last one wins basis. Implementing a per route prefix handling system is on the to do list.

If you create a base controller class and then overide it's methods in a derived class the path needs to be the same in both methods. If you don't do this then FastAPI gets confused about which handler maps to which path.

Caution

This project is in a very early state and might not be very useful to anyone yet. There is no support avilable, use at your own risk.

License

This project is licensed under the MIT License.

Release files for webapicontrollers 1.0.1

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

Source distribution (sdist)

Source distribution for webapicontrollers 1.0.1
File Size Uploaded
webapicontrollers-1.0.1.tar.gz 8.1 kB Details

Built distribution (wheel)

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

Total release size: 19.1 kB

Release files / webapicontrollers-1.0.1.tar.gz

Download URL webapicontrollers-1.0.1.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
54a12a7677f8d9e5acf46b40cbe96cc2afd7796aa6c58da24c2c2ac6e999f5dd
BLAKE2b-256 checksum
How to use checksums
7c27e9586f4c7bf9f78c2f0d5761cfeb0d328574b38276c61ee89140f4f9aefa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Release files / webapicontrollers-1.0.1-py3-none-any.whl

Download URL webapicontrollers-1.0.1-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
660cbdc6c827a058627da9446c8ee66898b880458f03e72a5398880c1bff855f
BLAKE2b-256 checksum
How to use checksums
104ffa86a6f4b7340b0cea4d5659777f1bff37ab1444d20654e1b1cf7a85813a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

0.0.16

2 release files

0.0.15

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

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