Skip to content

Commit

Permalink
fix: list bucket tables method
Browse files Browse the repository at this point in the history
docs: update to new classes
  • Loading branch information
odinuv committed Aug 28, 2017
1 parent 0a64724 commit 86c41e0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ for working with buckets, tables and workspaces are covered.

## Install

`$ pip install git+https://github.com/keboola/sapi-python-client.git`
`$ pip3 install git+https://github.com/keboola/sapi-python-client.git`

or

Expand All @@ -17,24 +17,26 @@ $ python setup.py install

## Usage
```
from kbcstorage.client import Client
from kbcstorage.tables import Tables
from kbcstorage.buckets import Buckets
cl = Client("MY_KBC_TOKEN")
tables = Tables('https://connection.keboola.com', 'your-token')
# get table data into local file
cl.get_table_data("in.c-myBucket.myTable", "local_file_name.csv')
tables.export_to_file(table_id='in.c-demo.some-table', path_name='/data/')
# save data
cl.save_table("tableName", "in.c-myBucket", "csv_I_want_to_store.csv")
tables.create(name='some-table-2', bucket_id='in.c-demo', file_path='/data/some-table')
# list buckets
cl.list_buckets()
buckets = Buckets('https://connection.keboola.com', 'your-token')
buckets.list()
# list bucket tables
cl.list_tables(bucketId)
buckets.list_tables('in.c-demo')
# get table info
cl.get_table(tableId)
tables.detail('in.c-demo')
```

Expand All @@ -52,6 +54,11 @@ $ git clone https://github.com/keboola/sapi-python-client.git && cd sapi-python-
$ python setup.py test
```

or

```bash
$ docker-compose run --rm -e KBC_TEST_TOKEN -e KBC_TEST_API_URL sapi-python-client -m unittest discover
```

Under development -- all contributions very welcome :)

Expand Down
21 changes: 21 additions & 0 deletions kbcstorage/buckets.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ def list(self):

return self.get(self.base_url, headers=headers)

def list_tables(self, bucket_id, include=None):
"""
List all tables in a bucket.
Args:
bucket_id (str): Id of the bucket
include (list): Properties to list (attributes, columns)
Returns:
response_body: The parsed json from the HTTP response.
Raises:
requests.HTTPError: If the API request fails.
"""
headers = {'X-StorageApi-Token': self.token}

url = '{}/{}/tables'.format(self.base_url, bucket_id)
params = {}
if include is not None and isinstance(include, list):
params['include'] = ','.join(include)
return self.get(url, headers=headers, params=params)

def detail(self, bucket_id):
"""
Retrieves information about a given bucket.
Expand Down
19 changes: 0 additions & 19 deletions kbcstorage/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,6 @@ def list(self, include=None):
params = {'include': ','.join(include)}
return self.get(url, headers=headers, params=params)

def list_bucket(self, bucket_id, include=None):
"""
List all tables in a bucket.
Args:
bucket_id (str): Id of the bucket
include (list): Properties to list (attributes, columns)
Returns:
response_body: The parsed json from the HTTP response.
Raises:
requests.HTTPError: If the API request fails.
"""
headers = {'X-StorageApi-Token': self.token}

url = '{}/{}/tables'.format(self.base_url, bucket_id)
params = {'include': ','.join(include)}
return self.get(url, headers=headers, params=params)

def detail(self, table_id):
"""
Retrieves information about a given table.
Expand Down
27 changes: 27 additions & 0 deletions tests/test_functional_buckets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import csv
import os
import tempfile
import unittest
import warnings

from requests import exceptions
from kbcstorage.buckets import Buckets
from kbcstorage.tables import Tables


class TestFunctionalBuckets(unittest.TestCase):
Expand All @@ -13,6 +18,8 @@ def setUp(self):
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:
Expand All @@ -27,6 +34,26 @@ def test_create_bucket(self):
description='Test bucket')['id']
self.assertEqual(bucket_id, self.buckets.detail(bucket_id)['id'])

def test_list_tables(self):
bucket_id = self.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)
tables = Tables(os.getenv('KBC_TEST_API_URL'),
os.getenv('KBC_TEST_TOKEN'))
tables.create(name='some-table', file_path=path,
bucket_id='in.c-py-test')
tables = self.buckets.list_tables(bucket_id)
self.assertEqual(1, len(tables))
self.assertEqual('in.c-py-test.some-table', tables[0]['id'])

def test_bucket_detail(self):
bucket_id = self.buckets.create(name='py-test',
stage='in',
Expand Down

0 comments on commit 86c41e0

Please sign in to comment.