def test_http_request_to_from_json(self): def _postproc(*kwargs): pass http = httplib2.Http() media_upload = MediaFileUpload( datafile('small.png'), chunksize=500, resumable=True) req = HttpRequest( http, _postproc, 'http://example.com', method='POST', body='{}', headers={'content-type': 'multipart/related; boundary="---flubber"'}, methodId='foo', resumable=media_upload) json = req.to_json() new_req = HttpRequest.from_json(json, http, _postproc) self.assertEquals(new_req.headers, {'content-type': 'multipart/related; boundary="---flubber"'}) self.assertEquals(new_req.uri, 'http://example.com') self.assertEquals(new_req.body, '{}') self.assertEquals(new_req.http, http) self.assertEquals(new_req.resumable.to_json(), media_upload.to_json()) self.assertEquals(new_req.multipart_boundary, '---flubber--')
def test_http_request_to_from_json(self): def _postproc(*kwargs): pass http = httplib2.Http() media_upload = MediaFileUpload(datafile('small.png'), chunksize=500, resumable=True) req = HttpRequest(http, _postproc, 'http://example.com', method='POST', body='{}', headers={ 'content-type': 'multipart/related; boundary="---flubber"' }, methodId='foo', resumable=media_upload) json = req.to_json() new_req = HttpRequest.from_json(json, http, _postproc) self.assertEqual( {'content-type': 'multipart/related; boundary="---flubber"'}, new_req.headers) self.assertEqual('http://example.com', new_req.uri) self.assertEqual('{}', new_req.body) self.assertEqual(http, new_req.http) self.assertEqual(media_upload.to_json(), new_req.resumable.to_json()) self.assertEqual(random.random, new_req._rand) self.assertEqual(time.sleep, new_req._sleep)
def test_http_request_to_from_json(self): def _postproc(*kwargs): pass http = httplib2.Http() media_upload = MediaFileUpload(datafile("small.png"), chunksize=500, resumable=True) req = HttpRequest( http, _postproc, "http://example.com", method="POST", body="{}", headers={"content-type": 'multipart/related; boundary="---flubber"'}, methodId="foo", resumable=media_upload, ) json = req.to_json() new_req = HttpRequest.from_json(json, http, _postproc) self.assertEqual({"content-type": 'multipart/related; boundary="---flubber"'}, new_req.headers) self.assertEqual("http://example.com", new_req.uri) self.assertEqual("{}", new_req.body) self.assertEqual(http, new_req.http) self.assertEqual(media_upload.to_json(), new_req.resumable.to_json())
def test_media_file_upload_to_from_json(self): upload = MediaFileUpload(datafile("small.png"), chunksize=500, resumable=True) self.assertEqual("image/png", upload.mimetype()) self.assertEqual(190, upload.size()) self.assertEqual(True, upload.resumable()) self.assertEqual(500, upload.chunksize()) self.assertEqual("PNG", upload.getbytes(1, 3)) json = upload.to_json() new_upload = MediaUpload.new_from_json(json) self.assertEqual("image/png", new_upload.mimetype()) self.assertEqual(190, new_upload.size()) self.assertEqual(True, new_upload.resumable()) self.assertEqual(500, new_upload.chunksize()) self.assertEqual("PNG", new_upload.getbytes(1, 3))
def test_media_file_upload_to_from_json(self): upload = MediaFileUpload( datafile('small.png'), chunksize=500, resumable=True) self.assertEqual('image/png', upload.mimetype()) self.assertEqual(190, upload.size()) self.assertEqual(True, upload.resumable()) self.assertEqual(500, upload.chunksize()) self.assertEqual('PNG', upload.getbytes(1, 3)) json = upload.to_json() new_upload = MediaUpload.new_from_json(json) self.assertEqual('image/png', new_upload.mimetype()) self.assertEqual(190, new_upload.size()) self.assertEqual(True, new_upload.resumable()) self.assertEqual(500, new_upload.chunksize()) self.assertEqual('PNG', new_upload.getbytes(1, 3))
def updateFile(service, fileID, path, createRevision): log_msg("Updating file: " + path + ". Progress: ", newline="", color=Fore.CYAN) file = service.files().get(fileId=fileID).execute() media_body = MediaFileUpload(path, resumable=True) if '"_mimetype": null' in media_body.to_json(): #unrecognized mimetype: set it manually media_body = MediaFileUpload(path, mimetype=getMimeType(path), resumable=True) request = service.files().update(fileId=fileID, body=file, newRevision=createRevision, media_body=media_body) while True: status, done = request.next_chunk() if status: progress = int(status.progress() * 100) if progress < 100: log_msg("%d%%" % progress, newline=" ... ") if done: log_msg("100%", newline=" ") log_msg("Done!", color=Fore.CYAN) return
def uploadFile(service, parentID, path): log_msg("Uploading file: " + path + ". Progress: ", newline="", color=Fore.GREEN) media_body = MediaFileUpload(path, resumable=True) if '"_mimetype": null' in media_body.to_json(): #unrecognized mimetype: set it manually media_body = MediaFileUpload(path, mimetype=getMimeType(path), resumable=True) body = {"title": os.path.basename(path)} if parentID: #set the parent folder body["parents"] = [{"id": parentID}] request = service.files().insert(media_body=media_body, body=body) while True: status, done = request.next_chunk() if status: progress = int(status.progress() * 100) if progress < 100: log_msg("%d%%" % progress, newline=" ... ") if done: id = request.execute()["id"] log_msg("100%", newline=" ") log_msg("Done! ID: " + id, color=Fore.GREEN) return id