-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependencies.py
88 lines (67 loc) · 2.81 KB
/
dependencies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import secrets
import time
from typing import Annotated
from fastapi import Depends, Header, HTTPException, status
from fastapi.security import HTTPBasicCredentials
import httpx
import pendulum
from sqlmodel import Session
from werkzeug.datastructures import LanguageAccept
from werkzeug.http import parse_accept_header
from app_config import settings
from babel_config import babel, LANGUAGES
from database import engine
# Global dependency to set the preferred locale according to Accept-Language
async def get_locale(
accept_language: Annotated[str | None, Header()] = None,
x_user_locale: Annotated[str | None, Header()] = None):
babel.locale = (
x_user_locale or
LanguageAccept(parse_accept_header(accept_language)).best_match(LANGUAGES) or
'bg'
)
pendulum.set_locale(babel.locale)
# Uncomment below for testing
# babel.locale = 'bg'
# pendulum.set_locale('bg')
# Get user timezone for pendulum
async def get_user_timezone(x_user_timezone: Annotated[str | None, Header()] = None):
if x_user_timezone == 'Europe/Kyiv':
tz = 'Europe/Kiev'
return tz
return x_user_timezone
# Get session dependency to use with all models
async def get_session() -> Session:
with Session(engine) as session:
yield session
async def get_basic_credentials(credentials: Annotated[HTTPBasicCredentials, Depends(settings.basic_security)]):
current_username_bytes = credentials.username.encode('UTF-8')
correct_username_bytes = settings.basic_user.encode('UTF-8')
is_correct_username = secrets.compare_digest(current_username_bytes, correct_username_bytes)
current_password_bytes = credentials.password.encode('UTF-8')
correct_password_bytes = settings.basic_pass.encode('UTF-8')
is_correct_password = secrets.compare_digest(current_password_bytes, correct_password_bytes)
if not (is_correct_username and is_correct_password):
time.sleep(3)
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Basic"},
)
return True
async def entase_http_basic():
api_key = settings.entase_api_key
auth = httpx.BasicAuth(api_key, '')
return auth
async def get_entase_raw_data():
auth = await entase_http_basic()
entase_api_url = (f'https://api.entase.com/v2/events?' # f'&filter[productionID]={play.entase_id}'
f'filter[status][0]=1'
f'&extend[0]=productionTitle')
try:
async with httpx.AsyncClient() as client:
r = await client.get(entase_api_url, auth=auth)
entase_data = r.json()
except httpx.HTTPError as exc:
print(f'HTTP exception for {exc.request.url} - {exc}')
return entase_data