1+ import logging
2+ import time
13from dataclasses import dataclass
24from os import path
35
46import requests
57
8+ from edge_addons_api .exceptions import UploadException
69from edge_addons_api .responses import SubmitResponse
710
11+ logger = logging .getLogger (__name__ )
12+
13+
14+ class ResponseStatus :
15+ SUCCEEDED = "Succeeded"
16+ IN_PROGRESS = "InProgress"
17+ FAILED = "Failed"
18+
819
920@dataclass
1021class Options :
@@ -26,10 +37,11 @@ def submit(self, file_path: str, notes: str) -> SubmitResponse:
2637 raise FileNotFoundError (f"Unable to locate file at { file_path } " )
2738
2839 access_token = self ._get_access_token ()
29- upload = self ._upload (file_path , access_token )
40+ operation_id = self ._upload (file_path , access_token )
41+ self ._check_upload (operation_id , access_token )
3042 publish = self ._publish (notes , access_token )
3143
32- return SubmitResponse (upload , publish )
44+ return SubmitResponse ({} , publish )
3345
3446 def _publish (self , notes : str , access_token : str ) -> dict :
3547 response = requests .post (
@@ -42,9 +54,11 @@ def _publish(self, notes: str, access_token: str) -> dict:
4254
4355 response .raise_for_status ()
4456
57+ logger .debug (f"Publish response: { response .content .decode ()} " )
58+
4559 return response .json ()
4660
47- def _upload (self , file_path : str , access_token : str ) -> dict :
61+ def _upload (self , file_path : str , access_token : str ) -> str :
4862
4963 files = {"file" : open (file_path , "rb" )}
5064
@@ -59,7 +73,42 @@ def _upload(self, file_path: str, access_token: str) -> dict:
5973
6074 response .raise_for_status ()
6175
62- return response .json ()
76+ return response .headers ["Location" ]
77+
78+ def _check_upload (
79+ self ,
80+ operation_id ,
81+ access_token : str ,
82+ retry_count : int = 5 ,
83+ sleep_seconds : int = 3 ,
84+ ) -> str :
85+ upload_status = ""
86+ attempts = 0
87+
88+ while upload_status != ResponseStatus .SUCCEEDED and attempts < retry_count :
89+ response = requests .get (
90+ self ._status_endpoint (operation_id ),
91+ headers = {
92+ "Authorization" : f"Bearer { access_token } " ,
93+ },
94+ )
95+
96+ response .raise_for_status ()
97+ response_json = response .json ()
98+
99+ logger .debug (f"Status response: { response_json } " )
100+ upload_status = response_json ["status" ]
101+ if upload_status == ResponseStatus .FAILED :
102+ raise UploadException (
103+ response_json ["status" ],
104+ response_json ["message" ],
105+ response_json ["errorCode" ],
106+ response_json ["errors" ],
107+ )
108+ elif upload_status == ResponseStatus .IN_PROGRESS :
109+ time .sleep (sleep_seconds )
110+
111+ return upload_status
63112
64113 def _get_access_token (self ) -> str :
65114 response = requests .post (
@@ -86,3 +135,6 @@ def _publish_endpoint(self) -> str:
86135
87136 def _upload_endpoint (self ) -> str :
88137 return f"{ self ._publish_endpoint ()} /draft/package"
138+
139+ def _status_endpoint (self , operation_id : str ) -> str :
140+ return f"{ self ._upload_endpoint ()} /operations/{ operation_id } "
0 commit comments