Skip to content

Commit cc81865

Browse files
authored
Update to v1.1 API changes (#46)
1 parent 07bf240 commit cc81865

File tree

4 files changed

+26
-49
lines changed

4 files changed

+26
-49
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.4.0
3+
rev: v5.0.0
44
hooks:
55
- id: trailing-whitespace
66
- id: end-of-file-fixer
77
- id: check-yaml
88
- id: check-toml
99
- id: check-added-large-files
10-
- repo: https://github.com/psf/black
11-
rev: 23.7.0
12-
hooks:
13-
- id: black
1410
- repo: https://github.com/pre-commit/mirrors-isort
1511
rev: v5.10.1
1612
hooks:
1713
- id: isort
1814
additional_dependencies: [toml]
1915
- repo: https://github.com/pre-commit/mirrors-mypy
20-
rev: v1.5.1
16+
rev: v1.14.1
2117
hooks:
2218
- id: mypy
2319
additional_dependencies: [types-requests]
24-
- repo: https://github.com/astral-sh/ruff-pre-commit
25-
rev: v0.0.287
20+
- repo: https://github.com/charliermarsh/ruff-pre-commit
21+
rev: 'v0.9.4'
2622
hooks:
27-
- id: ruff
23+
- id: ruff
24+
args:
25+
- '--fix'
26+
- id: ruff-format

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ from edge_addons_api.client import Options, Client
1919
options = Options(
2020
product_id="Your product ID",
2121
client_id="Your client ID",
22-
client_secret="Your client secret",
22+
api_key="Your API key",
2323
access_token_url="Your access token URL"
2424
)
2525

edge_addons_api/client.py

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ class ResponseStatus:
2020
class Options:
2121
product_id: str
2222
client_id: str
23-
client_secret: str
24-
access_token_url: str
23+
api_key: str
2524
retry_count: int = 10
2625
sleep_seconds: int = 3
2726

@@ -36,34 +35,28 @@ def submit(self, file_path: str, notes: str) -> str:
3635
if not path.exists(file_path):
3736
raise FileNotFoundError(f"Unable to locate file at {file_path}")
3837

39-
access_token = self._get_access_token()
40-
operation_id = self._upload(file_path, access_token)
41-
self._check_upload(operation_id, access_token)
42-
return self._publish(notes, access_token)
38+
operation_id = self._upload(file_path)
39+
self._check_upload(operation_id)
40+
return self._publish(notes)
4341

4442
def fetch_publish_status(self, operation_id: str) -> dict:
4543
logger.debug(f"Fetching publish status for {operation_id}")
46-
access_token = self._get_access_token()
4744
response = requests.get(
4845
self._publish_status_endpoint(operation_id),
49-
headers={
50-
"Authorization": f"Bearer {access_token}",
51-
},
46+
headers=self._publish_default_headers(),
5247
)
5348

5449
response.raise_for_status()
5550

5651
logger.debug(f"Publish status response: {response.content.decode()}")
5752
return response.json()
5853

59-
def _publish(self, notes: str, access_token: str) -> str:
54+
def _publish(self, notes: str) -> str:
6055
logger.debug("Publishing")
6156
response = requests.post(
6257
self._publish_endpoint(),
6358
data={"notes": notes},
64-
headers={
65-
"Authorization": f"Bearer {access_token}",
66-
},
59+
headers=self._publish_default_headers(),
6760
)
6861

6962
response.raise_for_status()
@@ -72,15 +65,15 @@ def _publish(self, notes: str, access_token: str) -> str:
7265

7366
return response.headers["Location"]
7467

75-
def _upload(self, file_path: str, access_token: str) -> str:
68+
def _upload(self, file_path: str) -> str:
7669
logger.debug(f"Uploading {file_path}")
7770
with open(file_path, "rb") as f:
7871
response = requests.post(
7972
self._upload_endpoint(),
8073
data=f,
8174
headers={
82-
"Authorization": f"Bearer {access_token}",
8375
"Content-Type": "application/zip",
76+
**self._publish_default_headers(),
8477
},
8578
)
8679

@@ -93,7 +86,6 @@ def _upload(self, file_path: str, access_token: str) -> str:
9386
def _check_upload(
9487
self,
9588
operation_id,
96-
access_token: str,
9789
) -> str:
9890
logger.debug("Checking upload")
9991

@@ -106,9 +98,7 @@ def _check_upload(
10698
):
10799
response = requests.get(
108100
self._status_endpoint(operation_id),
109-
headers={
110-
"Authorization": f"Bearer {access_token}",
111-
},
101+
headers=self._publish_default_headers(),
112102
)
113103

114104
response.raise_for_status()
@@ -129,23 +119,6 @@ def _check_upload(
129119

130120
return upload_status
131121

132-
def _get_access_token(self) -> str:
133-
response = requests.post(
134-
self.options.access_token_url,
135-
data={
136-
"client_id": self.options.client_id,
137-
"scope": f"{self.BASE_URL}/.default",
138-
"client_secret": self.options.client_secret,
139-
"grant_type": "client_credentials",
140-
},
141-
)
142-
143-
response.raise_for_status()
144-
145-
json = response.json()
146-
147-
return json["access_token"]
148-
149122
def _product_endpoint(self) -> str:
150123
return f"{self.BASE_URL}/v1/products/{self.options.product_id}"
151124

@@ -160,3 +133,9 @@ def _status_endpoint(self, operation_id: str) -> str:
160133

161134
def _publish_status_endpoint(self, operation_id: str) -> str:
162135
return f"{self._publish_endpoint()}/operations/{operation_id}"
136+
137+
def _publish_default_headers(self):
138+
return {
139+
"Authorization": f"ApiKey {self.options.api_key}",
140+
"X-ClientID": self.options.client_id,
141+
}

script.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
options = Options(
2121
product_id=os.environ["EDGE_PRODUCT_ID"],
2222
client_id=os.environ["EDGE_CLIENT_ID"],
23-
client_secret=os.environ["EDGE_CLIENT_SECRET"],
24-
access_token_url=os.environ["EDGE_ACCESS_TOKEN_URL"],
23+
api_key=os.environ["EDGE_API_KEY"],
2524
)
2625

2726
client = Client(options)

0 commit comments

Comments
 (0)