コード例 #1
0
ファイル: homework3.py プロジェクト: mateusz91t/PyLvlUp1
from datetime import date

from fastapi import APIRouter, Request, Response, Cookie, HTTPException, Depends
from fastapi.responses import RedirectResponse
from fastapi.security import HTTPBasicCredentials, HTTPBasic
from fastapi.templating import Jinja2Templates

from myservices.auxiliary_methods import get_token_hex, get_response_by_format

homework3 = APIRouter()

templates = Jinja2Templates(directory='templates')

basic = HTTPBasic()

homework3.secret_key = "secret should be long because it is more secjurne"
login, password = '******', 'NotSoSecurePa$$'
homework3.saved_tokens = list()
homework3.saved_cookies = list()


# http://127.0.0.1:8000/hello
@homework3.get("/hello")
def get_hello_html(request: Request):
    return templates.TemplateResponse(
        "hello.html.j2", dict(request=request, tdate=str(date.today())))


@homework3.post("/login_session", status_code=201)
def post_login_session(response: Response,
                       credentials: HTTPBasicCredentials = Depends(basic)):
コード例 #2
0
ファイル: lecture3.py プロジェクト: mateusz91t/PyLvlUp1
from hashlib import sha256
from typing import List

from fastapi import Request, Query, APIRouter, Response, Cookie, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates

lecture2 = APIRouter()

templates = Jinja2Templates(directory='templates')

lecture2.secret_key = "secret should be long because it is more secjurne"
lecture2.access_tokens = list()


# http://127.0.0.1:8000/lec2/request_query_string_discovery/?asd=2&aac=abc
@lecture2.get("/request_query_string_discovery/")
def get_all_query_params(request: Request):
    qp = request.query_params
    print(f"{qp = }")
    return qp


# http://127.0.0.1:8000/lec2/request_query_string_discovery2/?u=1&q=2&u=3&q=4
@lecture2.get("/request_query_string_discovery2/")
def get_all_query_params2(u: str = Query("default"),
                          q: List[int] = Query(None)):
    query_items = {"q": q, "u": u}
    return query_items

コード例 #3
0
MAX_QUEUE_SIZE = 3

router.counter = 0
router.__name__ = "First router"
router.fake_datebase = {}
start = date.today()


objects = {
    1: {"field_a": "a", "field_b": "b"},
    2: {"field_a": "a", "field_b": "b"},
    3: {"field_a": "a", "field_b": "b"},
    # .... #
}

router.secret_key = "very constatn and random secret, best 64+ characters"
router.access_tokens = []

security = HTTPBasic()


class HelloResp(BaseModel):
    msg: str


class Person(BaseModel):
    name: str
    surname: str


class RegisteredPerson(BaseModel):
コード例 #4
0
import secrets
from fastapi import APIRouter, HTTPException, Depends, Request, status
from fastapi.responses import RedirectResponse
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from hashlib import sha256
from functools import wraps
from fastapi.templating import Jinja2Templates

router = APIRouter()
security = HTTPBasic()
templates = Jinja2Templates(directory="templates")
router.secret_key = "QXV0aG9yaXphdGlvbiBCYXNpYyBsb2dpbjogdHJ1ZG5ZIHBhc3N3b3JkOiBQYUMx"


#dekorator
def to_authorize(to_authorize):
    @wraps(to_authorize)
    def inner(request: Request, *args, **kwargs):
        if request.cookies.get('session_token'):
            result = to_authorize(request, *args, **kwargs)
        else:
            result = RedirectResponse(url='/',
                                      status_code=status.HTTP_401_UNAUTHORIZED)
        return result

    return inner


@router.get('/')
def root():
    return {"message": "Welcome to my world!"}