Skip to main content

Example

fastapi_cls demo

  • Create a file view.py with:

    from pydantic import BaseModel
    
    from fastapi import Depends
    
    class User(BaseModel):
        username: str
        email: Optional[str] = None
        full_name: Optional[str] = None
        disabled: Optional[bool] = None
    
    async def get_user():
        return User(username="john")
    
    class ItemView:
        user: User = Depends(get_user)
    
        def get(self) -> User:
            return self.user
    
        def post(self) -> str:
            self.user.username = 'change name'
            return "ok"
    
  • Create a file route.py with:

    from fastapi_cls import ClassRouter 
    router = ClassRouter()
    router.add_resource("/item",ItemView,methods=["GET","POST"])    
    

    Or, if you want to define your owner method reflect to http method. you can do like this.

    from fastapi_cls import ClassRouter 
    router = ClassRouter()
    router.add_method_route("/item",ItemView,ItemView.get, methods=["GET"])    
    router.add_method_route("/item",ItemView,ItemView.post, methods=["POST"])    
    

Its equivalent to

  • fastapi

    from fastapi import ApiRouter, Depends
    
    
    class User(BaseModel):
        username: str
        email: Optional[str] = None
        full_name: Optional[str] = None
        disabled: Optional[bool] = None
    
    async def get_user():
        return User(username="john")
    
    router = ApiRouter()
    
    @router.get('/item')
    def get(user: User = Depends(get_user)) -> User:
        return user
    
    @router.post('/item')
    def post(user: User = Depends(get_user)) -> str:
        user.username = 'change name'
        return "ok"
    

    In this case. The ItemView methods get and post will bind on router as a function.

Release files for fastapi-cls 0.1.3

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

Source distribution (sdist)

Source distribution for fastapi-cls 0.1.3
File Size Uploaded
fastapi_cls-0.1.3.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-cls 0.1.3
File Interpreter ABI Platform
fastapi_cls-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 10.2 kB

Release files / fastapi_cls-0.1.3.tar.gz

Download URL fastapi_cls-0.1.3.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
31fc62761f304115a8b5226c87456f273798f7b2f3bba461b0e05fa6012f0877
BLAKE2b-256 checksum
How to use checksums
22c85d462bf831b2fbb1f1a51a919cd28662663c5b04756dc14c546898c22289
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.7

Release files / fastapi_cls-0.1.3-py3-none-any.whl

Download URL fastapi_cls-0.1.3-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
063285720660b037dcb3016d54cdd1e0eb0b2a9969904b15cd7c2b2af45d62d6
BLAKE2b-256 checksum
How to use checksums
a72c214e72536af7af8aea92f0c2924eb1dca1dc860d689a48472486aeeae200
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.0 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.7

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

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