def remove_child(parent, child): r = requests.delete(oauth.get_metadata_url() + 'nodes/' + parent + "/children/" + child, headers=oauth.get_auth_header()) if r.status_code != http.OK: print('Removing child failed.') raise RequestError(r.status_code, r.text) return r.json()
def paginated_get_request(url, params=None, headers=None): if params is None: params = {} if headers is None: headers = {} node_list = [] while True: r = requests.get(url, params=params, headers=dict(headers, **oauth.get_auth_header())) if r.status_code != http.OK: print("Error getting node list.") raise RequestError(r.status_code, r.text) ret = r.json() node_list.extend(ret['data']) if 'nextToken' in ret.keys(): params['startToken'] = ret['nextToken'] else: if ret['count'] != len(node_list): print('Expected {} items, received {}.'.format( ret['count'], len(node_list))) break return node_list
def add_child(parent, child): r = requests.put(oauth.get_metadata_url() + 'nodes/' + parent + '/children/' + child, headers=oauth.get_auth_header()) if r.status_code != http.OK: print('Adding child failed.') raise RequestError(r.status_code, r.text) return r.json()
def get_metadata(node_id): params = {'tempLink': 'true'} r = requests.get(oauth.get_metadata_url() + 'nodes/' + node_id, headers=oauth.get_auth_header(), params=params) if r.status_code != http.OK: return RequestError(r.status_code, r.text) return r.json()
def update_metadata(node_id, properties): body = json.dumps(properties) r = requests.patch(oauth.get_metadata_url() + 'nodes/' + node_id, headers=oauth.get_auth_header(), data=body) if r.status_code != http.OK: raise RequestError(r.status_code, r.text) return r.json()
def add_child(parent, child): r = requests.put(oauth.get_metadata_url() + 'nodes/' + parent + '/children/' + child, headers=oauth.get_auth_header()) if r.status_code != http.OK: print('Adding child failed.') raise RequestError(r.status_code, r.text) return r.json()
def remove_child(parent, child): r = requests.delete(oauth.get_metadata_url() + 'nodes/' + parent + "/children/" + child, headers=oauth.get_auth_header()) if r.status_code != http.OK: print('Removing child failed.') raise RequestError(r.status_code, r.text) return r.json()
def get_changes(): r = requests.post(oauth.get_metadata_url() + 'changes', headers=oauth.get_auth_header()) if r.status_code != http.OK: raise RequestError(r.status_code, r.text) # return format: '{}\n{"end": true|false}' # TODO: check end ro = str.splitlines(r.text) return json.loads(ro[0])
def get_root_id(): params = {'filters': 'isRoot:true'} r = requests.get(oauth.get_metadata_url() + 'nodes', headers=oauth.get_auth_header(), params=params) if r.status_code != http.OK: return RequestError(r.status_code, r.text) data = r.json() if 'id' in data['data'][0]: return data['data'][0]['id']
def get_changes(): r = requests.post(oauth.get_metadata_url() + 'changes', headers=oauth.get_auth_header()) if r.status_code != http.OK: raise RequestError(r.status_code, r.text) # return format: '{}\n{"end": true|false}' # TODO: check end ro = str.splitlines(r.text) return json.loads(ro[0])
def get_root_id(): params = {'filters': 'isRoot:true'} r = requests.get(oauth.get_metadata_url() + 'nodes', headers=oauth.get_auth_header(), params=params) if r.status_code != http.OK: return RequestError(r.status_code, r.text) data = r.json() if 'id' in data['data'][0]: return data['data'][0]['id']
def _get_endpoints() -> dict: global endpoint_data r = requests.get(AMZ_ENDPOINT_REQ_URL, headers=oauth.get_auth_header()) try: e = r.json() e[EXP_TIME_KEY] = time.time() + ENDPOINT_VAL_TIME endpoint_data = e _save_endpoint_data() except ValueError as e: logger.critical('Invalid JSON.') raise e return e
def create_folder(name, parent=None): # params = {'localId' : ''} body = {'kind': 'FOLDER', 'name': name} if parent: body['parents'] = [parent] body_str = json.dumps(body) r = requests.post(oauth.get_metadata_url() + 'nodes', headers=oauth.get_auth_header(), data=body_str) if r.status_code != http.CREATED: # print('Error creating folder "%s"' % name) raise RequestError(r.status_code, r.text) return r.json()
def _request(cls, type_, url, acc_codes, **kwargs): if not cls.__session: cls.__session = requests.session() headers = oauth.get_auth_header() cls._wait() try: r = cls.__session.request(type_, url, headers=headers, timeout=REQUESTS_TIMEOUT, **kwargs) except requests.exceptions.ConnectionError as e: raise RequestError(1001, e) if r.status_code in acc_codes: cls._success() else: cls._failed() return r
def create_folder(name, parent=None): # params = {'localId' : ''} body = {'kind': 'FOLDER', 'name': name} if parent: body['parents'] = [parent] body_str = json.dumps(body) r = requests.post(oauth.get_metadata_url() + 'nodes', headers=oauth.get_auth_header(), data=body_str) if r.status_code != http.CREATED: # print('Error creating folder "%s"' % name) raise RequestError(r.status_code, r.text) return r.json()
def download_file(node_id, local_name, local_path=None, write_callback=None): r = requests.get(oauth.get_content_url() + 'nodes/' + node_id, headers=oauth.get_auth_header(), stream=True) if r.status_code != http.OK: print('Downloading %s failed.' % node_id) raise RequestError(r.status_code, r.text) dl_path = local_name if local_path: dl_path = os.path.join(local_path, local_name) with open(dl_path, 'wb') as f: total_ln = int(r.headers.get('content-length')) curr_ln = 0 for chunk in r.iter_content(chunk_size=8192): if chunk: # filter out keep-alive new chunks f.write(chunk) f.flush() if write_callback: write_callback(chunk) curr_ln += len(chunk) progress(0, 0, total_ln, curr_ln) print() # break progress line return # no response text?
def paginated_get_request(url, params=None, headers=None): if params is None: params = {} if headers is None: headers = {} node_list = [] while True: r = requests.get(url, params=params, headers=dict(headers, **oauth.get_auth_header())) if r.status_code != http.OK: print("Error getting node list.") raise RequestError(r.status_code, r.text) ret = r.json() node_list.extend(ret['data']) if 'nextToken' in ret.keys(): params['startToken'] = ret['nextToken'] else: if ret['count'] != len(node_list): print('Expected {} items, received {}.'.format(ret['count'], len(node_list))) break return node_list
def download_file(node_id, local_name, local_path=None, write_callback=None): r = requests.get(oauth.get_content_url() + 'nodes/' + node_id, headers=oauth.get_auth_header(), stream=True) if r.status_code != http.OK: # print('Downloading %s failed.' % node_id) raise RequestError(r.status_code, r.text) dl_path = local_name if local_path: dl_path = os.path.join(local_path, local_name) with open(dl_path, 'wb') as f: total_ln = int(r.headers.get('content-length')) curr_ln = 0 for chunk in r.iter_content(chunk_size=8192): if chunk: # filter out keep-alive new chunks f.write(chunk) f.flush() if write_callback: write_callback(chunk) curr_ln += len(chunk) progress(0, 0, total_ln, curr_ln) print() # break progress line return # no response text?
def get_metadata(node_id): params = {'tempLink': 'true'} r = requests.get(oauth.get_metadata_url() + 'nodes/' + node_id, headers=oauth.get_auth_header(), params=params) if r.status_code != http.OK: return RequestError(r.status_code, r.text) return r.json()
def move_to_trash(node): r = requests.put(oauth.get_metadata_url() + 'trash/' + node, headers=oauth.get_auth_header()) if r.status_code != http.OK: raise RequestError(r.status_code, r.text) return r.json()
def restore(node): r = requests.post(oauth.get_metadata_url() + 'trash/' + node + '/restore', headers=oauth.get_auth_header()) if r.status_code != http.OK: raise RequestError(r.status_code, r.text) return r.json()
def list_children(node_id): r = requests.get(oauth.get_metadata_url() + 'nodes/' + node_id + '/children', headers=oauth.get_auth_header()) return r.json
def update_metadata(node_id, properties): body = json.dumps(properties) r = requests.patch(oauth.get_metadata_url() + 'nodes/' + node_id, headers=oauth.get_auth_header(), data=body) if r.status_code != http.OK: raise RequestError(r.status_code, r.text) return r.json()
def purge(node): r = requests.delete(oauth.get_metadata_url() + 'nodes/' + node, headers=oauth.get_auth_header()) if r.status_code != http.OK: raise RequestError(r.status_code, r.text) return r.json()
def get_account_usage(): r = requests.get(oauth.get_metadata_url() + 'account/usage', headers=oauth.get_auth_header()) if r.status_code != http.OK: raise RequestError(r.status_code, r.text) return r.json()
def list_properties(node_id): owner_id = '' r = requests.get(oauth.get_metadata_url() + "/nodes/" + node_id + "/properties/" + owner_id, headers=oauth.get_auth_header()) return r.text
def list_properties(node_id): owner_id = '' r = requests.get(oauth.get_metadata_url() + "/nodes/" + node_id + "/properties/" + owner_id, headers=oauth.get_auth_header()) return r.text
def get_quota(): r = requests.get(oauth.get_metadata_url() + 'account/quota', headers=oauth.get_auth_header()) if r.status_code != http.OK: raise RequestError(r.status_code, r.text) return r.json()
def list_children(node_id): r = requests.get(oauth.get_metadata_url() + 'nodes/' + node_id + '/children', headers=oauth.get_auth_header()) return r.json