Exemplo n.º 1
0
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()
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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()
Exemplo n.º 4
0
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()
Exemplo n.º 5
0
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()
Exemplo n.º 6
0
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()
Exemplo n.º 7
0
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()
Exemplo n.º 8
0
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])
Exemplo n.º 9
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']
Exemplo n.º 10
0
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])
Exemplo n.º 11
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']
Exemplo n.º 12
0
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
Exemplo n.º 13
0
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()
Exemplo n.º 14
0
 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
Exemplo n.º 15
0
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()
Exemplo n.º 16
0
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?
Exemplo n.º 17
0
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
Exemplo n.º 18
0
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?
Exemplo n.º 19
0
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()
Exemplo n.º 20
0
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()
Exemplo n.º 21
0
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()
Exemplo n.º 22
0
def list_children(node_id):
    r = requests.get(oauth.get_metadata_url() + 'nodes/' + node_id +
                     '/children',
                     headers=oauth.get_auth_header())
    return r.json
Exemplo n.º 23
0
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()
Exemplo n.º 24
0
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()
Exemplo n.º 25
0
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()
Exemplo n.º 26
0
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
Exemplo n.º 27
0
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
Exemplo n.º 28
0
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()
Exemplo n.º 29
0
def list_children(node_id):
    r = requests.get(oauth.get_metadata_url() + 'nodes/' + node_id + '/children', headers=oauth.get_auth_header())
    return r.json