Skip to content

Commit d9c241a

Browse files
authored
Merge pull request #31 from keboola/client-class
feat: entry client class
2 parents 038f67a + 801c2c7 commit d9c241a

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

kbcstorage/client.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
""""
2+
Entry point for the Storage API client.
3+
"""
4+
5+
from kbcstorage.buckets import Buckets
6+
from kbcstorage.workspaces import Workspaces
7+
from kbcstorage.jobs import Jobs
8+
from kbcstorage.tables import Tables
9+
from kbcstorage.files import Files
10+
11+
12+
class Client:
13+
"""
14+
Storage API Client.
15+
"""
16+
17+
def __init__(self, api_domain, token):
18+
"""
19+
Initialise a client.
20+
21+
22+
Args:
23+
api_domain (str): The domain on which the API sits. eg.
24+
"https://connection.keboola.com".
25+
token (str): A storage API key.
26+
"""
27+
self.root_url = api_domain
28+
self.token = token
29+
30+
self.buckets = Buckets(self.root_url, self.token)
31+
self.workspaces = Workspaces(self.root_url, self.token)
32+
self.jobs = Jobs(self.root_url, self.token)
33+
self.tables = Tables(self.root_url, self.token)
34+
self.files = Files(self.root_url, self.token)

tests/test_functional_client.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import csv
2+
import os
3+
import tempfile
4+
import unittest
5+
import warnings
6+
7+
from requests import exceptions
8+
from kbcstorage.client import Client
9+
10+
11+
class TestFunctionalBuckets(unittest.TestCase):
12+
def setUp(self):
13+
self.client = Client(os.getenv('KBC_TEST_API_URL'),
14+
os.getenv('KBC_TEST_TOKEN'))
15+
try:
16+
self.client.buckets.delete('in.c-py-test', force=True)
17+
except exceptions.HTTPError as e:
18+
if e.response.status_code != 404:
19+
raise
20+
# https://github.com/boto/boto3/issues/454
21+
warnings.simplefilter("ignore", ResourceWarning)
22+
23+
def tearDown(self):
24+
try:
25+
self.client.buckets.delete('in.c-py-test', force=True)
26+
except exceptions.HTTPError as e:
27+
if e.response.status_code != 404:
28+
raise
29+
30+
def test_client(self):
31+
bucket_id = self.client.buckets.create(name='py-test',
32+
stage='in',
33+
description='Test bucket')['id']
34+
file, path = tempfile.mkstemp(prefix='sapi-test')
35+
with open(path, 'w') as csv_file:
36+
writer = csv.DictWriter(csv_file, fieldnames=['col1', 'col2'],
37+
lineterminator='\n', delimiter=',',
38+
quotechar='"')
39+
writer.writeheader()
40+
writer.writerow({'col1': 'ping', 'col2': 'pong'})
41+
os.close(file)
42+
self.assertEqual(bucket_id,
43+
self.client.buckets.detail(bucket_id)['id'])
44+
table_id = self.client.tables.create(name='some-table', file_path=path,
45+
bucket_id='in.c-py-test')
46+
table_info = self.client.tables.detail(table_id)
47+
self.assertEqual(table_id, table_info['id'])
48+
self.assertEqual('in.c-py-test', table_info['bucket']['id'])
49+
self.assertTrue(len(self.client.jobs.list()) > 2)
50+
self.assertEqual(1, len(self.client.files.list(limit=1)))

0 commit comments

Comments
 (0)