Add sample request using api client
This commit is contained in:
11
app.py
11
app.py
@ -1,7 +1,9 @@
|
|||||||
import os
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
|
||||||
|
from dto.requests import GameStartRequest
|
||||||
|
from services.julius_baer_api_client import JuliusBaerApiClient
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -11,4 +13,9 @@ def hello_world(): # put application's code here
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
jb_client = JuliusBaerApiClient()
|
||||||
|
game_start_request = GameStartRequest("Welch")
|
||||||
|
res = jb_client.start_game(game_start_request)
|
||||||
|
print(res)
|
||||||
|
|
||||||
app.run()
|
app.run()
|
||||||
|
23
config.py
23
config.py
@ -1,19 +1,8 @@
|
|||||||
from flask.cli import load_dotenv
|
import os
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
class Config:
|
load_dotenv()
|
||||||
"""
|
API_URI = str(os.getenv("API_URI") or "")
|
||||||
Configuration manager for the Julius Baer API client.
|
API_KEY = str(os.getenv("API_KEY") or "")
|
||||||
|
API_TEAM = str(os.getenv("API_TEAM") or "")
|
||||||
Reads configuration from a YAML file and overrides values
|
|
||||||
with environment variables when available.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
"""
|
|
||||||
Initialize the configuration manager.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
config_file_path: Path to the YAML configuration file. Defaults to "config.yaml".
|
|
||||||
"""
|
|
||||||
load_dotenv()
|
|
@ -1,27 +1,22 @@
|
|||||||
import requests
|
from typing import Dict, Any
|
||||||
import uuid
|
|
||||||
from typing import Dict, Any, Optional
|
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
import config
|
||||||
from dto.requests import GameStartRequest, GameDecisionRequest
|
from dto.requests import GameStartRequest, GameDecisionRequest
|
||||||
|
|
||||||
|
|
||||||
class GameApiClient:
|
class JuliusBaerApiClient:
|
||||||
"""
|
"""
|
||||||
Client for interacting with the Game API service.
|
Client for interacting with the Julius Baer API service.
|
||||||
|
|
||||||
Provides methods to start a game and make game decisions.
|
Provides methods to start a game and make game decisions.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""
|
self.api_uri = config.API_URI
|
||||||
Initialize the Game API client.
|
self.api_key = config.API_KEY
|
||||||
|
self.api_team = config.API_TEAM
|
||||||
Args:
|
|
||||||
base_url: Base URL for the API service. Defaults to "http://localhost:5000".
|
|
||||||
"""
|
|
||||||
# TODO: import base_url from config self.base_url = base_url.rstrip('/')
|
|
||||||
self.session_id = None
|
|
||||||
self.client_id = None
|
|
||||||
|
|
||||||
def start_game(self, game_start_request: GameStartRequest) -> Dict[str, Any]:
|
def start_game(self, game_start_request: GameStartRequest) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
@ -33,10 +28,15 @@ class GameApiClient:
|
|||||||
Returns:
|
Returns:
|
||||||
Dict containing the game start response with session_id, player_id, etc.
|
Dict containing the game start response with session_id, player_id, etc.
|
||||||
"""
|
"""
|
||||||
url = f"{self.base_url}/game/start"
|
url = f"{self.api_uri}/game/start"
|
||||||
payload = {"player_name": game_start_request.player_name}
|
payload = {"player_name": game_start_request.player_name}
|
||||||
|
|
||||||
response = requests.post(url, json=payload)
|
headers = {
|
||||||
|
"x-api-key": self.api_key,
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
response = requests.post(url, json=payload, headers=headers)
|
||||||
response.raise_for_status() # Raise exception for HTTP errors
|
response.raise_for_status() # Raise exception for HTTP errors
|
||||||
|
|
||||||
data = response.json()
|
data = response.json()
|
||||||
@ -71,7 +71,8 @@ class GameApiClient:
|
|||||||
client_id = game_decision_request.client_id or self.client_id
|
client_id = game_decision_request.client_id or self.client_id
|
||||||
|
|
||||||
if not session_id or not client_id:
|
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.")
|
raise ValueError(
|
||||||
|
"Session ID and Client ID are required. Either provide them explicitly or call start_game first.")
|
||||||
|
|
||||||
url = f"{self.base_url}/game/decision"
|
url = f"{self.base_url}/game/decision"
|
||||||
payload = {
|
payload = {
|
||||||
|
Reference in New Issue
Block a user