consolidate CommonSecurity to Common.dll

This commit is contained in:
Alex Bezdieniezhnykh
2025-06-13 23:06:48 +03:00
parent 904bc688ca
commit 8aa2f563a4
58 changed files with 362 additions and 151 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ cdef class ApiClient:
cdef set_token(self, str token)
cdef get_user(self)
cdef post(self, url, json)
cdef request(self, str method, str url, object payload, bint is_stream)
cdef list_files(self, str folder, str search_file)
cdef load_bytes(self, str filename, str folder)
cdef upload_file(self, str filename, bytes resource, str folder)
+11 -11
View File
@@ -47,7 +47,7 @@ cdef class ApiClient:
token = response.json()["token"]
self.set_token(token)
except HTTPError as e:
print(response.json())
constants.log(response.json())
if response.status_code == HTTPStatus.CONFLICT:
res = response.json()
raise Exception(res['Message'])
@@ -98,9 +98,9 @@ cdef class ApiClient:
constants.log(f"Upload fail: {e}")
cdef list_files(self, str folder, str search_file):
response = self.post(f'{self.api_url}/resources/list/{folder}', {
response = self.request('get', f'{self.api_url}/resources/list/{folder}', {
"search": search_file
})
}, is_stream=False)
constants.log(<str> f'Get files list by {folder}')
return response.json()
@@ -114,14 +114,14 @@ cdef class ApiClient:
"hardware": hardware,
"fileName": filename
}, indent=4)
response = self.post(f'{self.api_url}/resources/get/{folder}', payload)
response = self.request('post', f'{self.api_url}/resources/get/{folder}', payload, is_stream=True)
resp_bytes = response.raw.read()
data = Security.decrypt_to(resp_bytes, key)
constants.log(<str>f'Downloaded file: {filename}, {len(data)} bytes')
return data
cdef post(self, url, payload):
cdef request(self, str method, str url, object payload, bint is_stream):
if self.token is None:
self.login()
headers = {
@@ -129,14 +129,14 @@ cdef class ApiClient:
"Content-Type": "application/json"
}
response = requests.post(url, data=payload, headers=headers, stream=True)
response = requests.request(method, url, data=payload, headers=headers, stream=is_stream)
if response.status_code == HTTPStatus.UNAUTHORIZED or response.status_code == HTTPStatus.FORBIDDEN:
self.login()
headers = {
"Authorization": f"Bearer {self.token}",
"Content-Type": "application/json"
}
response = requests.post(url, data=payload, headers=headers, stream=True)
response = requests.request(method, url, data=payload, headers=headers, stream=is_stream)
if response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR:
raise Exception(f'Internal API error! {response.text}')
@@ -148,7 +148,7 @@ cdef class ApiClient:
return response
cdef load_big_file_cdn(self, str folder, str big_part):
print(f'downloading file {folder}\\{big_part} from cdn...')
constants.log(f'downloading file {folder}\\{big_part} from cdn...')
if self.cdn_manager.download(folder, big_part):
with open(path.join(<str> folder, big_part), 'rb') as binary_file:
encrypted_bytes_big = binary_file.read()
@@ -164,16 +164,16 @@ cdef class ApiClient:
key = Security.get_resource_encryption_key()
print(f'checking on existence for {folder}\\{big_part}')
constants.log(f'checking on existence for {folder}\\{big_part}')
if os.path.exists(os.path.join(<str> folder, big_part)):
with open(path.join(<str> folder, big_part), 'rb') as binary_file:
local_bytes_big = binary_file.read()
print(f'local file {folder}\\{big_part} is found!')
constants.log(f'local file {folder}\\{big_part} is found!')
try:
resource = Security.decrypt_to(encrypted_bytes_small + local_bytes_big, key)
return resource
except Exception as ex:
print('Local file doesnt match with api file, old version')
constants.log('Local file {folder}\\{big_part} doesnt match with api file, old version')
remote_bytes_big = self.load_big_file_cdn(folder, big_part)
return Security.decrypt_to(encrypted_bytes_small + remote_bytes_big, key)