upload_file(public_api_client, file_path, artifact_type):
"""
Description
-----------
High level wrapper function to upload artifact.
It checks to upload file in chunks or not based on the size.
Args
----
public_api_client: QDC api client object.
file_path: Local path of the artifact to be uploaded.
artifact_type: Type of the artifact (TESTPACKAGE, TESTSCRIPT).
Returns
-------
uuid: uuid of the uploaded artifact.
None: In case of any failure.
Usages
------
apk_file_to_upload = "C:\\Temp\\test.apk"
# qdc_api is the name of the module
apk_uuid = qdc_api.upload_file(public_api_client, apk_file_to_upload, ArtifactType.TESTPACKAGE)
"""
if check_file_exists(file_path):
file_size = get_file_size(file_path)
file_name = get_filename(file_path)
public_api_client = _get_client_with_new_trace_header(public_api_client)
print(f"start uploading file:{file_name}, path:{file_path}, type:{artifact_type}, size:{file_size} bytes")
if file_size < CHUNK_SIZE:
uuid = single_upload(public_api_client, file_path, file_name, artifact_type)
if uuid is not None:
print(f"successfully uploaded file, uuid:{uuid}")
return uuid
else:
print(f"failed to upload file")
else:
print(f"starting chunk upload for large file")
uuid = start_upload(public_api_client, file_name, artifact_type)
if uuid is not None:
print(f"successfully started chunk upload, uuid:{uuid}")
if continue_upload(public_api_client, uuid, file_path, file_name, artifact_type):
print("successfully uploaded all chunks")
if end_upload(public_api_client, uuid):
print("successfully ended chunks upload")
return uuid
else:
print("failed to end chunks upload")
else:
print("failed to upload chunk")
else:
print("failed to start chunk upload")
else:
print(f"file path does not exists {file_path}, exiting...")
return None