mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-06-21 19:23:56 +02:00
feat(config): Read connection from secrets.json file
This commit is contained in:
91
src/aki_prj23_transparenzregister/config/config_providers.py
Normal file
91
src/aki_prj23_transparenzregister/config/config_providers.py
Normal file
@ -0,0 +1,91 @@
|
||||
"""Wrappers for config providers."""
|
||||
|
||||
import abc
|
||||
import json
|
||||
import os
|
||||
|
||||
from aki_prj23_transparenzregister.config.config_template import PostgreConnectionString
|
||||
from aki_prj23_transparenzregister.utils.mongo.connector import MongoConnection
|
||||
|
||||
|
||||
class ConfigProvider(metaclass=abc.ABCMeta):
|
||||
"""Generic abstract class for a wrapper providing the config options for PostgreSQL and MongoDB."""
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_postgre_connection_string(self) -> PostgreConnectionString:
|
||||
"""Get PostgreSQL connection string.
|
||||
|
||||
Raises:
|
||||
NotImplementedError: To be defined by child classes
|
||||
|
||||
Returns:
|
||||
PostgreConnectionString: Connection details
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_mongo_connection_string(self) -> MongoConnection:
|
||||
"""Get MongoDB connection string.
|
||||
|
||||
Raises:
|
||||
NotImplementedError: To be defined by child classes
|
||||
|
||||
Returns:
|
||||
MongoConnection: Connection details
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class JsonFileConfigProvider(ConfigProvider):
|
||||
"""Config provider based on .json file."""
|
||||
|
||||
__data__: dict = {}
|
||||
|
||||
def __init__(self, file_path: str):
|
||||
"""Constructor reading it's data from given .json file.
|
||||
|
||||
Args:
|
||||
file_path (str): PATH to .json file containing config
|
||||
|
||||
Raises:
|
||||
FileNotFoundError: File does not exist
|
||||
TypeError: File could not be read or is malformed
|
||||
"""
|
||||
if not os.path.isfile(file_path):
|
||||
raise FileNotFoundError
|
||||
with open(file_path) as file:
|
||||
try:
|
||||
data = json.loads(file.read())
|
||||
self.__data__ = data
|
||||
except Exception as error:
|
||||
raise TypeError("File content is not a valid JSON object") from error
|
||||
|
||||
def get_postgre_connection_string(self) -> PostgreConnectionString:
|
||||
"""Read PostgreSQL connection string from .json file added in constructor.
|
||||
|
||||
Returns:
|
||||
PostgreConnectionString: Connection details
|
||||
"""
|
||||
details = self.__data__["postgres"]
|
||||
return PostgreConnectionString(
|
||||
details["username"],
|
||||
details["password"],
|
||||
details["host"],
|
||||
details["database"],
|
||||
details["port"],
|
||||
)
|
||||
|
||||
def get_mongo_connection_string(self) -> MongoConnection:
|
||||
"""Read MongodB connection string from .json file added in constructor.
|
||||
|
||||
Returns:
|
||||
MongoConnection: Connection details
|
||||
"""
|
||||
details = self.__data__["mongo"]
|
||||
return MongoConnection(
|
||||
details["hostname"],
|
||||
details["database"],
|
||||
details["port"],
|
||||
details["username"],
|
||||
details["password"],
|
||||
)
|
@ -4,11 +4,7 @@ from dataclasses import asdict, dataclass
|
||||
|
||||
@dataclass
|
||||
class News:
|
||||
"""_summary_.
|
||||
|
||||
Returns:
|
||||
_type_: _description_
|
||||
"""
|
||||
"""News data model."""
|
||||
|
||||
id: str
|
||||
title: str
|
||||
@ -17,9 +13,9 @@ class News:
|
||||
source_url: str
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""_summary_.
|
||||
"""Transform dataclass to dict.
|
||||
|
||||
Returns:
|
||||
dict: _description_
|
||||
dict: Transformed object
|
||||
"""
|
||||
return asdict(self)
|
||||
|
@ -3,6 +3,7 @@ from sqlalchemy import create_engine
|
||||
from sqlalchemy.engine import URL
|
||||
from sqlalchemy.orm import declarative_base
|
||||
|
||||
from aki_prj23_transparenzregister.config.config_providers import JsonFileConfigProvider
|
||||
from aki_prj23_transparenzregister.config.config_template import PostgreConnectionString
|
||||
|
||||
|
||||
@ -26,10 +27,8 @@ def get_engine(conn_args: PostgreConnectionString):
|
||||
|
||||
if __name__ == "__main__":
|
||||
"""Main flow creating tables"""
|
||||
conn_args = PostgreConnectionString(
|
||||
"postgres", "postgres", "localhost", "postgres", 5432
|
||||
)
|
||||
engine = get_engine(conn_args)
|
||||
config_provider = JsonFileConfigProvider("./secrets.json")
|
||||
engine = get_engine(config_provider.get_postgre_connection_string())
|
||||
with engine.connect() as connection:
|
||||
Base = declarative_base()
|
||||
|
||||
|
Reference in New Issue
Block a user