refactor responses related dto with pydantic

This commit is contained in:
NoeBerdoz
2025-04-12 10:38:12 +02:00
parent 93fa2dd5ac
commit 176b23368d
2 changed files with 33 additions and 25 deletions

View File

@ -1,10 +1,9 @@
from dataclasses import dataclass from pydantic import BaseModel
from typing import Dict, Optional, Any from typing import Dict, Optional, Any
from uuid import UUID from uuid import UUID
@dataclass class GameStartResponseDTO(BaseModel):
class GameStartResponse:
"""Response model for a new game started.""" """Response model for a new game started."""
message: str message: str
session_id: UUID session_id: UUID
@ -14,8 +13,7 @@ class GameStartResponse:
score: int score: int
@dataclass class GameDecisionResponseDTO(BaseModel):
class GameDecisionResponse:
"""Response model for a game decision result.""" """Response model for a game decision result."""
status: str status: str
score: int score: int

View File

@ -1,7 +1,9 @@
import requests import requests
import config import config
import logging
from typing import Dict, Any from typing import Dict, Any
from dto.requests import GameStartRequestDTO, GameDecisionRequestDTO from dto.requests import GameStartRequestDTO, GameDecisionRequestDTO
from dto.responses import GameStartResponseDTO
class JuliusBaerApiClient: class JuliusBaerApiClient:
@ -12,38 +14,46 @@ class JuliusBaerApiClient:
""" """
def __init__(self): def __init__(self):
self.client_id = None
self.session_id = None
self.api_uri = config.API_URI self.api_uri = config.API_URI
self.api_key = config.API_KEY self.api_key = config.API_KEY
self.api_team = config.API_TEAM self.api_team = config.API_TEAM
def start_game(self, game_start_request: GameStartRequestDTO) -> Dict[str, Any]: if not self.api_uri or not self.api_key or not self.api_team:
""" logging.error("[!] API credentials are not configured, edit .env file")
Start a new game session.
Args: self.headers = {
player_name: Name of the player.
Returns:
Dict containing the game start response with session_id, player_id, etc.
"""
url = f"{self.api_uri}/game/start"
payload = {"player_name": game_start_request.player_name}
headers = {
"x-api-key": self.api_key, "x-api-key": self.api_key,
"Content-Type": "application/json" "Content-Type": "application/json"
} }
response = requests.post(url, json=payload, headers=headers) def start_game(self, game_start_request: GameStartRequestDTO) -> GameStartResponseDTO:
response.raise_for_status() # Raise exception for HTTP errors """
Start a new game session.
"""
logging.info("[+] Starting new game session")
start_url = f"{self.api_uri}/game/start"
payload = game_start_request.model_dump() # Convert GameStartRequestDTO to dict for JSON
data = response.json() try:
response = requests.post(start_url, json=payload, headers=self.headers)
response.raise_for_status() # Raise exception for HTTP errors
# Store session_id and client_id for convenience in future calls response_data = response.json()
self.session_id = data.get("session_id") validated_response = GameStartResponseDTO.model_validate(response_data)
self.client_id = data.get("client_id") logging.info(f"Game started successfully. Session: {validated_response.session_id}, Client: {validated_response.client_id}")
return data
# Store session_id and client_id for future calls
self.session_id = validated_response.session_id
self.client_id = validated_response.client_id
return validated_response
except Exception as e:
logging.error(f"[!] Failed to start game session: {e}")
raise
def make_decision(self, game_decision_request: GameDecisionRequestDTO) -> Dict[str, Any]: def make_decision(self, game_decision_request: GameDecisionRequestDTO) -> Dict[str, Any]:
""" """