Skip to content

Instantly share code, notes, and snippets.

View francbartoli's full-sized avatar

Francesco Bartoli francbartoli

View GitHub Profile
from fastapi import Security, Depends, FastAPI, HTTPException
from fastapi.security.api_key import APIKeyQuery, APIKeyCookie, APIKeyHeader, APIKey
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.openapi.utils import get_openapi
from starlette.status import HTTP_403_FORBIDDEN
from starlette.responses import RedirectResponse, JSONResponse
API_KEY = "1234567asdfgh"
API_KEY_NAME = "access_token"
from typing import Optional
from datetime import datetime, timedelta
import jwt
from jwt import PyJWTError
from fastapi import Depends, FastAPI, HTTPException
from fastapi.encoders import jsonable_encoder
from fastapi.security.oauth2 import (
OAuth2,
from typing import Optional
import base64
from passlib.context import CryptContext
from datetime import datetime, timedelta
import jwt
from jwt import PyJWTError
from pydantic import BaseModel
from fastapi import Security, Depends, FastAPI, HTTPException
from fastapi.security.api_key import APIKeyQuery, APIKeyCookie, APIKeyHeader, APIKey
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.openapi.utils import get_openapi
from starlette.status import HTTP_403_FORBIDDEN
from starlette.responses import RedirectResponse, JSONResponse
@francbartoli
francbartoli / code_samples.py
Created June 2, 2019 12:46 — forked from wshayes/code_samples.py
[FastAPI code samples] #fastapi
# Detecting multiple request methods
@app.api_route(methods=["PATCH", "POST"])
OR
@app.get('/profile')
@app.post('/profile')
async def profile(request: Request, api_key: str = Depends(get_api_key)):
if request.method == 'POST':
app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
@app.get("/")
async def homepage():
return "Welcome to the security test!"
@app.get("/openapi.json")
async def get_open_api_endpoint(current_user: User = Depends(get_current_active_user)):
return JSONResponse(get_openapi(title="FastAPI", version=1, routes=app.routes))
API_KEY = "1234567asdfgh"
API_KEY_NAME = "access_token"
COOKIE_DOMAIN = "localtest.me"
api_key_query = APIKeyQuery(name=API_KEY_NAME, auto_error=False)
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
api_key_cookie = APIKeyCookie(name=API_KEY_NAME, auto_error=False)
app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
@app.get("/")
async def homepage():
return "Welcome to the security test!"
@app.get("/openapi.json", tags=["documentation"])
async def get_open_api_endpoint(api_key: APIKey = Depends(get_api_key)):
response = JSONResponse(
get_openapi(title="FastAPI security test", version=1, routes=app.routes)
)
return response
@francbartoli
francbartoli / example.py
Created June 2, 2019 12:43 — forked from wshayes/example.py
[Repeat every] Example FastAPI code to run a function every X seconds #fastapi
# Decorator for fastapi
def repeat_every(*, seconds: float, wait_first: bool = False):
def decorator(func: Callable[[], Optional[Awaitable[None]]]):
is_coroutine = asyncio.iscoroutinefunction(func)
@wraps(func)
async def wrapped():
async def loop():
if wait_first:
await asyncio.sleep(seconds)
class Token(BaseModel):
access_token: str
token_type: str
class TokenData(BaseModel):
username: str = None
email: str = None