Skip to content

Commit b76f8c5

Browse files
committed
use config.settings attributes instead of os.getenv
APISettings() will load all values from .env file as APISettings's attributes. These can be imported in all other modules. ref: https://fastapi.tiangolo.com/advanced/settings/#reading-a-env-file This commit also fixes the issue of missing value of PADDLES_URL in services/helpers.py Signed-off-by: Vallari Agrawal <[email protected]>
1 parent f92d3f3 commit b76f8c5

File tree

6 files changed

+29
-17
lines changed

6 files changed

+29
-17
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ A REST API to execute [teuthology commands](https://docs.ceph.com/projects/teuth
2222
teuthology_api:
2323
build:
2424
context: ../../../teuthology-api
25+
env_file: ../../../teuthology-api/.env
2526
ports:
2627
- 8082:8080
2728
environment:

Diff for: src/teuthology_api/config.py

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ class APISettings(BaseSettings):
77
Class for API settings.
88
"""
99

10+
deployment: str = ""
11+
pulpito_url: str = ""
12+
paddles_url: str = ""
13+
14+
gh_client_id: str = ""
15+
gh_client_secret: str = ""
16+
gh_token_url: str = ""
17+
gh_authorization_base_url: str = ""
18+
gh_fetch_membership_url: str = ""
19+
20+
session_secret_key: str = ""
21+
1022
model_config = SettingsConfigDict(
1123
env_file=".env", env_file_encoding="utf-8", extra="ignore"
1224
)

Diff for: src/teuthology_api/main.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
from fastapi import FastAPI, Request
44
from fastapi.middleware.cors import CORSMiddleware
55
from starlette.middleware.sessions import SessionMiddleware
6-
from dotenv import load_dotenv
76

7+
from teuthology_api.config import settings
88
from teuthology_api.routes import suite, kill, login, logout
99

10-
load_dotenv()
1110

12-
DEPLOYMENT = os.getenv("DEPLOYMENT")
13-
SESSION_SECRET_KEY = os.getenv("SESSION_SECRET_KEY")
14-
PULPITO_URL = os.getenv("PULPITO_URL")
15-
PADDLES_URL = os.getenv("PADDLES_URL")
11+
DEPLOYMENT = settings.deployment
12+
SESSION_SECRET_KEY = settings.session_secret_key
13+
PULPITO_URL = settings.pulpito_url
14+
PADDLES_URL = settings.paddles_url
1615

1716
log = logging.getLogger(__name__)
1817
app = FastAPI()

Diff for: src/teuthology_api/routes/login.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
import os
33
from fastapi import APIRouter, HTTPException, Request
44
from fastapi.responses import RedirectResponse
5-
from dotenv import load_dotenv
65
import httpx
6+
from teuthology_api.config import settings
77

8-
load_dotenv()
98

10-
GH_CLIENT_ID = os.getenv("GH_CLIENT_ID")
11-
GH_CLIENT_SECRET = os.getenv("GH_CLIENT_SECRET")
12-
GH_AUTHORIZATION_BASE_URL = os.getenv("GH_AUTHORIZATION_BASE_URL")
13-
GH_TOKEN_URL = os.getenv("GH_TOKEN_URL")
14-
GH_FETCH_MEMBERSHIP_URL = os.getenv("GH_FETCH_MEMBERSHIP_URL")
15-
PULPITO_URL = os.getenv("PULPITO_URL")
9+
GH_CLIENT_ID = settings.gh_client_id
10+
GH_CLIENT_SECRET = settings.gh_client_secret
11+
GH_AUTHORIZATION_BASE_URL = settings.gh_authorization_base_url
12+
GH_TOKEN_URL = settings.gh_token_url
13+
GH_FETCH_MEMBERSHIP_URL = settings.gh_fetch_membership_url
14+
PULPITO_URL = settings.pulpito_url
1615

1716
log = logging.getLogger(__name__)
1817
router = APIRouter(

Diff for: src/teuthology_api/routes/logout.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import logging, os
1+
import logging
22
from fastapi import APIRouter, HTTPException, Request
33
from fastapi.responses import RedirectResponse
4+
from teuthology_api.config import settings
45

5-
PULPITO_URL = os.getenv("PULPITO_URL")
6+
PULPITO_URL = settings.pulpito_url
67
log = logging.getLogger(__name__)
78

89
router = APIRouter(

Diff for: src/teuthology_api/services/helpers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import requests # Note: import requests after teuthology
1212
from requests.exceptions import HTTPError
1313

14-
PADDLES_URL = os.getenv("PADDLES_URL")
14+
PADDLES_URL = settings.paddles_url
1515

1616
log = logging.getLogger(__name__)
1717

0 commit comments

Comments
 (0)