create fastapi
This commit is contained in:
parent
a21dbb08d4
commit
f42e8e4807
6 changed files with 40 additions and 43 deletions
|
|
@ -24,7 +24,7 @@ classifiers = [
|
|||
"Programming Language :: Python :: Implementation :: CPython",
|
||||
"Programming Language :: Python :: Implementation :: PyPy",
|
||||
]
|
||||
dependencies = [ 'rich', 'sqlmodel', 'typer', 'iterfzf']
|
||||
dependencies = [ 'rich', 'sqlmodel', 'typer', 'iterfzf', 'fastapi', 'uvicorn']
|
||||
|
||||
[project.urls]
|
||||
Documentation = "https://github.com/waylonwalker/sqlmodel-base#readme"
|
||||
|
|
|
|||
|
|
@ -1,13 +1,14 @@
|
|||
import json
|
||||
from typing import Optional
|
||||
|
||||
import typer
|
||||
import uvicorn
|
||||
from fastapi import APIRouter, FastAPI
|
||||
from iterfzf import iterfzf
|
||||
from pydantic import BaseModel, validator
|
||||
from pydantic import BaseModel
|
||||
from pydantic_core._pydantic_core import PydanticUndefinedType
|
||||
from rich.console import Console
|
||||
from sqlalchemy import func
|
||||
from sqlmodel import Field, Session, SQLModel, create_engine, select
|
||||
from sqlmodel import Session, SQLModel, select
|
||||
|
||||
from sqlmodel_base.database import get_engine
|
||||
|
||||
|
|
@ -177,6 +178,31 @@ class Base(SQLModel):
|
|||
item.update()
|
||||
console.print(item)
|
||||
|
||||
@classmethod
|
||||
def api(cls):
|
||||
api = FastAPI(
|
||||
title="FastAPI",
|
||||
version="0.1.0",
|
||||
docs_url=None,
|
||||
redoc_url=None,
|
||||
openapi_url=None,
|
||||
# openapi_tags=tags_metadata,
|
||||
# dependencies=[Depends(set_user), Depends(set_prefers)],
|
||||
)
|
||||
|
||||
api.include_router(cls.router())
|
||||
|
||||
return api
|
||||
|
||||
@classmethod
|
||||
def router(cls):
|
||||
router = APIRouter()
|
||||
router.add_api_route("/get/", cls.get, methods=["GET"])
|
||||
router.add_api_route("/list/", cls.all, methods=["GET"])
|
||||
router.add_api_route("/create/", cls.interactive_create, methods=["POST"])
|
||||
router.add_api_route("/update/", cls.interactive_update, methods=["PUT"])
|
||||
return router
|
||||
|
||||
@classmethod
|
||||
@property
|
||||
def cli(cls):
|
||||
|
|
@ -206,37 +232,16 @@ class Base(SQLModel):
|
|||
)
|
||||
)
|
||||
|
||||
@app.command()
|
||||
def api():
|
||||
cls.run_api()
|
||||
|
||||
@app.command()
|
||||
def update():
|
||||
console.print(cls.interactive_update())
|
||||
|
||||
return app
|
||||
|
||||
|
||||
# replace with alembic commands
|
||||
def create_db_and_tables():
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
|
||||
def create_heroes():
|
||||
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson").create()
|
||||
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador").create()
|
||||
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48).create()
|
||||
|
||||
|
||||
def page_heroes():
|
||||
next_page = 1
|
||||
while next_page:
|
||||
page = Hero.get_page(page=next_page, page_size=2)
|
||||
console.print(page)
|
||||
next_page = page.next_page
|
||||
|
||||
|
||||
def main():
|
||||
create_db_and_tables()
|
||||
create_heroes()
|
||||
page_heroes()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@classmethod
|
||||
def run_api(cls):
|
||||
uvicorn.run(cls.api(), host="127.0.0.1", port=8000)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from functools import lru_cache
|
||||
|
||||
from sqlmodel import Field, Session, SQLModel, create_engine, select
|
||||
from sqlmodel import Session, SQLModel, create_engine
|
||||
|
||||
sqlite_file_name = "database.db"
|
||||
sqlite_url = f"sqlite:///{sqlite_file_name}"
|
||||
|
|
@ -8,8 +8,6 @@ sqlite_url = f"sqlite:///{sqlite_file_name}"
|
|||
|
||||
@lru_cache
|
||||
def get_engine():
|
||||
from sqlmodel_base.hero.models import Hero
|
||||
from sqlmodel_base.team.models import Team
|
||||
|
||||
engine = create_engine(sqlite_url)
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
|
|
|||
|
|
@ -1,12 +1,8 @@
|
|||
import json
|
||||
|
||||
import typer
|
||||
from iterfzf import iterfzf
|
||||
from rich.console import Console
|
||||
|
||||
from sqlmodel_base.database import get_engine
|
||||
from sqlmodel_base.hero.models import Hero
|
||||
from sqlmodel_base.team.models import Team
|
||||
|
||||
engine = get_engine()
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, validator
|
||||
from pydantic import validator
|
||||
from rich.console import Console
|
||||
from sqlalchemy import func
|
||||
from sqlmodel import Field, Session, SQLModel, create_engine, select
|
||||
from sqlmodel import Field
|
||||
|
||||
from sqlmodel_base.base import Base
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
from typing import Optional
|
||||
|
||||
from rich.console import Console
|
||||
from sqlmodel import Field
|
||||
|
||||
from sqlmodel_base.base import Base
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue