Skip to content

Commit

Permalink
Merge pull request #31 from keboola/client-class
Browse files Browse the repository at this point in the history
feat: entry client class
  • Loading branch information
pivnicek authored Aug 30, 2017
2 parents 038f67a + 801c2c7 commit d9c241a
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
34 changes: 34 additions & 0 deletions kbcstorage/client.py
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)
50 changes: 50 additions & 0 deletions tests/test_functional_client.py
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)))

0 comments on commit d9c241a

Please sign in to comment.