-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from keboola/client-class
feat: entry client class
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
"""" | ||
Entry point for the Storage API client. | ||
""" | ||
|
||
from kbcstorage.buckets import Buckets | ||
from kbcstorage.workspaces import Workspaces | ||
from kbcstorage.jobs import Jobs | ||
from kbcstorage.tables import Tables | ||
from kbcstorage.files import Files | ||
|
||
|
||
class Client: | ||
""" | ||
Storage API Client. | ||
""" | ||
|
||
def __init__(self, api_domain, token): | ||
""" | ||
Initialise a client. | ||
Args: | ||
api_domain (str): The domain on which the API sits. eg. | ||
"https://connection.keboola.com". | ||
token (str): A storage API key. | ||
""" | ||
self.root_url = api_domain | ||
self.token = token | ||
|
||
self.buckets = Buckets(self.root_url, self.token) | ||
self.workspaces = Workspaces(self.root_url, self.token) | ||
self.jobs = Jobs(self.root_url, self.token) | ||
self.tables = Tables(self.root_url, self.token) | ||
self.files = Files(self.root_url, self.token) |
This file contains 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,50 @@ | ||
import csv | ||
import os | ||
import tempfile | ||
import unittest | ||
import warnings | ||
|
||
from requests import exceptions | ||
from kbcstorage.client import Client | ||
|
||
|
||
class TestFunctionalBuckets(unittest.TestCase): | ||
def setUp(self): | ||
self.client = Client(os.getenv('KBC_TEST_API_URL'), | ||
os.getenv('KBC_TEST_TOKEN')) | ||
try: | ||
self.client.buckets.delete('in.c-py-test', force=True) | ||
except exceptions.HTTPError as e: | ||
if e.response.status_code != 404: | ||
raise | ||
# https://github.com/boto/boto3/issues/454 | ||
warnings.simplefilter("ignore", ResourceWarning) | ||
|
||
def tearDown(self): | ||
try: | ||
self.client.buckets.delete('in.c-py-test', force=True) | ||
except exceptions.HTTPError as e: | ||
if e.response.status_code != 404: | ||
raise | ||
|
||
def test_client(self): | ||
bucket_id = self.client.buckets.create(name='py-test', | ||
stage='in', | ||
description='Test bucket')['id'] | ||
file, path = tempfile.mkstemp(prefix='sapi-test') | ||
with open(path, 'w') as csv_file: | ||
writer = csv.DictWriter(csv_file, fieldnames=['col1', 'col2'], | ||
lineterminator='\n', delimiter=',', | ||
quotechar='"') | ||
writer.writeheader() | ||
writer.writerow({'col1': 'ping', 'col2': 'pong'}) | ||
os.close(file) | ||
self.assertEqual(bucket_id, | ||
self.client.buckets.detail(bucket_id)['id']) | ||
table_id = self.client.tables.create(name='some-table', file_path=path, | ||
bucket_id='in.c-py-test') | ||
table_info = self.client.tables.detail(table_id) | ||
self.assertEqual(table_id, table_info['id']) | ||
self.assertEqual('in.c-py-test', table_info['bucket']['id']) | ||
self.assertTrue(len(self.client.jobs.list()) > 2) | ||
self.assertEqual(1, len(self.client.files.list(limit=1))) |