Files
julius_baer_onboarding/services/julius_baer_api_client.py

71 lines
2.4 KiB
Python
Raw Permalink Normal View History

2025-04-12 01:37:45 +02:00
import requests
2025-04-13 10:03:25 +02:00
import json
2025-04-12 02:15:19 +02:00
import config
import logging
from dto.requests import GameStartRequestDTO, GameDecisionRequestDTO
2025-04-12 10:45:17 +02:00
from dto.responses import GameStartResponseDTO, GameDecisionResponseDTO
2025-04-12 01:37:45 +02:00
2025-04-12 02:15:19 +02:00
class JuliusBaerApiClient:
2025-04-12 01:37:45 +02:00
"""
2025-04-12 02:15:19 +02:00
Client for interacting with the Julius Baer API service.
2025-04-12 01:37:45 +02:00
Provides methods to start a game and make game decisions.
"""
def __init__(self):
2025-04-12 02:15:19 +02:00
self.api_uri = config.API_URI
self.api_key = config.API_KEY
self.api_team = config.API_TEAM
2025-04-12 01:37:45 +02:00
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")
2025-04-12 01:37:45 +02:00
self.headers = {
2025-04-12 02:15:19 +02:00
"x-api-key": self.api_key,
"Content-Type": "application/json"
}
def start_game(self, game_start_request: GameStartRequestDTO) -> GameStartResponseDTO:
"""
Start a new game session.
"""
2025-04-12 13:59:59 +02:00
logging.debug("[+] Starting new game session")
start_uri = f"{self.api_uri}/game/start"
payload = game_start_request.model_dump_json()
try:
2025-04-13 10:22:38 +02:00
response = requests.post(start_uri, data=payload, headers=self.headers)
response.raise_for_status()
2025-04-13 10:22:38 +02:00
response_json = response.json()
validated_response = GameStartResponseDTO.model_validate(response_json)
logging.debug(f"Game started successfully. Session: {validated_response.session_id}, Client: {validated_response.client_id}")
return validated_response
except Exception as e:
logging.error(f"[!] Failed to start game session: {e}")
raise
2025-04-12 01:37:45 +02:00
2025-04-13 10:22:38 +02:00
def send_decision(self, game_decision_request: GameDecisionRequestDTO) -> GameDecisionResponseDTO :
2025-04-12 01:37:45 +02:00
"""
Make a game decision (Accept or Reject).
"""
2025-04-12 13:59:59 +02:00
logging.debug("[+] Sending decision")
decision_uri = f"{self.api_uri}/game/decision"
2025-04-12 01:37:45 +02:00
payload = game_decision_request.model_dump_json()
2025-04-12 01:37:45 +02:00
try:
2025-04-13 10:22:38 +02:00
response = requests.post(decision_uri, data=payload, headers=self.headers)
response.raise_for_status()
2025-04-12 01:37:45 +02:00
2025-04-13 10:22:38 +02:00
response_json = response.json()
validated_response = GameDecisionResponseDTO.model_validate(response_json)
logging.debug("[+] Decision sent successfully")
return validated_response
except Exception as e:
2025-04-12 11:51:09 +02:00
logging.error(f"[!] Failed to send a decision: {e}")
raise