diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3a38173..2c3a845 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/README.md b/README.md index 709c9c6..90fa16e 100644 --- a/README.md +++ b/README.md @@ -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" ) diff --git a/edge_addons_api/client.py b/edge_addons_api/client.py index e8800df..994eacc 100644 --- a/edge_addons_api/client.py +++ b/edge_addons_api/client.py @@ -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 @@ -36,19 +35,15 @@ 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() @@ -56,14 +51,12 @@ def fetch_publish_status(self, operation_id: str) -> dict: 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() @@ -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(), }, ) @@ -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") @@ -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() @@ -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}" @@ -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, + } diff --git a/script.py b/script.py index 3e4f57a..9c69c19 100644 --- a/script.py +++ b/script.py @@ -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)