-
Notifications
You must be signed in to change notification settings - Fork 7
Basic configuration support #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| """ | ||
| Manages calls to the Storage API relating to components | ||
|
|
||
| Full documentation https://keboola.docs.apiary.io/#reference/components-and-configurations | ||
| """ | ||
| from kbcstorage.base import Endpoint | ||
|
|
||
|
|
||
| class Components(Endpoint): | ||
| """ | ||
| Components Endpoint | ||
| """ | ||
| def __init__(self, root_url, token, branch_id): | ||
| """ | ||
| Create a Configuration endpoint. | ||
|
|
||
| Args: | ||
| root_url (:obj:`str`): The base url for the API. | ||
| token (:obj:`str`): A storage API key. | ||
| branch_id (str): The ID of branch to use, use 'default' to work without branch (in main). | ||
| """ | ||
| super().__init__(root_url, f"branch/{branch_id}/components", token) | ||
|
|
||
| def list(self, include=None): | ||
| """ | ||
| List all components (and optionally configurations) in a project. | ||
|
|
||
| Args: | ||
| include (list): Properties to list (configuration, rows, state) | ||
| Returns: | ||
| response_body: The parsed json from the HTTP response. | ||
|
|
||
| Raises: | ||
| requests.HTTPError: If the API request fails. | ||
| """ | ||
| params = {'include': ',' . join(include)} if include else {} | ||
| return self._get(self.base_url, params=params) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| """ | ||
| Manages calls to the Storage API relating to configurations | ||
|
|
||
| Full documentation https://keboola.docs.apiary.io/#reference/components-and-configurations | ||
| """ | ||
| from kbcstorage.base import Endpoint | ||
|
|
||
|
|
||
| class Configurations(Endpoint): | ||
| """ | ||
| Configurations Endpoint | ||
| """ | ||
|
|
||
| def __init__(self, root_url, token, branch_id): | ||
| """ | ||
| Create a Component endpoint. | ||
|
|
||
| Args: | ||
| root_url (:obj:`str`): The base url for the API. | ||
| token (:obj:`str`): A storage API key. | ||
| branch_id (str): The ID of branch to use, use 'default' to work without branch (in main). | ||
| """ | ||
| super().__init__(root_url, f"branch/{branch_id}/components", token) | ||
|
|
||
| def detail(self, component_id, configuration_id): | ||
| """ | ||
| Retrieves information about a given configuration. | ||
|
|
||
| Args: | ||
| component_id (str): The id of the component. | ||
| configuration_id (str): The id of the configuration. | ||
|
|
||
| Returns: | ||
| response_body: The parsed json from the HTTP response. | ||
|
|
||
| Raises: | ||
| requests.HTTPError: If the API request fails. | ||
| """ | ||
| if not isinstance(component_id, str) or component_id == '': | ||
| raise ValueError("Invalid component_id '{}'.".format(component_id)) | ||
| if not isinstance(configuration_id, str) or configuration_id == '': | ||
| raise ValueError("Invalid component_id '{}'.".format(configuration_id)) | ||
| url = '{}/{}/configs/{}'.format(self.base_url, component_id, configuration_id) | ||
| return self._get(url) | ||
|
|
||
| def delete(self, component_id, configuration_id): | ||
| """ | ||
| Deletes the configuration. | ||
|
|
||
| Args: | ||
| component_id (str): The id of the component. | ||
| configuration_id (str): The id of the configuration. | ||
|
|
||
| Raises: | ||
| requests.HTTPError: If the API request fails. | ||
| """ | ||
| if not isinstance(component_id, str) or component_id == '': | ||
| raise ValueError("Invalid component_id '{}'.".format(component_id)) | ||
| if not isinstance(configuration_id, str) or configuration_id == '': | ||
| raise ValueError("Invalid component_id '{}'.".format(configuration_id)) | ||
|
Comment on lines
+57
to
+60
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| url = '{}/{}/configs/{}'.format(self.base_url, component_id, configuration_id) | ||
| self._delete(url) | ||
|
|
||
| def list(self, component_id): | ||
| """ | ||
| Lists configurations of the given component. | ||
|
|
||
| Args: | ||
| component_id (str): The id of the component. | ||
|
|
||
| Raises: | ||
| requests.HTTPError: If the API request fails. | ||
| """ | ||
| if not isinstance(component_id, str) or component_id == '': | ||
| raise ValueError("Invalid component_id '{}'.".format(component_id)) | ||
pivnicek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| url = '{}/{}/configs'.format(self.base_url, component_id) | ||
| return self._get(url) | ||
|
|
||
| def create(self, component_id, name, description='', configuration=None, state=None, change_description='', | ||
| is_disabled=False, configuration_id=None): | ||
| """ | ||
| Create a new configuration. | ||
|
|
||
| Args: | ||
| component_id (str): ID of the component to create configuration for. | ||
| name (str): Name of the configuration visible to end-user. | ||
| description (str): Optional configuration description | ||
| configuration (dict): Actual configuration parameters | ||
| state (dict): Optional state parameters | ||
| changeDescription (str): Optional change description | ||
| is_disabled (bool): Optional flag to disable the configuration, default False | ||
| configuration_id (str): Optional configuration ID, if not specified, new ID is generated | ||
| Returns: | ||
| response_body: The parsed json from the HTTP response. | ||
|
|
||
| Raises: | ||
| requests.HTTPError: If the API request fails. | ||
| """ | ||
odinuv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if not isinstance(component_id, str) or component_id == '': | ||
| raise ValueError("Invalid component_id '{}'.".format(component_id)) | ||
| if state is None: | ||
| state = {} | ||
| if configuration is None: | ||
| configuration = {} | ||
| body = { | ||
| 'name': name, | ||
| 'description': description, | ||
| 'configuration': configuration, | ||
| 'state': state, | ||
| 'changeDescription': change_description, | ||
| 'isDisabled': is_disabled | ||
| } | ||
| if configuration_id: | ||
| body['id'] = configuration_id | ||
| url = '{}/{}/configs'.format(self.base_url, component_id) | ||
| return self._post(url, data=body) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import os | ||
| from requests import exceptions | ||
| from kbcstorage.components import Components | ||
| from kbcstorage.configurations import Configurations | ||
| from tests.base_test_case import BaseTestCase | ||
|
|
||
|
|
||
| class TestEndpoint(BaseTestCase): | ||
| def setUp(self): | ||
| self.components = Components(os.getenv('KBC_TEST_API_URL'), os.getenv('KBC_TEST_TOKEN'), 'default') | ||
| self.configurations = Configurations(os.getenv('KBC_TEST_API_URL'), os.getenv('KBC_TEST_TOKEN'), 'default') | ||
| self.configurations.create(self.TEST_COMPONENT_NAME, 'test_components') | ||
|
|
||
| def tearDown(self): | ||
| try: | ||
| for configuration in self.configurations.list(self.TEST_COMPONENT_NAME): | ||
| self.configurations.delete(self.TEST_COMPONENT_NAME, configuration['id']) | ||
| except exceptions.HTTPError as e: | ||
| if e.response.status_code != 404: | ||
| raise | ||
|
|
||
| def testListComponents(self): | ||
| components = self.components.list() | ||
| self.assertTrue(len(components) > 0) | ||
| for component in components: | ||
| with self.subTest(): | ||
| self.assertTrue('id' in component) | ||
| self.assertTrue('name' in component) | ||
| self.assertTrue('type' in component) | ||
| self.assertTrue('uri' in component) | ||
|
|
||
| with self.subTest(): | ||
| for configuration in component['configurations']: | ||
| self.assertTrue('id' in configuration) | ||
| self.assertTrue('name' in configuration) | ||
| self.assertTrue('description' in configuration) | ||
| self.assertFalse('configuration' in configuration) | ||
| self.assertFalse('rows' in configuration) | ||
| self.assertFalse('state' in configuration) | ||
|
|
||
| def testListComponentsIncludeConfigurations(self): | ||
| components = self.components.list(include=['configuration', 'rows', 'state']) | ||
| self.assertTrue(len(components) > 0) | ||
| for component in components: | ||
| with self.subTest(): | ||
| self.assertTrue('id' in component) | ||
| self.assertTrue('name' in component) | ||
| self.assertTrue('type' in component) | ||
| self.assertTrue('uri' in component) | ||
|
|
||
| with self.subTest(): | ||
| self.assertTrue('configurations' in component) | ||
| for configuration in component['configurations']: | ||
| self.assertTrue('id' in configuration) | ||
| self.assertTrue('name' in configuration) | ||
| self.assertTrue('description' in configuration) | ||
| self.assertTrue('configuration' in configuration) | ||
| self.assertTrue('rows' in configuration) | ||
| self.assertTrue('state' in configuration) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import os | ||
| from requests import exceptions | ||
| from kbcstorage.configurations import Configurations | ||
| from tests.base_test_case import BaseTestCase | ||
|
|
||
|
|
||
| class TestEndpoint(BaseTestCase): | ||
| def setUp(self): | ||
| self.configurations = Configurations(os.getenv('KBC_TEST_API_URL'), os.getenv('KBC_TEST_TOKEN'), 'default') | ||
|
|
||
| def tearDown(self): | ||
| try: | ||
| for configuration in self.configurations.list(self.TEST_COMPONENT_NAME): | ||
| self.configurations.delete(self.TEST_COMPONENT_NAME, configuration['id']) | ||
| except exceptions.HTTPError as e: | ||
| if e.response.status_code != 404: | ||
| raise | ||
|
|
||
| def testCreateConfiguration(self): | ||
| configuration = self.configurations.create(self.TEST_COMPONENT_NAME, 'test_create_configuration') | ||
| self.assertTrue('id' in configuration) | ||
| self.assertTrue('name' in configuration) | ||
| self.assertTrue('description' in configuration) | ||
| self.assertTrue('configuration' in configuration) | ||
|
|
||
| def testDeleteConfiguration(self): | ||
| configuration = self.configurations.create(self.TEST_COMPONENT_NAME, 'test_delete_configuration') | ||
| configuration = self.configurations.detail(self.TEST_COMPONENT_NAME, configuration['id']) | ||
| self.assertTrue('id' in configuration) | ||
| self.assertTrue('name' in configuration) | ||
| self.assertEqual(configuration['name'], 'test_delete_configuration') | ||
| self.assertTrue('description' in configuration) | ||
| self.assertTrue('configuration' in configuration) | ||
|
|
||
| self.configurations.delete(self.TEST_COMPONENT_NAME, configuration['id']) | ||
| with self.assertRaises(exceptions.HTTPError): | ||
| self.configurations.detail(self.TEST_COMPONENT_NAME, configuration['id']) | ||
|
|
||
| def testListConfigurations(self): | ||
| self.configurations.create(self.TEST_COMPONENT_NAME, 'test_list_configurations') | ||
| configurations = self.configurations.list(self.TEST_COMPONENT_NAME) | ||
| self.assertTrue(len(configurations) > 0) | ||
| for configuration in configurations: | ||
| with self.subTest(): | ||
| self.assertTrue('id' in configuration) | ||
| self.assertTrue('name' in configuration) | ||
| self.assertTrue('description' in configuration) | ||
| self.assertTrue('configuration' in configuration) | ||
|
|
||
| with self.subTest(): | ||
| with self.assertRaises(exceptions.HTTPError): | ||
| configurations = self.configurations.list('non-existent-component') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to accept empty strings? Doesn't the detail call require values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If component_id is an empty string, it raises an error, doesn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
omg, sorry, I don't know how to read a simple conditional statement 🤦