|
| 1 | +from dataclasses import dataclass |
| 2 | + |
| 3 | +import requests |
| 4 | + |
| 5 | + |
| 6 | +@dataclass |
| 7 | +class Options: |
| 8 | + product_id: str |
| 9 | + client_id: str |
| 10 | + client_secret: str |
| 11 | + access_token_url: str |
| 12 | + |
| 13 | + |
| 14 | +class Client: |
| 15 | + |
| 16 | + BASE_URL = "https://api.addons.microsoftedge.microsoft.com" |
| 17 | + |
| 18 | + def __init__(self, options: Options): |
| 19 | + self.options = options |
| 20 | + |
| 21 | + def submit(self, file_path: str, notes: str): |
| 22 | + access_token = self._get_access_token() |
| 23 | + self._upload(file_path, access_token) |
| 24 | + self._publish(notes, access_token) |
| 25 | + |
| 26 | + def _publish(self, notes: str, access_token: str): |
| 27 | + response = requests.post( |
| 28 | + self._publish_endpoint(), |
| 29 | + data={"notes": notes}, |
| 30 | + headers={ |
| 31 | + "Authorization": f"Bearer {access_token}", |
| 32 | + }, |
| 33 | + ) |
| 34 | + |
| 35 | + response.raise_for_status() |
| 36 | + |
| 37 | + def _upload(self, file_path: str, access_token: str): |
| 38 | + |
| 39 | + files = {"file": open(file_path, "rb")} |
| 40 | + |
| 41 | + response = requests.post( |
| 42 | + self._upload_endpoint(), |
| 43 | + files=files, |
| 44 | + headers={ |
| 45 | + "Authorization": f"Bearer {access_token}", |
| 46 | + "Content-Type": "application/zip", |
| 47 | + }, |
| 48 | + ) |
| 49 | + |
| 50 | + response.raise_for_status() |
| 51 | + |
| 52 | + def _get_access_token(self) -> str: |
| 53 | + response = requests.post( |
| 54 | + self.options.access_token_url, |
| 55 | + data={ |
| 56 | + "client_id": self.options.client_id, |
| 57 | + "scope": f"{self.BASE_URL}/.default", |
| 58 | + "client_secret": self.options.client_secret, |
| 59 | + "grant_type": "client_credentials", |
| 60 | + }, |
| 61 | + ) |
| 62 | + |
| 63 | + response.raise_for_status() |
| 64 | + |
| 65 | + json = response.json() |
| 66 | + |
| 67 | + return json["access_token"] |
| 68 | + |
| 69 | + def _product_endpoint(self) -> str: |
| 70 | + return f"{self.BASE_URL}/v1/products/{self.options.product_id}" |
| 71 | + |
| 72 | + def _publish_endpoint(self) -> str: |
| 73 | + return f"{self._product_endpoint()}/submissions" |
| 74 | + |
| 75 | + def _upload_endpoint(self) -> str: |
| 76 | + return f"{self._publish_endpoint()}/draft/package" |
0 commit comments