From 7969caacaf174868f0cabf7c0cdab3800d29ca36 Mon Sep 17 00:00:00 2001 From: dylan <12473240+dlmw@users.noreply.github.com> Date: Sat, 12 Apr 2025 11:37:08 +0200 Subject: [PATCH] Refactor make_decision and add usage example in app.py --- app.py | 16 ++++++-- services/julius_baer_api_client.py | 59 +++++++++--------------------- 2 files changed, 29 insertions(+), 46 deletions(-) diff --git a/app.py b/app.py index 6a0f4c1..0adc89a 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,5 @@ from flask import Flask -from dto.requests import GameStartRequestDTO from services.julius_baer_api_client import JuliusBaerApiClient app = Flask(__name__) @@ -13,8 +12,17 @@ def hello_world(): # put application's code here if __name__ == '__main__': jb_client = JuliusBaerApiClient() - game_start_request = GameStartRequestDTO(player_name="Welch") - res = jb_client.start_game(game_start_request) - print(res) + # game_start_request = GameStartRequestDTO(player_name=config.API_TEAM) + # res = jb_client.start_game(game_start_request) + # + # game_decision_request = GameDecisionRequestDTO(decision="Accept", client_id=res.client_id, session_id=res.session_id) + # decision_response = jb_client.make_decision(game_decision_request) + # + # while decision_response.status == "active": + # game_decision_request = GameDecisionRequestDTO(decision="Accept", client_id=res.client_id, session_id=res.session_id) + # decision_response = jb_client.make_decision(game_decision_request) + # + # if decision_response.status == "gameover": + # logging.info("Game over") app.run() diff --git a/services/julius_baer_api_client.py b/services/julius_baer_api_client.py index b19db6e..a61315e 100644 --- a/services/julius_baer_api_client.py +++ b/services/julius_baer_api_client.py @@ -14,8 +14,6 @@ class JuliusBaerApiClient: """ def __init__(self): - self.client_id = None - self.session_id = None self.api_uri = config.API_URI self.api_key = config.API_KEY self.api_team = config.API_TEAM @@ -33,24 +31,18 @@ class JuliusBaerApiClient: Start a new game session. """ logging.info("[+] Starting new game session") - start_url = f"{self.api_uri}/game/start" + start_uri = f"{self.api_uri}/game/start" payload = game_start_request.model_dump() # Convert GameStartRequestDTO to dict for JSON try: - response = requests.post(start_url, json=payload, headers=self.headers) + response = requests.post(start_uri, json=payload, headers=self.headers) response.raise_for_status() # Raise exception for HTTP errors - response_data = response.json() - validated_response = GameStartResponseDTO.model_validate(response_data) + response_json = response.json() + validated_response = GameStartResponseDTO.model_validate(response_json) logging.info(f"Game started successfully. Session: {validated_response.session_id}, Client: {validated_response.client_id}") - - # 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 @@ -58,38 +50,21 @@ class JuliusBaerApiClient: def make_decision(self, game_decision_request: GameDecisionRequestDTO) -> GameDecisionResponseDTO: """ Make a game decision (Accept or Reject). - - Args: - decision: Either "Accept" or "Reject". - session_id: Unique session ID for the game. If None, uses the stored session_id. - client_id: Unique client ID for the game. If None, uses the stored client_id. - - Returns: - Dict containing the game decision response with status, score, etc. - - Raises: - ValueError: If decision is not "Accept" or "Reject". - ValueError: If session_id and client_id are not provided or stored from a previous start_game call. """ - if game_decision_request.decision not in ["Accept", "Reject"]: - raise ValueError('Decision must be either "Accept" or "Reject"') + logging.info("[+] Making decision") + decision_uri = f"{self.api_uri}/game/decision" - # Use stored values if not provided - session_id = game_decision_request.session_id or self.session_id - client_id = game_decision_request.client_id or self.client_id + payload = game_decision_request.model_dump_json() - if not session_id or not client_id: - raise ValueError( - "Session ID and Client ID are required. Either provide them explicitly or call start_game first.") + try: + response = requests.post(decision_uri, headers=self.headers, data=payload) + response.raise_for_status() - url = f"{self.base_url}/game/decision" - payload = { - "decision": game_decision_request.decision, - "session_id": session_id, - "client_id": client_id - } + response_json = response.json() + validated_response = GameDecisionResponseDTO.model_validate(response_json) + logging.info("Game decision made successfully") - response = requests.post(url, json=payload) - response.raise_for_status() # Raise exception for HTTP errors - - return response.json() + return validated_response + except Exception as e: + logging.error(f"[!] Failed to start game session: {e}") + raise