1
+ import logging
2
+ import time
1
3
from dataclasses import dataclass
2
4
from os import path
3
5
4
6
import requests
5
7
8
+ from edge_addons_api .exceptions import UploadException
6
9
from edge_addons_api .responses import SubmitResponse
7
10
11
+ logger = logging .getLogger (__name__ )
12
+
13
+
14
+ class ResponseStatus :
15
+ SUCCEEDED = "Succeeded"
16
+ IN_PROGRESS = "InProgress"
17
+ FAILED = "Failed"
18
+
8
19
9
20
@dataclass
10
21
class Options :
@@ -26,10 +37,11 @@ def submit(self, file_path: str, notes: str) -> SubmitResponse:
26
37
raise FileNotFoundError (f"Unable to locate file at { file_path } " )
27
38
28
39
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 )
30
42
publish = self ._publish (notes , access_token )
31
43
32
- return SubmitResponse (upload , publish )
44
+ return SubmitResponse ({} , publish )
33
45
34
46
def _publish (self , notes : str , access_token : str ) -> dict :
35
47
response = requests .post (
@@ -42,9 +54,11 @@ def _publish(self, notes: str, access_token: str) -> dict:
42
54
43
55
response .raise_for_status ()
44
56
57
+ logger .debug (f"Publish response: { response .content .decode ()} " )
58
+
45
59
return response .json ()
46
60
47
- def _upload (self , file_path : str , access_token : str ) -> dict :
61
+ def _upload (self , file_path : str , access_token : str ) -> str :
48
62
49
63
files = {"file" : open (file_path , "rb" )}
50
64
@@ -59,7 +73,42 @@ def _upload(self, file_path: str, access_token: str) -> dict:
59
73
60
74
response .raise_for_status ()
61
75
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
63
112
64
113
def _get_access_token (self ) -> str :
65
114
response = requests .post (
@@ -86,3 +135,6 @@ def _publish_endpoint(self) -> str:
86
135
87
136
def _upload_endpoint (self ) -> str :
88
137
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