|
| 1 | +from typing import Any, Dict |
| 2 | + |
| 3 | +import requests |
| 4 | +from requests import HTTPError |
| 5 | + |
| 6 | + |
| 7 | +class StorageBucketAPI: |
| 8 | + """This class abstracts access to the endpoint to the Get, List, Empty, and Delete operations on a bucket""" |
| 9 | + |
| 10 | + def __init__(self, url, headers): |
| 11 | + self.url = url |
| 12 | + self.headers = headers |
| 13 | + |
| 14 | + def list_buckets(self) -> Dict[str, Any]: |
| 15 | + """Retrieves the details of all storage buckets within an existing product.""" |
| 16 | + try: |
| 17 | + response = requests.get(f"{self.url}/bucket", headers=self.headers) |
| 18 | + response.raise_for_status() |
| 19 | + except HTTPError as http_err: |
| 20 | + print(f"HTTP error occurred: {http_err}") # Python 3.6 |
| 21 | + except Exception as err: |
| 22 | + print(f"Other error occurred: {err}") # Python 3.6 |
| 23 | + else: |
| 24 | + return response.json() |
| 25 | + |
| 26 | + def get_bucket(self, id: str) -> Dict[str, Any]: |
| 27 | + """Retrieves the details of an existing storage bucket. |
| 28 | +
|
| 29 | + Parameters |
| 30 | + ---------- |
| 31 | + id |
| 32 | + The unique identifier of the bucket you would like to retrieve. |
| 33 | + """ |
| 34 | + try: |
| 35 | + response = requests.get(f"{self.url}/bucket/{id}", headers=self.headers) |
| 36 | + response.raise_for_status() |
| 37 | + except HTTPError as http_err: |
| 38 | + print(f"HTTP error occurred: {http_err}") # Python 3.6 |
| 39 | + except Exception as err: |
| 40 | + print(f"Other error occurred: {err}") # Python 3.6 |
| 41 | + else: |
| 42 | + return response.json() |
| 43 | + |
| 44 | + def create_bucket(self, id: str) -> Dict[str, Any]: |
| 45 | + """Creates a new storage bucket. |
| 46 | +
|
| 47 | + Parameters |
| 48 | + ---------- |
| 49 | + id |
| 50 | + A unique identifier for the bucket you are creating. |
| 51 | + """ |
| 52 | + try: |
| 53 | + response = requests.post( |
| 54 | + f"{self.url}/bucket", data={"id": id}, headers=self.headers |
| 55 | + ) |
| 56 | + response.raise_for_status() |
| 57 | + except HTTPError as http_err: |
| 58 | + print(f"HTTP error occurred: {http_err}") # Python 3.6 |
| 59 | + except Exception as err: |
| 60 | + print(f"Other error occurred: {err}") # Python 3.6 |
| 61 | + else: |
| 62 | + return response.json() |
| 63 | + |
| 64 | + def empty_bucket(self, id: str) -> Dict[str, Any]: |
| 65 | + """Removes all objects inside a single bucket. |
| 66 | +
|
| 67 | + Parameters |
| 68 | + ---------- |
| 69 | + id |
| 70 | + The unique identifier of the bucket you would like to empty. |
| 71 | + """ |
| 72 | + try: |
| 73 | + response = requests.post( |
| 74 | + f"{self.url}/bucket/{id}/empty", data={}, headers=self.headers |
| 75 | + ) |
| 76 | + response.raise_for_status() |
| 77 | + except HTTPError as http_err: |
| 78 | + print(f"HTTP error occurred: {http_err}") # Python 3.6 |
| 79 | + except Exception as err: |
| 80 | + print(f"Other error occurred: {err}") # Python 3.6 |
| 81 | + else: |
| 82 | + return response.json() |
| 83 | + |
| 84 | + def delete_bucket(self, id: str) -> Dict[str, Any]: |
| 85 | + """Deletes an existing bucket. Note that you cannot delete buckets with existing objects inside. You must first |
| 86 | + `empty()` the bucket. |
| 87 | +
|
| 88 | + Parameters |
| 89 | + ---------- |
| 90 | + id |
| 91 | + The unique identifier of the bucket you would like to delete. |
| 92 | + """ |
| 93 | + try: |
| 94 | + response = requests.delete( |
| 95 | + f"{self.url}/bucket/{id}", data={}, headers=self.headers |
| 96 | + ) |
| 97 | + |
| 98 | + response.raise_for_status() |
| 99 | + except HTTPError as http_err: |
| 100 | + print(f"HTTP error occurred: {http_err}") # Python 3.6 |
| 101 | + except Exception as err: |
| 102 | + print(f"Other error occurred: {err}") # Python 3.6 |
| 103 | + else: |
| 104 | + return response.json() |
0 commit comments