add pxd headers for correct work

fixes definitions
can run until API call
This commit is contained in:
Alex Bezdieniezhnykh
2025-01-16 17:56:58 +02:00
parent 7439005ed7
commit e21dd7e70f
17 changed files with 207 additions and 165 deletions
+13 -20
View File
@@ -1,47 +1,41 @@
# cython: language_level=3
import io
import os
from http import HTTPStatus
import requests
import constants
from hardware_service import HardwareService
from processor_command import FileCommand, CommandType
cimport constants
from hardware_service cimport HardwareService, HardwareInfo
from security import Security
cdef class ApiClient:
"""Handles API authentication and downloading of the AI model."""
cdef str email
cdef str password
cdef str token
cdef str folder
def __init__(self, str email, str password, str folder):
self.email = email
self.password = password
self.folder = folder
if os.path.exists(constants.TOKEN_FILE):
with open(constants.TOKEN_FILE, "r") as file:
if os.path.exists(<str>constants.TOKEN_FILE):
with open(<str>constants.TOKEN_FILE, "r") as file:
self.token = file.read().strip()
else:
self.token = None
cdef get_encryption_key(self, command: FileCommand, str hardware_hash):
cdef get_encryption_key(self, str hardware_hash):
return f'{self.email}-{self.password}-{hardware_hash}-#%@AzaionKey@%#---'
cdef login(self, str email, str password, persist_token:bool=False):
cdef login(self, str email, str password, bint persist_token=False):
response = requests.post(f"{constants.API_URL}/login", json={"email": email, "password": password})
response.raise_for_status()
self.token = response.json()["token"]
print(f'')
if persist_token:
with open(constants.TOKEN_FILE, 'w') as file:
with open(<str>constants.TOKEN_FILE, 'w') as file:
file.write(self.token)
cdef bytes load_file(self, command: FileCommand, persist_token:bool=False):
cdef bytes load_file(self, str filename, bint persist_token=False):
hardware_service = HardwareService()
hardware = hardware_service.get_hardware_info()
cdef HardwareInfo hardware = hardware_service.get_hardware_info()
if self.token is None:
self.login(self.email, self.password, persist_token)
@@ -51,7 +45,7 @@ cdef class ApiClient:
payload = {
"password": self.password,
"hardware": hardware,
"fileName": command.filename
"fileName": filename
}
response = requests.post(url, json=payload, headers=headers, stream=True)
@@ -59,13 +53,12 @@ cdef class ApiClient:
self.login(self.email, self.password, persist_token)
response = requests.post(url, json=payload, headers=headers, stream=True)
key = self.get_encryption_key(command, hardware.hash)
key = self.get_encryption_key(hardware.hash)
encrypted_stream = io.BytesIO(response.content)
decrypted_stream = io.BytesIO()
Security.decrypt_to(encrypted_stream, decrypted_stream, key)
return decrypted_stream
cdef bytes load_ai_model(self):
file_command = FileCommand(CommandType.LOAD, constants.AI_MODEL_FILE)
return self.load_file(file_command, True)
return self.load_file(constants.AI_MODEL_FILE, <bint>True)