trackoptimizer / test_api.py
smugalamunga's picture
upaded
c4fe061
Raw History Blame Contribute Delete
1.74 kB
import requests
import json
import os
from dotenv import load_dotenv
load_dotenv() #loads the .env file
def test_optimize():
url = "http://127.0.0.1:8000/optimize"
token = os.getenv("HF_TOKEN")
if not token:
print("WARNING: HF_TOKEN is not set! Check your .env file or environment variables.")
else:
print(f"Token loaded (starts with: {token[:5]}...)")
headers = {
"Authorization": f"Bearer {token}",
}
payload = {
"aptitudes": {
"Turf": "B",
"Dirt": "A",
"Sprint": "B",
"Mile": "A",
"Medium": "A",
"Long": "C",
},
"cutoff": "c",
"preferredDistance": "Mile",
"locked": {},
}
print(f"Sending request to {url}...")
try:
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
data = response.json()
# Save to file for review
output_file = "response_sample.json"
with open(output_file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4, ensure_ascii=False)
print(f"Success! Response saved to {output_file}")
race_count = 0
race_slots = data.get("slots", [])
for race in race_slots:
if race["race"] is not None:
race_count += 1
# Print a summary
print("\n--- Optimization Summary ---")
print(f"Status: {data.get('status')}")
print(f"Races Scheduled: {race_count}")
print(f"Epithets Achieved: {len(data.get('achievedEpithets', []))}")
print(f"Total Stats: {data.get('statTotal')}")
except requests.exceptions.ConnectionError:
print(
"Error: Could not connect to the server. Make sure 'python app.py' is running."
)
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
test_optimize()