|
| 1 | +""" |
| 2 | +Manages calls to the Storage API relating to buckets |
| 3 | +
|
| 4 | +Full documentation `here`. |
| 5 | +
|
| 6 | +.. _here: |
| 7 | + http://docs.keboola.apiary.io/#reference/buckets/ |
| 8 | +""" |
| 9 | +from kbcstorage.base import Endpoint |
| 10 | + |
| 11 | + |
| 12 | +class Buckets(Endpoint): |
| 13 | + """ |
| 14 | + Buckets Endpoint |
| 15 | + """ |
| 16 | + def __init__(self, root_url, token): |
| 17 | + """ |
| 18 | + Create a Buckets endpoint. |
| 19 | +
|
| 20 | + Args: |
| 21 | + root_url (:obj:`str`): The base url for the API. |
| 22 | + token (:obj:`str`): A storage API key. |
| 23 | + """ |
| 24 | + super().__init__(root_url, 'buckets', token) |
| 25 | + |
| 26 | + def list(self): |
| 27 | + """ |
| 28 | + List all buckets in project. |
| 29 | +
|
| 30 | + Returns: |
| 31 | + response_body: The parsed json from the HTTP response. |
| 32 | +
|
| 33 | + Raises: |
| 34 | + requests.HTTPError: If the API request fails. |
| 35 | + """ |
| 36 | + headers = {'X-StorageApi-Token': self.token} |
| 37 | + |
| 38 | + return self.get(self.base_url, headers=headers) |
| 39 | + |
| 40 | + def list_tables(self, bucket_id, include=None): |
| 41 | + """ |
| 42 | + List all tables in a bucket. |
| 43 | +
|
| 44 | + Args: |
| 45 | + bucket_id (str): Id of the bucket |
| 46 | + include (list): Properties to list (attributes, columns) |
| 47 | + Returns: |
| 48 | + response_body: The parsed json from the HTTP response. |
| 49 | +
|
| 50 | + Raises: |
| 51 | + requests.HTTPError: If the API request fails. |
| 52 | + """ |
| 53 | + headers = {'X-StorageApi-Token': self.token} |
| 54 | + |
| 55 | + url = '{}/{}/tables'.format(self.base_url, bucket_id) |
| 56 | + params = {} |
| 57 | + if include is not None and isinstance(include, list): |
| 58 | + params['include'] = ','.join(include) |
| 59 | + return self.get(url, headers=headers, params=params) |
| 60 | + |
| 61 | + def detail(self, bucket_id): |
| 62 | + """ |
| 63 | + Retrieves information about a given bucket. |
| 64 | +
|
| 65 | + Args: |
| 66 | + bucket_id (str): The id of the bucket. |
| 67 | +
|
| 68 | + Raises: |
| 69 | + requests.HTTPError: If the API request fails. |
| 70 | + """ |
| 71 | + url = '{}/{}'.format(self.base_url, bucket_id) |
| 72 | + headers = {'X-StorageApi-Token': self.token} |
| 73 | + |
| 74 | + return self.get(url, headers=headers) |
| 75 | + |
| 76 | + def create(self, name, stage='in', description='', backend=None): |
| 77 | + """ |
| 78 | + Create a new bucket. |
| 79 | +
|
| 80 | + Args: |
| 81 | + name (str): The new bucket name (only alphanumeric and underscores) |
| 82 | + stage (str): The new bucket stage. Can be one of ``in`` or ``out``. |
| 83 | + Default ``in``. |
| 84 | + description (str): The new bucket description. |
| 85 | + backend (str): The new bucket backend. Cand be one of |
| 86 | + ``snowflake``, ``redshift`` or ``mysql``. Default determined by |
| 87 | + project settings. |
| 88 | + Returns: |
| 89 | + response_body: The parsed json from the HTTP response. |
| 90 | +
|
| 91 | + Raises: |
| 92 | + requests.HTTPError: If the API request fails. |
| 93 | + """ |
| 94 | + # Separating create and link into two distinct functions... |
| 95 | + headers = { |
| 96 | + 'X-StorageApi-Token': self.token, |
| 97 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 98 | + } |
| 99 | + # Need to check args... |
| 100 | + body = { |
| 101 | + 'name': name, |
| 102 | + 'stage': stage, |
| 103 | + 'description': description, |
| 104 | + 'backend': backend |
| 105 | + } |
| 106 | + |
| 107 | + return self.post(self.base_url, headers=headers, data=body) |
| 108 | + |
| 109 | + def delete(self, bucket_id, force=False): |
| 110 | + """ |
| 111 | + Delete a bucket referenced by ``bucket_id``. |
| 112 | +
|
| 113 | + By default, only empty buckets without dependencies (aliases etc) can |
| 114 | + be deleted. The optional ``force`` parameter allows for the deletion |
| 115 | + of non-empty buckets. |
| 116 | +
|
| 117 | + Args: |
| 118 | + bucket_id (str): The id of the bucket to be deleted. |
| 119 | + force (bool): If ``True``, deletes the bucket even if it is not |
| 120 | + empty. Default ``False``. |
| 121 | + """ |
| 122 | + # How does the API handle it when force == False and the bucket is non- |
| 123 | + # empty? |
| 124 | + url = '{}/{}'.format(self.base_url, bucket_id) |
| 125 | + headers = {'X-StorageApi-Token': self.token} |
| 126 | + params = {'force': force} |
| 127 | + super().delete(url, headers=headers, params=params) |
| 128 | + |
| 129 | + def link(self, *args, **kwargs): |
| 130 | + """ |
| 131 | + **Not implemented** |
| 132 | +
|
| 133 | + Link an existing bucket from another project. |
| 134 | +
|
| 135 | + Creates a new bucket which contains the contents of a shared bucket in |
| 136 | + a source project. Linking a bucket from another project is only |
| 137 | + possible if it has been enabled in the project. |
| 138 | + """ |
| 139 | + raise NotImplementedError |
| 140 | + |
| 141 | + def share(self, *args, **kwargs): |
| 142 | + """ |
| 143 | + **Not implemented** |
| 144 | +
|
| 145 | + Enable sharing of a bucket. |
| 146 | +
|
| 147 | + The bucket will be shared to the entire organisation to which the |
| 148 | + project belongs. It may then be shared to any project of that |
| 149 | + organization. This operation is only available to administrator tokens. |
| 150 | + """ |
| 151 | + raise NotImplementedError |
| 152 | + |
| 153 | + def unshare(self, *args, **kwargs): |
| 154 | + """ |
| 155 | + **Not implemented** |
| 156 | +
|
| 157 | + Stop sharing a bucket. |
| 158 | +
|
| 159 | + The bucket must not be linked to other projects. To unshare an already |
| 160 | + linked bucket, the links must first be deleted - use ``delete`` on the |
| 161 | + bucket in the linking project. This operation is only available for |
| 162 | + administrator tokens. |
| 163 | + """ |
| 164 | + raise NotImplementedError |
0 commit comments