fix id problems with day/winter switch

This commit is contained in:
Alex Bezdieniezhnykh
2025-02-26 22:09:07 +02:00
parent d1af7958f8
commit 58839933fc
28 changed files with 379 additions and 281 deletions
+28 -10
View File
@@ -1,6 +1,4 @@
import json
import os
import time
from http import HTTPStatus
from uuid import UUID
import jwt
@@ -10,7 +8,6 @@ from hardware_service cimport HardwareService, HardwareInfo
from security cimport Security
from io import BytesIO
from user cimport User, RoleEnum
from file_data cimport FileData
cdef class ApiClient:
"""Handles API authentication and downloading of the AI model."""
@@ -19,9 +16,8 @@ cdef class ApiClient:
self.user = None
self.token = None
cdef get_encryption_key(self, str hardware_hash):
cdef str key = f'{self.credentials.email}-{self.credentials.password}-{hardware_hash}-#%@AzaionKey@%#---'
return Security.calc_hash(key)
cdef set_credentials(self, Credentials credentials):
self.credentials = credentials
cdef login(self):
response = requests.post(f"{constants.API_URL}/login",
@@ -61,6 +57,20 @@ cdef class ApiClient:
self.login()
return self.user
cdef upload_file(self, str filename, str folder=None):
folder = folder or self.credentials.folder
if self.token is None:
self.login()
url = f"{constants.API_URL}/resources/{folder}"
headers = { "Authorization": f"Bearer {self.token}" }
files = dict(data=open(<str>filename, 'rb'))
try:
r = requests.post(url, headers=headers, files=files, allow_redirects=True)
r.raise_for_status()
print(f"Upload success: {r.status_code}")
except Exception as e:
print(f"Upload fail: {e}")
cdef load_bytes(self, str filename, str folder=None):
folder = folder or self.credentials.folder
hardware_service = HardwareService()
@@ -68,7 +78,6 @@ cdef class ApiClient:
if self.token is None:
self.login()
url = f"{constants.API_URL}/resources/get/{folder}"
headers = {
"Authorization": f"Bearer {self.token}",
@@ -82,7 +91,6 @@ cdef class ApiClient:
"fileName": filename
}, indent=4)
response = requests.post(url, data=payload, headers=headers, stream=True)
if response.status_code == HTTPStatus.UNAUTHORIZED or response.status_code == HTTPStatus.FORBIDDEN:
self.login()
headers = {
@@ -94,7 +102,8 @@ cdef class ApiClient:
if response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR:
print('500!')
key = self.get_encryption_key(hardware.hash)
hw_hash = Security.get_hw_hash(hardware)
key = Security.get_api_encryption_key(self.credentials, hw_hash)
stream = BytesIO(response.raw.read())
data = Security.decrypt_to(stream, key)
@@ -102,7 +111,16 @@ cdef class ApiClient:
return data
cdef load_ai_model(self):
return self.load_bytes(constants.AI_MODEL_FILE)
with open(<str>constants.AI_MODEL_FILE_BIG, 'rb') as binary_file:
encrypted_bytes_big = binary_file.read()
encrypted_bytes_small = self.load_bytes(constants.AI_MODEL_FILE_SMALL)
encrypted_model_bytes = encrypted_bytes_small + encrypted_bytes_big
key = Security.get_model_encryption_key()
model_bytes = Security.decrypt_to(BytesIO(encrypted_model_bytes), key)
cdef load_queue_config(self):
return self.load_bytes(constants.QUEUE_CONFIG_FILENAME).decode(encoding='utf-8')