Skip to content

Commit

Permalink
api.config: add module to define various settings
Browse files Browse the repository at this point in the history
Instead of defining multiple settings class in different
API modules like `pubsub`, `auth`, `email_sender`,
add `api.config` module to store all different settings
classes.

Signed-off-by: Jeny Sadadia <[email protected]>
Signed-off-by: Guillaume Tucker <[email protected]>
  • Loading branch information
Jeny Sadadia authored and JenySadadia committed Nov 16, 2023
1 parent 8afe94f commit 936938e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 32 deletions.
12 changes: 2 additions & 10 deletions api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@
"""User authentication utilities"""

from passlib.context import CryptContext
from pydantic import BaseSettings
from fastapi_users.authentication import (
AuthenticationBackend,
BearerTransport,
JWTStrategy,
)


class Settings(BaseSettings):
"""Authentication settings"""
secret_key: str
algorithm: str = "HS256"
# Set to None so tokens don't expire
access_token_expire_seconds: int = None
from .config import AuthSettings


class Authentication:
Expand All @@ -29,7 +21,7 @@ class Authentication:
CRYPT_CTX = CryptContext(schemes=["bcrypt"], deprecated="auto")

def __init__(self, token_url: str):
self._settings = Settings()
self._settings = AuthSettings()
self._token_url = token_url

@classmethod
Expand Down
32 changes: 32 additions & 0 deletions api/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# Copyright (C) 2023 Collabora Limited
# Author: Jeny Sadadia <[email protected]>

"""Module settings"""

from pydantic import BaseSettings, EmailStr


class AuthSettings(BaseSettings):
"""Authentication settings"""
secret_key: str
algorithm: str = "HS256"
# Set to None so tokens don't expire
access_token_expire_seconds: float = None


class PubSubSettings(BaseSettings):
"""Pub/Sub settings loaded from the environment"""
cloud_events_source: str = "https://api.kernelci.org/"
redis_host: str = "redis"
redis_db_number: int = 1
keep_alive_period: int = 45


class EmailSettings(BaseSettings):
"""Email settings"""
smtp_host: str
smtp_port: int
email_sender: EmailStr
email_password: str
12 changes: 2 additions & 10 deletions api/email_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,13 @@
import email
import email.mime.text
import smtplib
from pydantic import BaseSettings, EmailStr


class Settings(BaseSettings):
"""Email settings"""
smtp_host: str
smtp_port: int
email_sender: EmailStr
email_password: str
from .config import EmailSettings


class EmailSender:
"""Class to send email report using SMTP"""
def __init__(self):
self._settings = Settings()
self._settings = EmailSettings()

def _smtp_connect(self):
"""Method to create a connection with SMTP server"""
Expand Down
13 changes: 3 additions & 10 deletions api/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@

import aioredis
from cloudevents.http import CloudEvent, to_json
from pydantic import BaseModel, BaseSettings, Field


class Settings(BaseSettings):
"""Pub/Sub settings loaded from the environment"""
cloud_events_source: str = "https://api.kernelci.org/"
redis_host: str = "redis"
redis_db_number: int = 1
keep_alive_period: int = 45
from pydantic import BaseModel, Field
from .config import PubSubSettings


class Subscription(BaseModel):
Expand Down Expand Up @@ -49,7 +42,7 @@ async def create(cls, *args, **kwargs):
return pubsub

def __init__(self, host=None, db_number=None):
self._settings = Settings()
self._settings = PubSubSettings()
if host is None:
host = self._settings.redis_host
if db_number is None:
Expand Down
4 changes: 2 additions & 2 deletions api/user_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
from beanie import PydanticObjectId
import jinja2
from .user_models import User
from .auth import Settings
from .config import AuthSettings
from .email_sender import EmailSender


class UserManager(ObjectIDIDMixin, BaseUserManager[User, PydanticObjectId]):
"""User management logic"""
settings = Settings()
settings = AuthSettings()
reset_password_token_secret = settings.secret_key
verification_token_secret = settings.secret_key

Expand Down

0 comments on commit 936938e

Please sign in to comment.