From 2ce541d211e61ff3aaf65e03f52cc4e48aaf93f1 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 9 Jul 2014 20:59:24 +0200 Subject: [PATCH] refactor async calls --- pandora_client/__init__.py | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/pandora_client/__init__.py b/pandora_client/__init__.py index ed3d15b..676859d 100644 --- a/pandora_client/__init__.py +++ b/pandora_client/__init__.py @@ -596,21 +596,13 @@ class Client(object): post['files'] = files['files'] post['volume'] = name print 'sending list of files in %s (%s total)' % (name, len(post['files'])) + r = self.api.async('update', post) + #send empty list to get updated list of requested info/files/data + post = {'info': {}} r = self.api.update(post) - if r['status']['code'] == 200: - #backend works on update request asyncronously, wait for it to finish - if 'taskId' in r['data']: - t = self.api.taskStatus(task_id=r['data']['taskId']) - print 'waiting for server ...' - while t['data']['status'] == 'PENDING': - time.sleep(5) - t = self.api.taskStatus(task_id=r['data']['taskId']) - #send empty list to get updated list of requested info/files/data - post = {'info': {}} - r = self.api.update(post) - if r['data']['info']: - r = self.update_info(r['data']['info'], prefix) + if r['data']['info']: + r = self.update_info(r['data']['info'], prefix) if not 'data' in r: print r @@ -718,13 +710,7 @@ class Client(object): print 'sending info for %d files' % len(info) post = {'info': {}, 'upload': True} post['info'] = self.get_info_for_ids(info, prefix) - r = self.api.update(post) - if 'taskId' in r['data']: - t = self.api.taskStatus(task_id=r['data']['taskId']) - print 'waiting for server ...' - while t['data']['status'] == 'PENDING': - time.sleep(5) - t = self.api.taskStatus(task_id=r['data']['taskId']) + r = self.api.async('update', post) #send empty list to get updated list of requested info/files/data post = {'info': {}} r = self.api.update(post) @@ -908,6 +894,18 @@ class API(ox.API): netloc = urlparse(self.url).netloc self._resume_file = '/tmp/pandora_client.%s.%s.json' % (os.environ.get('USER'), netloc) + def async(self, action, data, interval=5): + t = r = getattr(self, action)(data) + if r['status']['code'] == 200: + # wait for async task to finish + if 'taskId' in r['data']: + t = self.taskStatus(task_id=r['data']['taskId']) + print 'waiting for server ...' + while t['data'].get('status') == 'PENDING': + time.sleep(interval) + t = self.taskStatus(task_id=r['data']['taskId']) + return t + def uploadFrames(self, i, data): #upload frames if self.site['media'].get('importFrames') and i['frames']: