Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-toml
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.10.1
hooks:
- id: isort
additional_dependencies: [toml]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.1
rev: v1.14.1
hooks:
- id: mypy
additional_dependencies: [types-requests]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.287
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.9.4'
hooks:
- id: ruff
- id: ruff
args:
- '--fix'
- id: ruff-format
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ from edge_addons_api.client import Options, Client
options = Options(
product_id="Your product ID",
client_id="Your client ID",
client_secret="Your client secret",
api_key="Your API key",
access_token_url="Your access token URL"
)

Expand Down
53 changes: 16 additions & 37 deletions edge_addons_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class ResponseStatus:
class Options:
product_id: str
client_id: str
client_secret: str
access_token_url: str
api_key: str
retry_count: int = 10
sleep_seconds: int = 3

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

access_token = self._get_access_token()
operation_id = self._upload(file_path, access_token)
self._check_upload(operation_id, access_token)
return self._publish(notes, access_token)
operation_id = self._upload(file_path)
self._check_upload(operation_id)
return self._publish(notes)

def fetch_publish_status(self, operation_id: str) -> dict:
logger.debug(f"Fetching publish status for {operation_id}")
access_token = self._get_access_token()
response = requests.get(
self._publish_status_endpoint(operation_id),
headers={
"Authorization": f"Bearer {access_token}",
},
headers=self._publish_default_headers(),
)

response.raise_for_status()

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

def _publish(self, notes: str, access_token: str) -> str:
def _publish(self, notes: str) -> str:
logger.debug("Publishing")
response = requests.post(
self._publish_endpoint(),
data={"notes": notes},
headers={
"Authorization": f"Bearer {access_token}",
},
headers=self._publish_default_headers(),
)

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

return response.headers["Location"]

def _upload(self, file_path: str, access_token: str) -> str:
def _upload(self, file_path: str) -> str:
logger.debug(f"Uploading {file_path}")
with open(file_path, "rb") as f:
response = requests.post(
self._upload_endpoint(),
data=f,
headers={
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/zip",
**self._publish_default_headers(),
},
)

Expand All @@ -93,7 +86,6 @@ def _upload(self, file_path: str, access_token: str) -> str:
def _check_upload(
self,
operation_id,
access_token: str,
) -> str:
logger.debug("Checking upload")

Expand All @@ -106,9 +98,7 @@ def _check_upload(
):
response = requests.get(
self._status_endpoint(operation_id),
headers={
"Authorization": f"Bearer {access_token}",
},
headers=self._publish_default_headers(),
)

response.raise_for_status()
Expand All @@ -129,23 +119,6 @@ def _check_upload(

return upload_status

def _get_access_token(self) -> str:
response = requests.post(
self.options.access_token_url,
data={
"client_id": self.options.client_id,
"scope": f"{self.BASE_URL}/.default",
"client_secret": self.options.client_secret,
"grant_type": "client_credentials",
},
)

response.raise_for_status()

json = response.json()

return json["access_token"]

def _product_endpoint(self) -> str:
return f"{self.BASE_URL}/v1/products/{self.options.product_id}"

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

def _publish_status_endpoint(self, operation_id: str) -> str:
return f"{self._publish_endpoint()}/operations/{operation_id}"

def _publish_default_headers(self):
return {
"Authorization": f"ApiKey {self.options.api_key}",
"X-ClientID": self.options.client_id,
}
3 changes: 1 addition & 2 deletions script.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
options = Options(
product_id=os.environ["EDGE_PRODUCT_ID"],
client_id=os.environ["EDGE_CLIENT_ID"],
client_secret=os.environ["EDGE_CLIENT_SECRET"],
access_token_url=os.environ["EDGE_ACCESS_TOKEN_URL"],
api_key=os.environ["EDGE_API_KEY"],
)

client = Client(options)
Expand Down
Loading