def upload_file(local_path, cloud_path): url_params = {'path': cloud_path} resp = send_req_with_status_code_check( lambda: requests.get(f'{conf.YANDEX_URL}/upload', params=url_params, headers=conf.YANDEX_HEADERS), HTTPStatus.OK) link = resp.json()['href'] with open(local_path, 'rb') as f: resp = send_req_with_status_code_check( lambda: requests.put(link, data=f), HTTPStatus.CREATED) return resp
def download_file(local_path, cloud_path): if not os.path.exists(local_path): raise FileNotFoundError url_params = {'path': cloud_path} resp1 = send_req_with_status_code_check( lambda: requests.get(f'{conf.YANDEX_URL}/download', params=url_params, headers=conf.YANDEX_HEADERS), HTTPStatus.OK) link = resp1.json()['href'] resp2 = send_req_with_status_code_check( lambda: requests.get(link, headers=conf.YANDEX_HEADERS), HTTPStatus.OK) file_name = cloud_path.split('/')[-1] with open(f'{local_path}/{file_name}', 'wb') as f: f.write(resp2.content) return resp2
def start_upload_session(data): headers = conf.DROPBOX_AUTH_HEADERS.copy() headers['Content-Type'] = 'application/octet-stream' resp = send_req_with_status_code_check( lambda: requests.post( url=f'{conf.DROPBOX_CONTENT_URL}/upload_session/start', headers=headers, data=data), HTTPStatus.OK) return resp
def create_cloud_dir(cloud_path): headers = conf.DROPBOX_AUTH_HEADERS.copy() headers['Content-Type'] = 'application/json' data = json.dumps({'path': cloud_path}) resp = send_req_with_status_code_check( lambda: requests.post(url=f'{conf.DROPBOX_API_URL}/create_folder_v2', headers=headers, data=data), HTTPStatus.OK) return resp
def get_dir(cloud_path): headers = conf.DROPBOX_AUTH_HEADERS headers['Content-Type'] = 'application/json' data = {"path": cloud_path} resp = send_req_with_status_code_check( lambda: requests.post(url=f'{conf.DROPBOX_API_URL}/list_folder', headers=headers, data=json.dumps(data)), HTTPStatus.OK) return resp
def download_file(local_path, cloud_path): if not os.path.exists(local_path): raise FileNotFoundError headers = conf.DROPBOX_AUTH_HEADERS headers['Dropbox-API-Arg'] = json.dumps({'path': cloud_path}) resp = send_req_with_status_code_check( lambda: requests.post(url=f'{conf.DROPBOX_CONTENT_URL}/download', headers=headers), HTTPStatus.OK) file_name = cloud_path.split('/')[-1] with open(f'{local_path}/{file_name}', 'wb') as f: f.write(resp.content) return resp
def append_to_upload_session(session_id, offset, data): headers = conf.DROPBOX_AUTH_HEADERS.copy() headers['Content-Type'] = 'application/octet-stream' headers['Dropbox-API-Arg'] = json.dumps( {'cursor': { 'session_id': session_id, 'offset': offset }}) resp = send_req_with_status_code_check( lambda: requests.post( url=f'{conf.DROPBOX_CONTENT_URL}/upload_session/append_v2', headers=headers, data=data), HTTPStatus.OK) return resp
def finish_upload_session(session_id, offset, cloud_path, data): headers = conf.DROPBOX_AUTH_HEADERS.copy() headers['Content-Type'] = 'application/octet-stream' headers['Dropbox-API-Arg'] = json.dumps({ 'cursor': { 'session_id': session_id, 'offset': offset }, 'commit': { 'path': cloud_path, 'strict_conflict': True } }) resp = send_req_with_status_code_check( lambda: requests.post( url=f'{conf.DROPBOX_CONTENT_URL}/upload_session/finish', headers=headers, data=data), HTTPStatus.OK) return resp
def get_dir(cloud_path): resp = send_req_with_status_code_check( lambda: requests.get(conf.YANDEX_URL, params={'path': cloud_path}, headers=conf.YANDEX_HEADERS), HTTPStatus.OK) return resp
def create_cloud_dir(cloud_path): resp = send_req_with_status_code_check( lambda: requests.put(conf.YANDEX_URL, params={'path': cloud_path}, headers=conf.YANDEX_HEADERS), HTTPStatus.CREATED) return resp