Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rennancockles/fastapi-vo
FastAPI View Objects
https://github.com/rennancockles/fastapi-vo
Last synced: 3 months ago
JSON representation
FastAPI View Objects
- Host: GitHub
- URL: https://github.com/rennancockles/fastapi-vo
- Owner: rennancockles
- License: mit
- Created: 2021-08-31T22:36:02.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-09-02T16:07:40.000Z (over 3 years ago)
- Last Synced: 2024-09-27T09:43:16.550Z (3 months ago)
- Language: Python
- Size: 521 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
FastAPI-VO, view objects for FastAPI designed for simplicity.---
**Documentation**: https://fastapi-vo.r3ck.com.br
**Source Code**: https://github.com/rennancockles/fastapi-vo
---
FastAPI-VO is a lightweight library for creating simple FastAPI view objects just by `picking` or `omitting` parts of a model. It is designed to be simple, intuitive and easy to use.
It is so simple that doesn't need much explanation. Just check some examples below.
## Requirements
A recent and currently supported version of Python (right now, Python supports versions 3.6 and above).
**FastAPI-VO** only requires **FastAPI**, but it will be automatically installed when you install FastAPI-VO.
## Installation
```console
$ pip install fastapi-vo
---> 100%
Successfully installed fastapi-vo
```## Example
For an introduction to FastAPI, see the FastAPI documentation.
Here's a quick example. ✨
👀 Full code preview
```Python
from typing import List
from fastapi import FastAPI
from pydantic import BaseModelfrom fastapi_vo import Omit, Pick
class User(BaseModel):
username: str
password: str
is_active: bool = True
is_admin: bool = FalseAuth = Pick(User, ["username", "password"], classname="Auth")
NoPassword = Omit(User, "password", classname="NoPasswordUser")app = FastAPI()
johndoe = User(
username="johndoe",
password="secret",
is_admin=False,
is_active=True,
)
janedoe = User(
username="janedoe",
password="janesecret",
is_admin=True,
is_active=True,
)@app.get("/users/", response_model=List[NoPassword])
async def list_users():
return [johndoe, janedoe]@app.get("/users/john/", response_model=NoPassword)
async def get_user():
return johndoe@app.get("/login/", response_model=NoPassword)
async def login(user: Auth):
# some authentication logic in here
return user
```### Create a Model
Let's create a model called `user` with:
* `username`
* `password`
* `is_active`
* `is_admin````Python hl_lines="1 4-8"
from pydantic import BaseModelclass User(BaseModel):
username: str
password: str
is_active: bool = True
is_admin: bool = Falsejohndoe = User(
username="johndoe",
password="secret",
is_admin=False,
is_active=True,
)janedoe = User(
username="janedoe",
password="janeSecret",
is_admin=True,
is_active=True,
)
```Now we create 2 instances of the **User** model:
```Python hl_lines="11-16 18-23"
from pydantic import BaseModelclass User(BaseModel):
username: str
password: str
is_active: bool = True
is_admin: bool = Falsejohndoe = User(
username="johndoe",
password="secret",
is_admin=False,
is_active=True,
)janedoe = User(
username="janedoe",
password="janeSecret",
is_admin=True,
is_active=True,
)
```### Create a Route
Now we are going to create a FastAPI app with a route to get the user data.
```Python hl_lines="1 26 29-31"
from fastapi import FastAPI
from pydantic import BaseModelclass User(BaseModel):
username: str
password: str
is_active: bool = True
is_admin: bool = Falsejohndoe = User(
username="johndoe",
password="secret",
is_admin=False,
is_active=True,
)janedoe = User(
username="janedoe",
password="janeSecret",
is_admin=True,
is_active=True,
)app = FastAPI()
@app.get("/user/john", response_model=User)
async def get_john():
return johndoe
```This way, FastAPI will return all the user data, including the **password**, and it is not a good thing to do.
### Omitting a field
Now let's use the **Omit** function to return everything from the user **but** the password.
```Python hl_lines="4 31"
from fastapi import FastAPI
from pydantic import BaseModelfrom fastapi_vo import Omit
class User(BaseModel):
username: str
password: str
is_active: bool = True
is_admin: bool = Falsejohndoe = User(
username="johndoe",
password="secret",
is_admin=False,
is_active=True,
)janedoe = User(
username="janedoe",
password="janeSecret",
is_admin=True,
is_active=True,
)app = FastAPI()
@app.get("/user/john", response_model=Omit(User, "password"))
async def get_john():
return johndoe
```### Multiple variations of the same model
If you want to use multiple variations of the same class, you have to give it a new `classname` to avoid conflicts. Another approach is to assign it to a **variable** for reuse.
```Python hl_lines="1 15 35-37 40"
from typing import List
from fastapi import FastAPI
from pydantic import BaseModelfrom fastapi_vo import Omit
class User(BaseModel):
username: str
password: str
is_active: bool = True
is_admin: bool = FalseNoPassword = Omit(User, "password", classname="NoPasswordUser")
johndoe = User(
username="johndoe",
password="secret",
is_admin=False,
is_active=True,
)janedoe = User(
username="janedoe",
password="janeSecret",
is_admin=True,
is_active=True,
)app = FastAPI()
@app.get("/users", response_model=List[NoPassword])
async def list_users():
return [johndoe, janedoe]@app.get("/user/john", response_model=NoPassword)
async def get_john():
return johndoe
```### Picking a field
Now let's create a login route with another variation of the user model by picking some fields.
```Python hl_lines="5 16 46-50"
from typing import List
from fastapi import FastAPI
from pydantic import BaseModelfrom fastapi_vo import Omit, Pick
class User(BaseModel):
username: str
password: str
is_active: bool = True
is_admin: bool = FalseNoPassword = Omit(User, "password", classname="NoPasswordUser")
Auth = Pick(User, ["username", "password"], classname="Auth")johndoe = User(
username="johndoe",
password="secret",
is_admin=False,
is_active=True,
)janedoe = User(
username="janedoe",
password="janeSecret",
is_admin=True,
is_active=True,
)app = FastAPI()
@app.get("/users", response_model=List[NoPassword])
async def list_users():
return [johndoe, janedoe]@app.get("/user/john", response_model=NoPassword)
async def get_john():
return johndoe@app.get("/login", response_model=NoPassword)
async def login(user: Auth):
# some authentication logic in here
return user
```## License
This project is licensed under the terms of the MIT license.