-
Notifications
You must be signed in to change notification settings - Fork 7
add verify token & branch metadata methods #75
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
37ade2c
add verify token method
odinuv b04b44c
fix cs
odinuv a680fbe
add branch metadata endpoint
odinuv 59aa351
fix cs
odinuv f26b87e
not relevant any more
odinuv 31e780b
make default branch default
odinuv c466048
fix cs
odinuv 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,43 @@ | ||
| """ | ||
| Manages calls to the Storage API relating to development branches | ||
|
|
||
| Full documentation https://keboola.docs.apiary.io/#reference/development-branches | ||
|
|
||
| """ | ||
| from kbcstorage.base import Endpoint | ||
|
|
||
|
|
||
| class Branches(Endpoint): | ||
| """ | ||
| Tokens Endpoint | ||
| """ | ||
| def __init__(self, root_url, token): | ||
| """ | ||
| Create a Tokens endpoint. | ||
|
|
||
| Args: | ||
| root_url (:obj:`str`): The base url for the API. | ||
| token (:obj:`str`): A storage API key. | ||
| """ | ||
| # Branches have inconsistent endpoint naming - it's either dev-branches or branch, so it need to be resolved | ||
| # endpoint by endpoint. | ||
| super().__init__(root_url, "", token) | ||
|
|
||
| def metadata(self, branch_id="default"): | ||
| """ | ||
| Get branch metadata | ||
|
|
||
| Args: | ||
| branch_id (str): The id of the branch or "default" to get metadata for the main branch (production). | ||
|
|
||
| Returns: | ||
| response_body: The parsed json from the HTTP response. | ||
|
|
||
| Raises: | ||
| requests.HTTPError: If the API request fails. | ||
| """ | ||
| if not isinstance(branch_id, str) or branch_id == "": | ||
| raise ValueError(f"Invalid branch_id '{branch_id}'") | ||
|
|
||
| url = f"{self.base_url}branch/{branch_id}/metadata" | ||
| return self._get(url) | ||
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,35 @@ | ||
| """ | ||
| Manages calls to the Storage API relating to tokens | ||
|
|
||
| Full documentation https://keboola.docs.apiary.io/#reference/tokens-and-permissions/. | ||
|
|
||
| """ | ||
| from kbcstorage.base import Endpoint | ||
|
|
||
|
|
||
| class Tokens(Endpoint): | ||
| """ | ||
| Tokens Endpoint | ||
| """ | ||
| def __init__(self, root_url, token): | ||
| """ | ||
| Create a Tokens endpoint. | ||
|
|
||
| Args: | ||
| root_url (:obj:`str`): The base url for the API. | ||
| token (:obj:`str`): A storage API key. | ||
| """ | ||
| super().__init__(root_url, 'tokens', token) | ||
|
|
||
| def verify(self): | ||
| """ | ||
| Verify token. | ||
|
|
||
| Returns: | ||
| response_body: The parsed json from the HTTP response. | ||
|
|
||
| Raises: | ||
| requests.HTTPError: If the API request fails. | ||
| """ | ||
| url = '{}/verify'.format(self.base_url) | ||
| return self._get(url) |
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,13 @@ | ||
| import os | ||
|
|
||
| from kbcstorage.branches import Branches | ||
| from tests.base_test_case import BaseTestCase | ||
|
|
||
|
|
||
| class TestEndpoint(BaseTestCase): | ||
| def setUp(self): | ||
| self.branches = Branches(os.getenv('KBC_TEST_API_URL'), os.getenv('KBC_TEST_TOKEN')) | ||
|
|
||
| def test_metadata(self): | ||
| metadata = self.branches.metadata('default') | ||
| self.assertTrue(isinstance(metadata, list)) |
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,16 @@ | ||
| import os | ||
| from kbcstorage.tokens import Tokens | ||
| from tests.base_test_case import BaseTestCase | ||
|
|
||
|
|
||
| class TestEndpoint(BaseTestCase): | ||
| def setUp(self): | ||
| self.tokens = Tokens(os.getenv('KBC_TEST_API_URL'), | ||
| os.getenv('KBC_TEST_TOKEN')) | ||
|
|
||
| def test_verify(self): | ||
| token_info = self.tokens.verify() | ||
| self.assertTrue('id' in token_info) | ||
| self.assertTrue('description' in token_info) | ||
| self.assertTrue('canManageBuckets' in token_info) | ||
| self.assertTrue('owner' in token_info) |
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,8 @@ | ||
| branches_metadata_response = [ | ||
| { | ||
| "id": "93670", | ||
| "key": "KBC.projectDescription", | ||
| "value": "Testing project one", | ||
| "timestamp": "2023-05-31T17:52:18+0200" | ||
| } | ||
| ] |
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,49 @@ | ||
| """ | ||
| Test basic functionality of the Branches endpoint | ||
| """ | ||
| import unittest | ||
|
|
||
| import responses | ||
|
|
||
| from kbcstorage.branches import Branches | ||
| from tests.mocks.branches_responses import branches_metadata_response | ||
|
|
||
|
|
||
| class TestBranchesEndpointWithMocks(unittest.TestCase): | ||
| """ | ||
| Test the methods of a Branches endpoint instance with mock HTTP responses | ||
| """ | ||
| def setUp(self): | ||
| token = 'dummy_token' | ||
| base_url = 'https://connection.keboola.com/' | ||
| self.branches = Branches(base_url, token) | ||
|
|
||
| @responses.activate | ||
| def test_metadata_no_branch(self): | ||
| """ | ||
| Branches lists metadata correctly | ||
| """ | ||
| responses.add( | ||
| responses.Response( | ||
| method='GET', | ||
| url='https://connection.keboola.com/v2/storage/branch/default/metadata', | ||
| json=branches_metadata_response | ||
| ) | ||
| ) | ||
| branch_metadata = self.branches.metadata() | ||
| self.assertEqual(branches_metadata_response, branch_metadata) | ||
|
|
||
| @responses.activate | ||
| def test_metadata_some_branch(self): | ||
| """ | ||
| Branches lists metadata correctly | ||
| """ | ||
| responses.add( | ||
| responses.Response( | ||
| method='GET', | ||
| url='https://connection.keboola.com/v2/storage/branch/1234/metadata', | ||
| json=branches_metadata_response | ||
| ) | ||
| ) | ||
| branch_metadata = self.branches.metadata('1234') | ||
| self.assertEqual(branches_metadata_response, branch_metadata) |
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,34 @@ | ||
| """ | ||
| Test basic functionality of the Tokens endpoint | ||
| """ | ||
| import unittest | ||
|
|
||
| import responses | ||
|
|
||
| from kbcstorage.tokens import Tokens | ||
| from tests.mocks.token_responses import verify_token_response | ||
|
|
||
|
|
||
| class TestTokensEndpointWithMocks(unittest.TestCase): | ||
| """ | ||
| Test the methods of a Tokens endpoint instance with mock HTTP responses | ||
| """ | ||
| def setUp(self): | ||
| token = 'dummy_token' | ||
| base_url = 'https://connection.keboola.com/' | ||
| self.tokens = Tokens(base_url, token) | ||
|
|
||
| @responses.activate | ||
| def test_verify(self): | ||
| """ | ||
| Verify token returns correctly | ||
| """ | ||
| responses.add( | ||
| responses.Response( | ||
| method='GET', | ||
| url='https://connection.keboola.com/v2/storage/tokens/verify', | ||
| json=verify_token_response | ||
| ) | ||
| ) | ||
| token_info = self.tokens.verify() | ||
| self.assertEqual(verify_token_response, token_info) |
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,141 @@ | ||
| verify_token_response = { | ||
| "id": "373115", | ||
| "created": "2021-06-23T09:33:36+0200", | ||
| "refreshed": "2021-06-23T09:33:36+0200", | ||
| "description": "[email protected]", | ||
| "uri": "https://connection.keboola.com/v2/storage/tokens/373115", | ||
| "isMasterToken": True, | ||
| "canManageBuckets": True, | ||
| "canManageTokens": True, | ||
| "canReadAllFileUploads": True, | ||
| "canPurgeTrash": True, | ||
| "expires": None, | ||
| "isExpired": False, | ||
| "isDisabled": False, | ||
| "dailyCapacity": 0, | ||
| "token": "8625-373115-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", | ||
| "bucketPermissions": { | ||
| "out.c-test2": "manage", | ||
| "in.c-test3": "manage", | ||
| "out.c-test": "manage", | ||
| "out.c-some-other": "manage" | ||
| }, | ||
| "owner": { | ||
| "id": 8625, | ||
| "name": "Test Project", | ||
| "type": "production", | ||
| "region": "us-east-1", | ||
| "created": "2021-06-23T09:33:32+0200", | ||
| "expires": None, | ||
| "features": [ | ||
| "syrup-jobs-limit-10", | ||
| "oauth-v3", | ||
| "queuev2", | ||
| "storage-types", | ||
| "allow-ai", | ||
| "alternat", | ||
| ], | ||
| "dataSizeBytes": 31474176, | ||
| "rowsCount": 797, | ||
| "hasMysql": False, | ||
| "hasSynapse": False, | ||
| "hasRedshift": True, | ||
| "hasSnowflake": True, | ||
| "hasExasol": False, | ||
| "hasTeradata": False, | ||
| "hasBigquery": False, | ||
| "defaultBackend": "snowflake", | ||
| "hasTryModeOn": "0", | ||
| "limits": { | ||
| "components.jobsParallelism": { | ||
| "name": "components.jobsParallelism", | ||
| "value": 10 | ||
| }, | ||
| "goodData.dataSizeBytes": { | ||
| "name": "goodData.dataSizeBytes", | ||
| "value": 1000000000 | ||
| }, | ||
| "goodData.demoTokenEnabled": { | ||
| "name": "goodData.demoTokenEnabled", | ||
| "value": 1 | ||
| }, | ||
| "goodData.prodTokenEnabled": { | ||
| "name": "goodData.prodTokenEnabled", | ||
| "value": 0 | ||
| }, | ||
| "goodData.usersCount": { | ||
| "name": "goodData.usersCount", | ||
| "value": 30 | ||
| }, | ||
| "kbc.adminsCount": { | ||
| "name": "kbc.adminsCount", | ||
| "value": 10 | ||
| }, | ||
| "kbc.extractorsCount": { | ||
| "name": "kbc.extractorsCount", | ||
| "value": 0 | ||
| }, | ||
| "kbc.monthlyProjectPowerLimit": { | ||
| "name": "kbc.monthlyProjectPowerLimit", | ||
| "value": 50 | ||
| }, | ||
| "kbc.writersCount": { | ||
| "name": "kbc.writersCount", | ||
| "value": 0 | ||
| }, | ||
| "orchestrations.count": { | ||
| "name": "orchestrations.count", | ||
| "value": 10 | ||
| }, | ||
| "storage.dataSizeBytes": { | ||
| "name": "storage.dataSizeBytes", | ||
| "value": 50000000000 | ||
| }, | ||
| "storage.jobsParallelism": { | ||
| "name": "storage.jobsParallelism", | ||
| "value": 10 | ||
| } | ||
| }, | ||
| "metrics": { | ||
| "kbc.adminsCount": { | ||
| "name": "kbc.adminsCount", | ||
| "value": 1 | ||
| }, | ||
| "orchestrations.count": { | ||
| "name": "orchestrations.count", | ||
| "value": 0 | ||
| }, | ||
| "storage.dataSizeBytes": { | ||
| "name": "storage.dataSizeBytes", | ||
| "value": 31474176 | ||
| }, | ||
| "storage.rowsCount": { | ||
| "name": "storage.rowsCount", | ||
| "value": 797 | ||
| } | ||
| }, | ||
| "isDisabled": False, | ||
| "billedMonthlyPrice": None, | ||
| "dataRetentionTimeInDays": 7, | ||
| "fileStorageProvider": "aws", | ||
| "redshift": { | ||
| "connectionId": 365, | ||
| "databaseName": "sapi_8625" | ||
| } | ||
| }, | ||
| "organization": { | ||
| "id": "111111" | ||
| }, | ||
| "admin": { | ||
| "name": "Devel", | ||
| "id": 59, | ||
| "features": [ | ||
| "ui-devel-preview", | ||
| "manage-try-mode", | ||
| "validate-sql", | ||
| "early-adopter-preview" | ||
| ], | ||
| "isOrganizationMember": True, | ||
| "role": "admin" | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.