Skip to content

Commit 8077c58

Browse files
authored
Update API connection check and add test data function
Added a function to send test data payload to the Auth server for debugging. Updated connection check to use the environment variable for website IP.
1 parent a2c107d commit 8077c58

1 file changed

Lines changed: 37 additions & 5 deletions

File tree

src/modules/api.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from . import colours, utils
2222

2323
def is_connected() -> bool:
24+
from . import env
2425
"""
2526
Sends a get request to the Corebench server to
2627
see if it is online and determine whether it's worth
@@ -32,7 +33,7 @@ def is_connected() -> bool:
3233

3334
try:
3435
# Send the get request
35-
response = requests.get("https://corebench.me", timeout=5)
36+
response = requests.get(env.website_ip, timeout=5)
3637

3738
# Response is 200, connection successful
3839
if response.status_code == 200:
@@ -132,7 +133,7 @@ def get_file_hash():
132133
Compute the sha256 hash of the .env module.
133134
134135
Returns:
135-
sha256.hexdigest(): str - the hash of the main script
136+
sha256.hexdigest(): str - the hash of the env script
136137
"""
137138
# Create the sha256 object
138139
sha256 = hashlib.sha256()
@@ -253,6 +254,8 @@ def send_data_for_auth(
253254
headers=headers
254255
)
255256

257+
return response
258+
256259
def validate_api_key(api_key: str) -> requests.Response:
257260
from . import env
258261

@@ -373,10 +376,39 @@ def upload_and_return_status(
373376
else:
374377
# 403 = forbidden, invalid API key, banned, or rate-limited
375378
# detail will describe the issue specifically
376-
print(f"{colours.red()}{response.json()["detail"]}{colours.reset()}")
379+
print(f"{colours.red()}{colours.bold()}An error ocurred: {colours.reset()}{colours.red()}{response.json()["detail"]}{colours.reset()}")
377380

378-
return response.json()["detail"]
381+
return response.json().get("detail")
379382

380383
except Exception as e:
381384
# Notify the user that the server disconnected or the connection failed
382-
print(f"{colours.grey()}The server is offline.{colours.reset()}")
385+
print(f"{colours.grey()}The server is offline.{colours.reset()}")
386+
print(e)
387+
388+
389+
def send_test_data():
390+
"""
391+
Send a test data payload to the Auth server.
392+
Used for debugging/connection tests.
393+
"""
394+
# tell the user API has initialised
395+
print("Sending payload...")
396+
# upload the test data to the server
397+
status = upload_and_return_status(
398+
cpu_name="Test CPU",
399+
core_count=4,
400+
thread_count=8,
401+
memory_megabytes=8192,
402+
single_core_score=1000,
403+
multi_core_score=1000,
404+
multi_thread_score=1000,
405+
gigaflops_score=100,
406+
full_load_score=1000,
407+
overall_score=1000,
408+
distribution_name="Test OS",
409+
api_key="jeff"
410+
)
411+
412+
print("------")
413+
414+
# void function, no need to return data

0 commit comments

Comments
 (0)