Skip to content

Commit 328d2b5

Browse files
[LABIMP-6908] Add export status checking and download URL fetching fu… (#1)
* [LABIMP-6908] Add export status checking and download URL fetching functionality * [LABIMP-6908] Refactor error messages for consistency and update API URLs to use BASE_URL constant .
1 parent 2bb0128 commit 328d2b5

File tree

1 file changed

+111
-6
lines changed

1 file changed

+111
-6
lines changed

labellerr/client.py

Lines changed: 111 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def get_direct_upload_url(self, file_name, client_id, purpose='pre-annotations')
7777
tracking_id = str(uuid.uuid4())
7878
logging.exception(f"Error getting direct upload url: {response.text}")
7979
raise LabellerrError({
80-
'status': 'internal server error',
80+
'status': 'Internal server error',
8181
'message': 'Please contact support with the request tracking id',
8282
'request_id': tracking_id
8383
})
@@ -827,7 +827,7 @@ def upload_preannotation_by_project_id(self,project_id,client_id,annotation_form
827827
raise LabellerrError(f"Failed to upload preannotation: {str(e)}")
828828

829829
def create_local_export(self,project_id,client_id,export_config):
830-
830+
unique_id = str(uuid.uuid4())
831831
required_params = ['export_name', 'export_description', 'export_format','statuses']
832832

833833
if project_id is None:
@@ -886,6 +886,109 @@ def create_local_export(self,project_id,client_id,export_config):
886886
logging.error(f"Failed to create local export: {str(e)}")
887887
raise LabellerrError(f"Failed to create local export: {str(e)}")
888888

889+
890+
def fetch_download_url(self, api_key, api_secret, project_id, uuid, export_id, client_id):
891+
try:
892+
headers = {
893+
'api_key': api_key,
894+
'api_secret': api_secret,
895+
'client_id': client_id,
896+
'origin': 'https://dev.labellerr.com',
897+
'source': 'sdk',
898+
'Content-Type': 'application/json'
899+
}
900+
901+
response = requests.get(
902+
url=f"{constants.BASE_URL}/exports/download",
903+
params={
904+
"client_id": client_id,
905+
"project_id": project_id,
906+
"uuid": uuid,
907+
"report_id": export_id
908+
},
909+
headers=headers
910+
)
911+
912+
if response.ok:
913+
return json.dumps(response.json().get("response"), indent=2)
914+
else:
915+
raise LabellerrError(f" Download request failed: {response.status_code} - {response.text}")
916+
except requests.exceptions.RequestException as e:
917+
logging.error(f"Failed to download export: {str(e)}")
918+
raise LabellerrError(f"Failed to download export: {str(e)}")
919+
except Exception as e:
920+
logging.error(f"Unexpected error in download_function: {str(e)}")
921+
raise LabellerrError(f"Unexpected error in download_function: {str(e)}")
922+
923+
924+
925+
926+
927+
def check_export_status(self, api_key, api_secret, project_id, report_ids, client_id):
928+
request_uuid = str(uuid.uuid4())
929+
try:
930+
if not project_id:
931+
raise LabellerrError("project_id cannot be null")
932+
if not report_ids or not isinstance(report_ids, list):
933+
raise LabellerrError("report_ids must be a non-empty list")
934+
935+
# Construct URL
936+
url = f"{constants.BASE_URL}/exports/status?project_id={project_id}&uuid={request_uuid}&client_id={client_id}"
937+
938+
# Headers
939+
headers = {
940+
'client_id': client_id,
941+
'api_key': api_key,
942+
'api_secret': api_secret,
943+
'origin': 'https://dev.labellerr.com',
944+
'source': 'sdk',
945+
'Content-Type': 'application/json'
946+
}
947+
948+
payload = json.dumps({
949+
"report_ids": report_ids
950+
})
951+
952+
response = requests.post(url, headers=headers, data=payload)
953+
954+
if response.status_code not in [200, 201]:
955+
if 400 <= response.status_code < 500:
956+
raise LabellerrError({'error': response.json(), 'code': response.status_code})
957+
elif response.status_code >= 500:
958+
raise LabellerrError({
959+
'status': 'Internal server error',
960+
'message': 'Please contact support with the request tracking id',
961+
'request_id': request_uuid
962+
})
963+
964+
result = response.json()
965+
966+
# Now process each report_id
967+
for status_item in result.get("status", []):
968+
if status_item.get("is_completed") and status_item.get("export_status") == "Created":
969+
# Download URL if job completed
970+
download_url = self.fetch_download_url(
971+
api_key=api_key,
972+
api_secret=api_secret,
973+
project_id=project_id,
974+
uuid=request_uuid,
975+
export_id=status_item["report_id"],
976+
client_id=client_id
977+
)
978+
979+
# Add download URL to response
980+
status_item["url"] = download_url
981+
982+
return json.dumps(result, indent=2)
983+
984+
except requests.exceptions.RequestException as e:
985+
logging.error(f"Failed to check export status: {str(e)}")
986+
raise LabellerrError(f"Failed to check export status: {str(e)}")
987+
except Exception as e:
988+
logging.error(f"Unexpected error checking export status: {str(e)}")
989+
raise LabellerrError(f"Unexpected error checking export status: {str(e)}")
990+
991+
889992
def create_project(self, project_name, data_type, client_id, dataset_id, annotation_template_id, rotation_config, created_by=None):
890993
"""
891994
Creates a project with the given configuration.
@@ -932,10 +1035,11 @@ def initiate_create_project(self, payload):
9321035
Orchestrates project creation by handling dataset creation, annotation guidelines,
9331036
and final project setup.
9341037
"""
1038+
9351039
try:
936-
937-
required_params = ['client_id', 'dataset_name', 'dataset_description', 'data_type',
938-
'created_by', 'project_name', 'annotation_guide', 'autolabel']
1040+
result = {}
1041+
# validate all the parameters
1042+
required_params = ['client_id', 'dataset_name', 'dataset_description', 'data_type', 'created_by', 'project_name','annotation_guide','autolabel']
9391043
for param in required_params:
9401044
if param not in payload:
9411045
raise LabellerrError(f"Required parameter {param} is missing")
@@ -1161,4 +1265,5 @@ def upload_folder_files_to_dataset(self, data_config):
11611265
except LabellerrError as e:
11621266
raise e
11631267
except Exception as e:
1164-
raise LabellerrError(f"Failed to upload files: {str(e)}")
1268+
raise LabellerrError(f"Failed to upload files: {str(e)}")
1269+

0 commit comments

Comments
 (0)