|
| 1 | +""" |
| 2 | +Manages calls to the Storage API relating to configurations metadata |
| 3 | +
|
| 4 | +Full documentation https://keboola.docs.apiary.io/#reference/metadata/components-configurations-metadata/ |
| 5 | +""" |
| 6 | +import json |
| 7 | +from kbcstorage.base import Endpoint |
| 8 | + |
| 9 | + |
| 10 | +class ConfigurationsMetadata(Endpoint): |
| 11 | + """ |
| 12 | + Configurations metadata Endpoint |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, root_url, token, branch_id): |
| 16 | + """ |
| 17 | + Create a Component metadata endpoint. |
| 18 | +
|
| 19 | + Args: |
| 20 | + root_url (:obj:`str`): The base url for the API. |
| 21 | + token (:obj:`str`): A storage API key. |
| 22 | + branch_id (str): The ID of branch to use, use 'default' to work without branch (in main). |
| 23 | + """ |
| 24 | + super().__init__(root_url, f"branch/{branch_id}/components", token) |
| 25 | + |
| 26 | + def delete(self, component_id, configuration_id, metadata_id): |
| 27 | + """ |
| 28 | + Deletes the configuration metadata identified by ``metadata_id``. |
| 29 | +
|
| 30 | + Args: |
| 31 | + component_id (str): The id of the component. |
| 32 | + configuration_id (str): The id of the configuration. |
| 33 | + metadata_id (str): The id of the metadata (not key!). |
| 34 | +
|
| 35 | + Raises: |
| 36 | + requests.HTTPError: If the API request fails. |
| 37 | + ValueError: If the component_id/configuration_id/metadata_id is not a string or is empty. |
| 38 | + """ |
| 39 | + if not isinstance(component_id, str) or component_id == '': |
| 40 | + raise ValueError("Invalid component_id '{}'.".format(component_id)) |
| 41 | + if not isinstance(configuration_id, str) or configuration_id == '': |
| 42 | + raise ValueError("Invalid configuration_id '{}'.".format(configuration_id)) |
| 43 | + if not isinstance(metadata_id, str) or metadata_id == '': |
| 44 | + raise ValueError("Invalid metadata_id '{}'.".format(metadata_id)) |
| 45 | + url = '{}/{}/configs/{}/metadata/{}'.format(self.base_url, component_id, configuration_id, metadata_id) |
| 46 | + self._delete(url) |
| 47 | + |
| 48 | + def list(self, component_id, configuration_id): |
| 49 | + """ |
| 50 | + Lists metadata for a given component configuration. |
| 51 | +
|
| 52 | + Args: |
| 53 | + component_id (str): The id of the component. |
| 54 | + configuration_id (str): The id of the configuration. |
| 55 | +
|
| 56 | + Raises: |
| 57 | + requests.HTTPError: If the API request fails. |
| 58 | + ValueError: If the component_id/configuration_id is not a string or is empty. |
| 59 | + """ |
| 60 | + if not isinstance(component_id, str) or component_id == '': |
| 61 | + raise ValueError("Invalid component_id '{}'.".format(component_id)) |
| 62 | + if not isinstance(configuration_id, str) or configuration_id == '': |
| 63 | + raise ValueError("Invalid configuration_id '{}'.".format(configuration_id)) |
| 64 | + url = '{}/{}/configs/{}/metadata'.format(self.base_url, component_id, configuration_id) |
| 65 | + return self._get(url) |
| 66 | + |
| 67 | + def create(self, component_id, configuration_id, provider, metadata): |
| 68 | + """ |
| 69 | + Writes metadata for a given component configuration. |
| 70 | +
|
| 71 | + Args: |
| 72 | + component_id (str): The id of the component. |
| 73 | + configuration (str): The id of the configuration. |
| 74 | + provider (str): The provider of the configuration (currently ignored and "user" is sent). |
| 75 | + metadata (list): A list of metadata items. Item is a dictionary with 'key' and 'value' keys. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + response_body: The parsed json from the HTTP response. |
| 79 | +
|
| 80 | + Raises: |
| 81 | + requests.HTTPError: If the API request fails. |
| 82 | + ValueError: If the component_id/configuration_id is not a string or is empty. |
| 83 | + ValueError: If the metadata is not a list. |
| 84 | + ValueError: If the metadata item is not a dictionary. |
| 85 | + """ |
| 86 | + if not isinstance(component_id, str) or component_id == '': |
| 87 | + raise ValueError("Invalid component_id '{}'.".format(component_id)) |
| 88 | + if not isinstance(configuration_id, str) or configuration_id == '': |
| 89 | + raise ValueError("Invalid component_id '{}'.".format(configuration_id)) |
| 90 | + url = '{}/{}/configs/{}/metadata'.format(self.base_url, component_id, configuration_id) |
| 91 | + if not isinstance(metadata, list): |
| 92 | + raise ValueError("Metadata must be a list '{}'.".format(metadata)) |
| 93 | + for metadataItem in metadata: |
| 94 | + if not isinstance(metadataItem, dict): |
| 95 | + raise ValueError("Metadata item must be a dictionary '{}'.".format(metadataItem)) |
| 96 | + |
| 97 | + headers = { |
| 98 | + 'Content-Type': 'application/json', |
| 99 | + } |
| 100 | + data = { |
| 101 | + # 'provider': provider, # not yet implemented |
| 102 | + 'metadata': metadata |
| 103 | + } |
| 104 | + return self._post(url, data=json.dumps(data), headers=headers) |
0 commit comments