Skip to main content

FastAPI framework class view router

Project description

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.

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

fastapi_cls-0.1.3.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

fastapi_cls-0.1.3-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_cls-0.1.3.tar.gz.

File metadata

  • Download URL: fastapi_cls-0.1.3.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for fastapi_cls-0.1.3.tar.gz
Algorithm Hash digest
SHA256 31fc62761f304115a8b5226c87456f273798f7b2f3bba461b0e05fa6012f0877
MD5 acf0c4761d3624f8367d247fd00a9c59
BLAKE2b-256 22c85d462bf831b2fbb1f1a51a919cd28662663c5b04756dc14c546898c22289

See more details on using hashes here.

File details

Details for the file fastapi_cls-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: fastapi_cls-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for fastapi_cls-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 063285720660b037dcb3016d54cdd1e0eb0b2a9969904b15cd7c2b2af45d62d6
MD5 b526241d3d8674665456c2716f0dda61
BLAKE2b-256 a72c214e72536af7af8aea92f0c2924eb1dca1dc860d689a48472486aeeae200

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