Skip to main content

Utilities to simplify FastAPI view objects

Project description

FastAPI-VO Logo

FastAPI-VO, view objects for FastAPI designed for simplicity.

Test Publish Coverage Package version


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

$ 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
from typing import List
from fastapi import FastAPI
from pydantic import BaseModel

from fastapi_vo import Omit, Pick


class User(BaseModel):
    username: str
    password: str
    is_active: bool = True
    is_admin: bool = False


Auth = 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
from pydantic import BaseModel


class User(BaseModel):
    username: str
    password: str
    is_active: bool = True
    is_admin: bool = False


johndoe = 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:

from pydantic import BaseModel


class User(BaseModel):
    username: str
    password: str
    is_active: bool = True
    is_admin: bool = False


johndoe = 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.

from fastapi import FastAPI
from pydantic import BaseModel


class User(BaseModel):
    username: str
    password: str
    is_active: bool = True
    is_admin: bool = False


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("/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.

from fastapi import FastAPI
from pydantic import BaseModel

from fastapi_vo import Omit


class User(BaseModel):
    username: str
    password: str
    is_active: bool = True
    is_admin: bool = False


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("/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.

from typing import List
from fastapi import FastAPI
from pydantic import BaseModel

from fastapi_vo import Omit


class User(BaseModel):
    username: str
    password: str
    is_active: bool = True
    is_admin: bool = False


NoPassword = 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.

from typing import List
from fastapi import FastAPI
from pydantic import BaseModel

from fastapi_vo import Omit, Pick


class User(BaseModel):
    username: str
    password: str
    is_active: bool = True
    is_admin: bool = False


NoPassword = 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.

Download files

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

Source Distribution

FastAPI-VO-0.1.4-1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

FastAPI_VO-0.1.4-1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file FastAPI-VO-0.1.4-1.tar.gz.

File metadata

  • Download URL: FastAPI-VO-0.1.4-1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.5

File hashes

Hashes for FastAPI-VO-0.1.4-1.tar.gz
Algorithm Hash digest
SHA256 8de5cd856291d73dc4cdd904dc1935af190f0d5319eff2e8ff5eb26c7bfa1996
MD5 cb92a9d86235d83a8e616130d4ae832a
BLAKE2b-256 93af7c8c21d8845a377b911cbe21ff9129ed6421bc8e80a244ca87d97e389da2

See more details on using hashes here.

File details

Details for the file FastAPI_VO-0.1.4-1-py3-none-any.whl.

File metadata

  • Download URL: FastAPI_VO-0.1.4-1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.5

File hashes

Hashes for FastAPI_VO-0.1.4-1-py3-none-any.whl
Algorithm Hash digest
SHA256 b4cb58dcf5baee67d32c282e9f0a2bc95a1270a8691a4ffd6c84e0d49129e00a
MD5 0a1f8fb5191ee05e7c6702e18333ea36
BLAKE2b-256 b38c8a59d186c993378371dc2adedcb018f114da5b651b0737c32e91e99748f0

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