From 5e54061465aec916630b4309029fe83a493694f0 Mon Sep 17 00:00:00 2001 From: MattBlack85 Date: Mon, 18 May 2015 14:19:12 +0200 Subject: [PATCH 1/4] Add a chunk_size attribute to the client. requests defaults to 1, which can be not optimal. Overriding this to optimize writing --- onesky/client.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/onesky/client.py b/onesky/client.py index ef64ba7..0426290 100644 --- a/onesky/client.py +++ b/onesky/client.py @@ -11,11 +11,13 @@ class Client: def __init__(self, api_key, api_secret, api_url=DEFAULT_API_URL, + chunk_size=1, download_dir='.', request_callback=None): self.api_url = api_url self.api_key = api_key self.api_secret = api_secret + self.chunk_size = chunk_size self.download_dir = download_dir self.request_callback = request_callback @@ -77,7 +79,7 @@ def do_http_request(self, relative_url, parameters=None, method='GET', absolute_filename = os.path.join(self.download_dir, short_filename) with open(absolute_filename, 'wb') as f: - for chunk in response.iter_content(): + for chunk in response.iter_content(chunk_size=self.chunk_size): f.write(chunk) response_dict = {'downloaded_filename': absolute_filename} From 6444b71583d131ba6e29f8f720197440d9daacb0 Mon Sep 17 00:00:00 2001 From: MattBlack85 Date: Mon, 18 May 2015 14:21:40 +0200 Subject: [PATCH 2/4] Make Client inherit from object. This will mantain eventually compatibility between py2/py3 --- onesky/client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/onesky/client.py b/onesky/client.py index 0426290..0d72487 100644 --- a/onesky/client.py +++ b/onesky/client.py @@ -8,7 +8,8 @@ # python wrapper for OneSky's REST API, see # https://github.com/onesky/api-documentation-platform -class Client: +class Client(object): + def __init__(self, api_key, api_secret, api_url=DEFAULT_API_URL, chunk_size=1, From 605a984daf8ff2385b855f2b1b8f70ec681e94b7 Mon Sep 17 00:00:00 2001 From: MattBlack85 Date: Mon, 18 May 2015 14:23:20 +0200 Subject: [PATCH 3/4] Make Client immediately available when importing the package. --- README.md | 6 +++--- onesky/__init__.py | 2 ++ tests/test_client.py | 5 +++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 30139eb..df27e86 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,10 @@ Depending on your setup, you may need to run the above commands with `sudo`. Simply create a client using your API key. Most of the methods return the JSON response from OneSky as a dictionary: ```python -import onesky.client +from onesky import Client -client = onesky.client.Client(api_key='', - api_secret='') +client = Client(api_key='', + api_secret='') status, json_response = client.project_group_list() # { diff --git a/onesky/__init__.py b/onesky/__init__.py index 1a6d33d..3eb1030 100644 --- a/onesky/__init__.py +++ b/onesky/__init__.py @@ -1,2 +1,4 @@ __version_info__ = ('1', '0', '2') __version__ = '.'.join(__version_info__) + +from onesky.client import Client diff --git a/tests/test_client.py b/tests/test_client.py index c95bfa9..3a79a7a 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -6,7 +6,7 @@ import requests import unittest -import onesky.client +from onesky import Client TEST_API_KEY = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' @@ -27,6 +27,7 @@ def mock_requests_function(*args, **kwargs): class ClientTestCase(unittest.TestCase): + def setUp(self): # we mock out all of the http requests and just make sure the correct # urls and parameters and such are being passed. @@ -38,7 +39,7 @@ def setUp(self): delete=mock_requests_function) self.patcher.start() - self.client = onesky.client.Client(TEST_API_KEY, TEST_API_SECRET) + self.client = Client(TEST_API_KEY, TEST_API_SECRET) def tearDown(self): del self.client From e64b0e870c6150cd27fd816c6f53596ac12fcc8a Mon Sep 17 00:00:00 2001 From: MattBlack85 Date: Mon, 18 May 2015 14:23:54 +0200 Subject: [PATCH 4/4] I suppose this work? The way it saves now file to the disk is perfectly reasonable, was there another way it had to work? --- onesky/client.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/onesky/client.py b/onesky/client.py index 0d72487..a293033 100644 --- a/onesky/client.py +++ b/onesky/client.py @@ -184,9 +184,6 @@ def file_delete(self, project_id, file_name): ################################################################ # translation - - # TODO: this doesn't actually work; we need to get the file out of the - # payload that's returned. def translation_export(self, project_id, locale, source_file_name, export_file_name=None): relative_url = 'projects/{}/translations'.format(project_id)