Skip to content

Commit 1b2c95b

Browse files
authored
Handle upload errors (#5)
1 parent 6d09b7c commit 1b2c95b

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

edge_addons_api/client.py

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
import logging
2+
import time
13
from dataclasses import dataclass
24
from os import path
35

46
import requests
57

8+
from edge_addons_api.exceptions import UploadException
69
from 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
1021
class 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}"

edge_addons_api/exceptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import List
2+
3+
4+
class UploadException(BaseException):
5+
def __init__(self, status: str, message: str, error_code: str, errors: List):
6+
self.status = status
7+
self.message = message
8+
self.error_code = error_code
9+
self.errors = errors

script.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import os
23
import sys
34

@@ -10,6 +11,9 @@
1011
file_path = sys.argv[1]
1112
notes = sys.argv[2]
1213

14+
logging.basicConfig()
15+
logging.getLogger().setLevel(logging.DEBUG)
16+
1317
options = Options(
1418
product_id=os.environ["EDGE_PRODUCT_ID"],
1519
client_id=os.environ["EDGE_CLIENT_ID"],

0 commit comments

Comments
 (0)