|
| 1 | +""" |
| 2 | +Manages calls to the Storage API relating to configurations |
| 3 | +
|
| 4 | +Full documentation https://keboola.docs.apiary.io/#reference/components-and-configurations |
| 5 | +""" |
| 6 | +from kbcstorage.base import Endpoint |
| 7 | + |
| 8 | + |
| 9 | +class Configurations(Endpoint): |
| 10 | + """ |
| 11 | + Configurations Endpoint |
| 12 | + """ |
| 13 | + |
| 14 | + def __init__(self, root_url, token, branch_id): |
| 15 | + """ |
| 16 | + Create a Component endpoint. |
| 17 | +
|
| 18 | + Args: |
| 19 | + root_url (:obj:`str`): The base url for the API. |
| 20 | + token (:obj:`str`): A storage API key. |
| 21 | + branch_id (str): The ID of branch to use, use 'default' to work without branch (in main). |
| 22 | + """ |
| 23 | + super().__init__(root_url, f"branch/{branch_id}/components", token) |
| 24 | + |
| 25 | + def detail(self, component_id, configuration_id): |
| 26 | + """ |
| 27 | + Retrieves information about a given configuration. |
| 28 | +
|
| 29 | + Args: |
| 30 | + component_id (str): The id of the component. |
| 31 | + configuration_id (str): The id of the configuration. |
| 32 | +
|
| 33 | + Returns: |
| 34 | + response_body: The parsed json from the HTTP response. |
| 35 | +
|
| 36 | + Raises: |
| 37 | + requests.HTTPError: If the API request fails. |
| 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 component_id '{}'.".format(configuration_id)) |
| 43 | + url = '{}/{}/configs/{}'.format(self.base_url, component_id, configuration_id) |
| 44 | + return self._get(url) |
| 45 | + |
| 46 | + def delete(self, component_id, configuration_id): |
| 47 | + """ |
| 48 | + Deletes the configuration. |
| 49 | +
|
| 50 | + Args: |
| 51 | + component_id (str): The id of the component. |
| 52 | + configuration_id (str): The id of the configuration. |
| 53 | +
|
| 54 | + Raises: |
| 55 | + requests.HTTPError: If the API request fails. |
| 56 | + """ |
| 57 | + if not isinstance(component_id, str) or component_id == '': |
| 58 | + raise ValueError("Invalid component_id '{}'.".format(component_id)) |
| 59 | + if not isinstance(configuration_id, str) or configuration_id == '': |
| 60 | + raise ValueError("Invalid component_id '{}'.".format(configuration_id)) |
| 61 | + url = '{}/{}/configs/{}'.format(self.base_url, component_id, configuration_id) |
| 62 | + self._delete(url) |
| 63 | + |
| 64 | + def list(self, component_id): |
| 65 | + """ |
| 66 | + Lists configurations of the given component. |
| 67 | +
|
| 68 | + Args: |
| 69 | + component_id (str): The id of the component. |
| 70 | +
|
| 71 | + Raises: |
| 72 | + requests.HTTPError: If the API request fails. |
| 73 | + """ |
| 74 | + if not isinstance(component_id, str) or component_id == '': |
| 75 | + raise ValueError("Invalid component_id '{}'.".format(component_id)) |
| 76 | + url = '{}/{}/configs'.format(self.base_url, component_id) |
| 77 | + return self._get(url) |
| 78 | + |
| 79 | + def create(self, component_id, name, description='', configuration=None, state=None, change_description='', |
| 80 | + is_disabled=False, configuration_id=None): |
| 81 | + """ |
| 82 | + Create a new configuration. |
| 83 | +
|
| 84 | + Args: |
| 85 | + component_id (str): ID of the component to create configuration for. |
| 86 | + name (str): Name of the configuration visible to end-user. |
| 87 | + description (str): Optional configuration description |
| 88 | + configuration (dict): Actual configuration parameters |
| 89 | + state (dict): Optional state parameters |
| 90 | + changeDescription (str): Optional change description |
| 91 | + is_disabled (bool): Optional flag to disable the configuration, default False |
| 92 | + configuration_id (str): Optional configuration ID, if not specified, new ID is generated |
| 93 | + Returns: |
| 94 | + response_body: The parsed json from the HTTP response. |
| 95 | +
|
| 96 | + Raises: |
| 97 | + requests.HTTPError: If the API request fails. |
| 98 | + """ |
| 99 | + if not isinstance(component_id, str) or component_id == '': |
| 100 | + raise ValueError("Invalid component_id '{}'.".format(component_id)) |
| 101 | + if state is None: |
| 102 | + state = {} |
| 103 | + if configuration is None: |
| 104 | + configuration = {} |
| 105 | + body = { |
| 106 | + 'name': name, |
| 107 | + 'description': description, |
| 108 | + 'configuration': configuration, |
| 109 | + 'state': state, |
| 110 | + 'changeDescription': change_description, |
| 111 | + 'isDisabled': is_disabled |
| 112 | + } |
| 113 | + if configuration_id: |
| 114 | + body['id'] = configuration_id |
| 115 | + url = '{}/{}/configs'.format(self.base_url, component_id) |
| 116 | + return self._post(url, data=body) |
0 commit comments