-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathdependencies.py
More file actions
120 lines (97 loc) · 4.41 KB
/
dependencies.py
File metadata and controls
120 lines (97 loc) · 4.41 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os
from dotenv import load_dotenv
from fastapi import Depends
from config.settings import (
TestingSettings,
Settings,
BaseAppSettings,
)
from notifications import (
EmailSenderInterface,
EmailSender,
)
from security.interfaces import JWTAuthManagerInterface
from security.token_manager import JWTAuthManager
from storages import (
S3StorageInterface,
S3StorageClient,
)
load_dotenv()
print(os.getenv("SECRET_KEY_ACCESS"))
def get_settings() -> BaseAppSettings:
"""
Retrieve the application settings based on the current environment.
This function reads the 'ENVIRONMENT' environment variable (defaulting to 'developing' if not set)
and returns a corresponding settings instance. If the environment is 'testing', it returns an instance
of TestingSettings; otherwise, it returns an instance of Settings.
Returns:
BaseAppSettings: The settings instance appropriate for the current environment.
"""
environment = os.getenv("ENVIRONMENT", "developing")
if environment == "testing":
return TestingSettings()
return Settings()
def get_jwt_auth_manager(settings: BaseAppSettings = Depends(get_settings)) -> JWTAuthManagerInterface:
"""
Create and return a JWT authentication manager instance.
This function uses the provided application settings to instantiate a JWTAuthManager, which implements
the JWTAuthManagerInterface. The manager is configured with secret keys for access and refresh tokens
as well as the JWT signing algorithm specified in the settings.
Args:
settings (BaseAppSettings, optional): The application settings instance.
Defaults to the output of get_settings().
Returns:
JWTAuthManagerInterface: An instance of JWTAuthManager configured with
the appropriate secret keys and algorithm.
"""
return JWTAuthManager(
secret_key_access=settings.SECRET_KEY_ACCESS,
secret_key_refresh=settings.SECRET_KEY_REFRESH,
algorithm=settings.JWT_SIGNING_ALGORITHM
)
def get_accounts_email_notificator(
settings: BaseAppSettings = Depends(get_settings)
) -> EmailSenderInterface:
"""
Retrieve an instance of the EmailSenderInterface configured with the application settings.
This function creates an EmailSender using the provided settings, which include details such as the email host,
port, credentials, TLS usage, and the directory and filenames for email templates. This allows the application
to send various email notifications (e.g., activation, password reset) as required.
Args:
settings (BaseAppSettings, optional): The application settings,
provided via dependency injection from `get_settings`.
Returns:
EmailSenderInterface: An instance of EmailSender configured with the appropriate email settings.
"""
return EmailSender(
hostname=settings.EMAIL_HOST,
port=settings.EMAIL_PORT,
email=settings.EMAIL_HOST_USER,
password=settings.EMAIL_HOST_PASSWORD,
use_tls=settings.EMAIL_USE_TLS,
template_dir=settings.PATH_TO_EMAIL_TEMPLATES_DIR,
activation_email_template_name=settings.ACTIVATION_EMAIL_TEMPLATE_NAME,
activation_complete_email_template_name=settings.ACTIVATION_COMPLETE_EMAIL_TEMPLATE_NAME,
password_email_template_name=settings.PASSWORD_RESET_TEMPLATE_NAME,
password_complete_email_template_name=settings.PASSWORD_RESET_COMPLETE_TEMPLATE_NAME
)
def get_s3_storage_client(
settings: BaseAppSettings = Depends(get_settings)
) -> S3StorageInterface:
"""
Retrieve an instance of the S3StorageInterface configured with the application settings.
This function instantiates an S3StorageClient using the provided settings, which include the S3 endpoint URL,
access credentials, and the bucket name. The returned client can be used to interact with an S3-compatible
storage service for file uploads and URL generation.
Args:
settings (BaseAppSettings, optional): The application settings,
provided via dependency injection from `get_settings`.
Returns:
S3StorageInterface: An instance of S3StorageClient configured with the appropriate S3 storage settings.
"""
return S3StorageClient(
endpoint_url=settings.S3_STORAGE_ENDPOINT,
access_key=settings.S3_STORAGE_ACCESS_KEY,
secret_key=settings.S3_STORAGE_SECRET_KEY,
bucket_name=settings.S3_BUCKET_NAME
)