Skip to content

Commit

Permalink
feat(api/mymdc): add mymdc client settings to app settings
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertRosca committed Nov 5, 2024
1 parent 16e4225 commit 0953119
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
16 changes: 6 additions & 10 deletions api/src/damnit_api/metadata/mymdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from dotenv import dotenv_values

from .. import get_logger
from ..settings import settings

logger = get_logger()

Expand All @@ -14,24 +15,19 @@ class MyMDC:
"Content-Type": "application/json",
}

# TODO: Supply default env values
SERVER_URL = ENV_VARS.get("MYMDC_SERVER_URL", "")
CLIENT_ID = ENV_VARS.get("MYMDC_CLIENT_ID", "")
CLIENT_SECRET = ENV_VARS.get("MYMDC_CLIENT_SECRET", "")

token: str

def __init__(self):
logger.info("Initializing MyMDC client")
self.fetch_token()

def fetch_token(self):
url = f"{self.SERVER_URL}/api/../oauth/token"
url = f"{settings.mymdc.base_url}/api/../oauth/token"
logger.debug("Fetching MyMDC token", url=url)
body = {
"grant_type": "client_credentials",
"client_id": self.CLIENT_ID,
"client_secret": self.CLIENT_SECRET,
"client_id": settings.mymdc.client_id,
"client_secret": settings.mymdc.client_secret.get_secret_value(),
}

response = requests.post(url, headers=self.HEADERS, json=body)
Expand All @@ -43,14 +39,14 @@ def fetch_token(self):
return self.token

def fetch_proposal_info(self, proposal_num):
url = f"{self.SERVER_URL}/api/proposals/by_number/{proposal_num}"
url = f"{settings.mymdc.base_url}/api/proposals/by_number/{proposal_num}"
response = requests.get(url, headers=self.headers)
response.raise_for_status()

return response.json()

def fetch_user(self, user_id):
url = f"{self.SERVER_URL}/api/users/{user_id}"
url = f"{settings.mymdc.base_url}/api/users/{user_id}"
response = requests.get(url, headers=self.headers)
response.raise_for_status()
return response.json()
Expand Down
19 changes: 19 additions & 0 deletions api/src/damnit_api/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import UTC, datetime
from typing import Annotated

from pydantic import (
Expand Down Expand Up @@ -37,6 +38,22 @@ def factory_must_be_true(cls, v, values):
model_config = SettingsConfigDict(extra="allow")


class MyMdCCredentials(BaseSettings):
"""MyMdC client settings.
Get from from <https://in.xfel.eu/metadata/oauth/applications>.
"""

client_id: str
client_secret: SecretStr
email: str
token_url: HttpUrl
base_url: HttpUrl

_access_token: str = ""
_expires_at: datetime = datetime.fromisocalendar(1970, 1, 1).astimezone(UTC)


class Settings(BaseSettings):
auth: AuthSettings

Expand All @@ -48,6 +65,8 @@ class Settings(BaseSettings):

uvicorn: UvicornSettings = UvicornSettings()

mymdc: MyMdCCredentials

model_config = SettingsConfigDict(
env_prefix="DW_API_",
env_file=[".env"],
Expand Down

0 comments on commit 0953119

Please sign in to comment.