Skip to main content

Rest API configured with pydantic, backed by file database or mongo

Project description

FastAPI Restify

Python application

This project helps create a restful api using FastAPI and pydantic with storage engines including file and mongodb.

The project uses pydantic to map incoming request body data into a storage format.

The project provides

  • File database implementing list, get, insert, update, replace, delete
  • Mongo database implementing list, get, insert, update, replace, delete
  • FastAPI router with routes for each HTTP verb get, post, put, patch, delete
  • Authentication helpers including a FastAPI router for login and create user plus a token_listener dependancy for ensuring login on a route

This project is based on the example provided by https://github.com/Youngestdev/fastapi-mongo.

Usage

  1. First up install the package pip install fastapi_mongo_restify

  2. Then create a model object that extends MongoDatabase (or FileDatabase)

  • provides pydantic model classes as class variables, updateModelClass and insertModelClass. The insert model is applied to parsing POST requests and the update model is used for PUT/PATCH requests.
  • calls super().__init('collectionName') in it's constructor
from fastapi_restify.mongo_database import MongoDatabase

from pydantic import BaseModel, EmailStr, Field

# used to map request parameters on insert/post
class InsertOrderModel(BaseModel):
    name : str
    status: str
    time_started: int
    time_completed: int
        
# used to map request parameters on update/put
class UpdateOrderModel(BaseModel):
    id: int = Field(..., alias='_id')  # update model needs an id field aliased to _id
    name : str
    status: str
    time_started: int
    time_completed: int
   
class OrdersModel(MongoDatabase):
    # used by router for parameter mapping
    updateModelClass = UpdateOrderModel
    insertModelClass = InsertOrderModel
    
    def __init__(self, name = 'orders'): 
        super().__init__(name)
  1. Use the restify router in your app, passing an instance of your model to the router and specifying the url prefix.
from fastapi import FastAPI, Depends
from fastapi_restify.restful_router import get_router
from orders_model_mongo import OrdersModel
orders_model = OrdersModel()

app = FastAPI(title='Test FastAPI Mongo Restify')
app.include_router(get_router(orders_model), tags=["Orders"], prefix="/orders") 

  1. start your app with uvicorn
import uvicorn

if __name__ == '__main__':
    uvicorn.run('app:app', host="0.0.0.0", port=8080, reload=True)
  1. Open http://localhost:8080/docs to see the FASTAPI generated documentation for your API.

Authentication

To use authentication, include the admin router and token_listener. !! Note that authentication is incomplete and create_user is exposed as a public endpoint.

from fastapi_restify.auth.jwt_bearer import JWTBearer
from fastapi_restify.auth.admin_router import router as AdminRouter

app.include_router(AdminRouter, tags=["Administrator"], prefix="/admin")
token_listener = JWTBearer()
# protect the products endpoint
app.include_router(get_router(products_model), tags=["Products"], prefix="/products" , dependencies=[Depends(token_listener)])

Change Callbacks

The database classes can be called with a dict of callbacks with keys insert, update, replace, delete . When a data modification is applied the callback is triggered with dict parameter including modification type and modified record. This behavior is intended to facilitate websocket streaming of changes posted through the restful API.

TODO

https://pynamodb.readthedocs.io/en/latest/local.html

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_restify-0.0.4.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

fastapi_restify-0.0.4-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_restify-0.0.4.tar.gz.

File metadata

  • Download URL: fastapi_restify-0.0.4.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

File hashes

Hashes for fastapi_restify-0.0.4.tar.gz
Algorithm Hash digest
SHA256 36267b9b73d08bdb9bb993a1891f32d856e4e728375244a9d26a0fe02c39fa1e
MD5 ab2d6c94312106f8e04d4b85129e848f
BLAKE2b-256 69c36b2bb92fb360cfd7d3f34d3e2f9ad49fbfbdeb7760bc96a2eb0df5aa16a7

See more details on using hashes here.

File details

Details for the file fastapi_restify-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: fastapi_restify-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

File hashes

Hashes for fastapi_restify-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fee3c616b7ea1c6de709c8809dc460cd9b581a08b10d451754fdc337a15b725d
MD5 e374bd200f5b69138fd07586baba515b
BLAKE2b-256 c1234efdf66c1038cac37ebaa9dfbcf4d9b1c5a1360682eefda7643f4298cc1d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page