Skip to content

Commit

Permalink
add pydantic-settings
Browse files Browse the repository at this point in the history
  • Loading branch information
grillazz committed Nov 19, 2023
1 parent 6deccf0 commit 4d5abba
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 31 deletions.
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ services:
uvicorn greens.main:app
--host 0.0.0.0 --port 8989
--lifespan=on --use-colors --loop uvloop --http httptools
--log-config=log-conf.yaml
--reload
"
volumes:
Expand Down
34 changes: 14 additions & 20 deletions greens/config.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import os
from functools import lru_cache

from pydantic import BaseSettings

from greens.utils import get_logger

logger = get_logger(__name__)
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
"""
BaseSettings, from Pydantic, validates the data so that when we create an instance of Settings,
environment and testing will have types of str and bool, respectively.
Parameters:
Returns:
instance of Settings
Represents the configuration settings for the application.
Args:
environment (str): The environment in which the application is running. Defaults to "local".
testing (str): The testing mode of the application. Defaults to "0".
up (str): The up status of the application. Defaults to "up".
down (str): The down status of the application. Defaults to "down".
web_server (str): The web server used by the application. Defaults to "web_server".
db_url (str): The URL of the MongoDB database.
db_name (str): The name of the MongoDB database.
collection (str): The name of the MongoDB collection.
test_db_name (str): The name of the MongoDB test database.
"""

environment: str = os.getenv("ENVIRONMENT", "local")
testing: str = os.getenv("TESTING", "0")
up: str = os.getenv("UP", "up")
Expand All @@ -34,7 +31,4 @@ class Settings(BaseSettings):
test_db_name: str = os.getenv("MONGO_TEST_DB", "")


@lru_cache
def get_settings():
logger.info("Loading config settings from the environment...")
return Settings()
settings = Settings()
3 changes: 1 addition & 2 deletions greens/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from fastapi import FastAPI

from greens import config
from greens.config import settings as global_settings
from greens.routers import router as v1
from greens.services.repository import get_mongo_meta
from greens.utils import get_logger, init_mongo

global_settings = config.get_settings()

if global_settings.environment == "local":
get_logger("uvicorn")
Expand Down
3 changes: 1 addition & 2 deletions greens/routers/v1/vegs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
from fastapi.encoders import jsonable_encoder
from starlette.status import HTTP_201_CREATED

from greens import config
from greens.config import settings as global_settings
from greens.routers.exceptions import NotFoundHTTPException
from greens.schemas.vegs import Document, DocumentResponse, ObjectIdField
from greens.services.repository import create_document, retrieve_document

global_settings = config.get_settings()
collection = global_settings.collection

router = APIRouter()
Expand Down
4 changes: 2 additions & 2 deletions greens/schemas/vegs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def __get_validators__(cls):
def validate(cls, value):
try:
return ObjectId(str(value))
except InvalidId:
raise ValueError("Not a valid ObjectId")
except InvalidId as e:
raise ValueError("Not a valid ObjectId") from e


class Document(BaseModel):
Expand Down
4 changes: 1 addition & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import pytest
from httpx import AsyncClient

from greens import config
from greens.config import settings as global_settings
from greens.main import app, init_mongo
from greens.utils import get_logger

global_settings = config.get_settings()


@pytest.fixture(
params=[
Expand Down
2 changes: 1 addition & 1 deletion tests/test_routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
async def test_health_check(client: AsyncClient):
response = await client.get("/health-check")
assert response.status_code == status.HTTP_200_OK
assert response.json()["version"] == "6.0.4"
assert response.json()["version"] == "7.0.3"

0 comments on commit 4d5abba

Please sign in to comment.