Refactor make_decision and add usage example in app.py
This commit is contained in:
16
app.py
16
app.py
@ -1,6 +1,5 @@
|
|||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
|
||||||
from dto.requests import GameStartRequestDTO
|
|
||||||
from services.julius_baer_api_client import JuliusBaerApiClient
|
from services.julius_baer_api_client import JuliusBaerApiClient
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@ -13,8 +12,17 @@ def hello_world(): # put application's code here
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
jb_client = JuliusBaerApiClient()
|
jb_client = JuliusBaerApiClient()
|
||||||
game_start_request = GameStartRequestDTO(player_name="Welch")
|
# game_start_request = GameStartRequestDTO(player_name=config.API_TEAM)
|
||||||
res = jb_client.start_game(game_start_request)
|
# res = jb_client.start_game(game_start_request)
|
||||||
print(res)
|
#
|
||||||
|
# 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()
|
app.run()
|
||||||
|
@ -14,8 +14,6 @@ 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
|
||||||
@ -33,24 +31,18 @@ class JuliusBaerApiClient:
|
|||||||
Start a new game session.
|
Start a new game session.
|
||||||
"""
|
"""
|
||||||
logging.info("[+] Starting 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
|
payload = game_start_request.model_dump() # Convert GameStartRequestDTO to dict for JSON
|
||||||
|
|
||||||
try:
|
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.raise_for_status() # Raise exception for HTTP errors
|
||||||
|
|
||||||
response_data = response.json()
|
response_json = response.json()
|
||||||
validated_response = GameStartResponseDTO.model_validate(response_data)
|
validated_response = GameStartResponseDTO.model_validate(response_json)
|
||||||
logging.info(f"Game started successfully. Session: {validated_response.session_id}, Client: {validated_response.client_id}")
|
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
|
return validated_response
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"[!] Failed to start game session: {e}")
|
logging.error(f"[!] Failed to start game session: {e}")
|
||||||
raise
|
raise
|
||||||
@ -58,38 +50,21 @@ class JuliusBaerApiClient:
|
|||||||
def make_decision(self, game_decision_request: GameDecisionRequestDTO) -> GameDecisionResponseDTO:
|
def make_decision(self, game_decision_request: GameDecisionRequestDTO) -> GameDecisionResponseDTO:
|
||||||
"""
|
"""
|
||||||
Make a game decision (Accept or Reject).
|
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"]:
|
logging.info("[+] Making decision")
|
||||||
raise ValueError('Decision must be either "Accept" or "Reject"')
|
decision_uri = f"{self.api_uri}/game/decision"
|
||||||
|
|
||||||
# Use stored values if not provided
|
payload = game_decision_request.model_dump_json()
|
||||||
session_id = game_decision_request.session_id or self.session_id
|
|
||||||
client_id = game_decision_request.client_id or self.client_id
|
|
||||||
|
|
||||||
if not session_id or not client_id:
|
try:
|
||||||
raise ValueError(
|
response = requests.post(decision_uri, headers=self.headers, data=payload)
|
||||||
"Session ID and Client ID are required. Either provide them explicitly or call start_game first.")
|
response.raise_for_status()
|
||||||
|
|
||||||
url = f"{self.base_url}/game/decision"
|
response_json = response.json()
|
||||||
payload = {
|
validated_response = GameDecisionResponseDTO.model_validate(response_json)
|
||||||
"decision": game_decision_request.decision,
|
logging.info("Game decision made successfully")
|
||||||
"session_id": session_id,
|
|
||||||
"client_id": client_id
|
|
||||||
}
|
|
||||||
|
|
||||||
response = requests.post(url, json=payload)
|
return validated_response
|
||||||
response.raise_for_status() # Raise exception for HTTP errors
|
except Exception as e:
|
||||||
|
logging.error(f"[!] Failed to start game session: {e}")
|
||||||
return response.json()
|
raise
|
||||||
|
Reference in New Issue
Block a user