Beispiel #1
0
import uvicorn
import sqlite3
from typing import Optional
from pydantic import BaseModel
from fastapi import FastAPI, Response

app = FastAPI()
app.db_connection = sqlite3.connect("northwind.db")


@app.on_event("startup")
async def startup():
    app.db_connection = sqlite3.connect("northwind.db")
    app.db_connection.text_factory = lambda b: b.decode(errors="ignore"
                                                        )  # northwind specific


@app.on_event("shutdown")
async def shutdown():
    app.db_connection.close()


@app.get("/categories", status_code=200)
async def categories():
    cursor = app.db_connection.cursor()
    cursor.row_factory = sqlite3.Row
    data = cursor.execute(
        "SELECT CategoryID, CategoryName FROM Categories").fetchall()
    return {
        "categories": [{
            "id": x['CategoryID'],
Beispiel #2
0
from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse, RedirectResponse
from hashlib import sha512, sha256

from fastapi.security import HTTPBasic, HTTPBasicCredentials
from pydantic.main import BaseModel

from views import router as northwind_api_router

app = FastAPI()
app.counter = 0
app.patient_id = 0
app.patients_register = dict()
app.secret_key = 0
app.login_tokens = collections.deque(maxlen=3)
app.session_tokens = collections.deque(maxlen=3)
app.db_connection = None

app.include_router(northwind_api_router, tags=["northwind"])

# 1st lecture


@app.get("/")
def root():
    return {"message": "Hello world!"}


@app.get("/hello/{name}")
def hello_name_view(name: str):
    return f"Hello {name}"