public UnityWebResponse GetResponse(Action<UnityWebResponse> callback) { UnityWebResponse response = new UnityWebResponse(this); if (callback != null) { response.done = (coroutine) => { callback(coroutine as UnityWebResponse); }; } return response; }
/// <summary> /// Upload a file. /// </summary> /// <param name="file">File metadata.</param> /// <param name="data">Data.</param> /// <returns>AsyncSuccess with File or Exception for error.</returns> /// <example> /// Upload a file to the root folder. /// <code> /// var bytes = Encoding.UTF8.GetBytes("world!"); /// /// var file = new GoogleDrive.File(new Dictionary<string, object> /// { /// { "title", "hello.txt" }, /// { "mimeType", "text/plain" }, /// }); /// /// StartCoroutine(drive.UploadFile(file, bytes)); /// </code> /// Update the file content. /// <code> /// var listFiles = drive.ListFilesByQueary("title = 'a.txt'"); /// yield return StartCoroutine(listFiles); /// /// var files = GoogleDrive.GetResult<List<GoogleDrive.File>>(listFiles); /// if (files != null && files.Count > 0) /// { /// var bytes = Encoding.UTF8.GetBytes("new content."); /// StartCoroutine(drive.UploadFile(files[0], bytes)); /// } /// </code> /// </example> public IEnumerator UploadFile(File file, byte[] data) { #region Check the access token is expired var check = CheckExpiration(); while (check.MoveNext()) yield return null; if (check.Current is Exception) { yield return check.Current; yield break; } #endregion string uploadUrl = null; // Start a resumable session. if (file.ID == null || file.ID == string.Empty) { var request = new UnityWebRequest( "https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable"); request.method = "POST"; request.headers["Authorization"] = "Bearer " + AccessToken; request.headers["Content-Type"] = "application/json"; request.headers["X-Upload-Content-Type"] = file.MimeType; request.headers["X-Upload-Content-Length"] = data.Length; string metadata = JsonWriter.Serialize(file.ToJSON()); request.body = Encoding.UTF8.GetBytes(metadata); var response = new UnityWebResponse(request); while (!response.isDone) yield return null; if (response.statusCode != 200) { JsonReader reader = new JsonReader(response.text); var json = reader.Deserialize<Dictionary<string, object>>(); if (json == null) { yield return new Exception(-1, "UploadFile response parsing failed."); yield break; } else if (json.ContainsKey("error")) { yield return GetError(json); yield break; } } // Save the resumable session URI. uploadUrl = response.headers["Location"] as string; } else { uploadUrl = "https://www.googleapis.com/upload/drive/v2/files/" + file.ID; } // Upload the file. { var request = new UnityWebRequest(uploadUrl); request.method = "PUT"; request.headers["Authorization"] = "Bearer " + AccessToken; request.headers["Content-Type"] = "application/octet-stream"; // file.MimeType; request.body = data; var response = new UnityWebResponse(request); while (!response.isDone) yield return null; JsonReader reader = new JsonReader(response.text); var json = reader.Deserialize<Dictionary<string, object>>(); if (json == null) { yield return new Exception(-1, "UploadFile response parsing failed."); yield break; } else if (json.ContainsKey("error")) { yield return GetError(json); yield break; } yield return new AsyncSuccess(new File(json)); } }
/// <summary> /// Download a file content. /// </summary> /// <param name="file">Download URL.</param> /// <returns>AsyncSuccess with byte[] or Exception for error.</returns> /// <example> /// Download the thumbnail image. /// <code> /// if (file.ThumbnailLink != null) /// { /// var download = drive.DownloadFile(file.ThumbnailLink); /// yield return StartCoroutine(drive); /// /// var data = GoogleDrive.GetResult<byte[]>(download); /// if (data != null) /// someTexture.LoadImage(data); /// } /// </code> /// </example> public IEnumerator DownloadFile(string url) { #region Check the access token is expired var check = CheckExpiration(); while (check.MoveNext()) yield return null; if (check.Current is Exception) { yield return check.Current; yield break; } #endregion var request = new UnityWebRequest(url); request.headers["Authorization"] = "Bearer " + AccessToken; var response = new UnityWebResponse(request); while (!response.isDone) yield return null; yield return new AsyncSuccess(response.bytes); }
/// <summary> /// Duplicate a file. /// </summary> /// <param name="file">File to duplicate.</param> /// <param name="newFile">New file data.</param> /// <returns>AsyncSuccess with File or Exception for error.</returns> /// <example> /// Copy 'someFile' to 'newFile'. /// <code> /// var newFile = new GoogleDrive.File(new Dictionary<string, object> /// { /// { "title", someFile.Title + "(2)" }, /// { "mimeType", someFile.MimeType }, /// }); /// newFile.Parents = new List<string> { newParentFolder.ID }; /// /// StartCoroutine(drive.DuplicateFile(someFile, newFile)); /// </code> /// </example> IEnumerator DuplicateFile(File file, File newFile) { #region Check the access token is expired var check = CheckExpiration(); while (check.MoveNext()) yield return null; if (check.Current is Exception) { yield return check.Current; yield break; } #endregion var request = new UnityWebRequest("https://www.googleapis.com/drive/v2/files/" + file.ID + "/copy"); request.method = "POST"; request.headers["Authorization"] = "Bearer " + AccessToken; request.headers["Content-Type"] = "application/json"; string metadata = JsonWriter.Serialize(newFile.ToJSON()); request.body = Encoding.UTF8.GetBytes(metadata); var response = new UnityWebResponse(request); while (!response.isDone) yield return null; JsonReader reader = new JsonReader(response.text); var json = reader.Deserialize<Dictionary<string, object>>(); if (json == null) { yield return new Exception(-1, "DuplicateFile response parsing failed."); yield break; } else if (json.ContainsKey("error")) { yield return GetError(json); yield break; } yield return new AsyncSuccess(new File(json)); }
/// <summary> /// Touch a file(or folder). /// </summary> /// <param name="file">File to touch.</param> /// <returns>AsyncSuccess with File or Exception for error.</returns> /// <example> /// <code> /// StartCoroutine(drive.TouchFile(someFile)); /// </code> /// </example> public IEnumerator TouchFile(File file) { #region Check the access token is expired var check = CheckExpiration(); while (check.MoveNext()) yield return null; if (check.Current is Exception) { yield return check.Current; yield break; } #endregion var request = new UnityWebRequest("https://www.googleapis.com/drive/v2/files/" + file.ID + "/touch"); request.method = "POST"; request.headers["Authorization"] = "Bearer " + AccessToken; request.body = new byte[0]; // with no data var response = new UnityWebResponse(request); while (!response.isDone) yield return null; JsonReader reader = new JsonReader(response.text); var json = reader.Deserialize<Dictionary<string, object>>(); if (json == null) { yield return new Exception(-1, "TouchFile response parsing failed."); yield break; } else if (json.ContainsKey("error")) { yield return GetError(json); yield break; } yield return new AsyncSuccess(new File(json)); }
/// <summary> /// Delete a file(or folder). /// </summary> /// <param name="file">File.</param> /// <returns>AsyncSuccess or Exception for error.</returns> /// <example> /// Delete all files. /// <code> /// var listFiles = drive.ListAllFiles(); /// yield return StartCoroutine(listFiles); /// var files = GoogleDrive.GetResult<List<GoogleDrive.File>>(listFiles); /// /// if (files != null) /// { /// for (int i = 0; i < files.Count; i++) /// yield return StartCoroutine(drive.DeleteFile(files[i])); /// } /// </code> /// </example> public IEnumerator DeleteFile(File file) { #region Check the access token is expired var check = CheckExpiration(); while (check.MoveNext()) yield return null; if (check.Current is Exception) { yield return check.Current; yield break; } #endregion var request = new UnityWebRequest("https://www.googleapis.com/drive/v2/files/" + file.ID); request.method = "DELETE"; request.headers["Authorization"] = "Bearer " + AccessToken; var response = new UnityWebResponse(request); while (!response.isDone) yield return null; // If successful, empty response. JsonReader reader = new JsonReader(response.text); var json = reader.Deserialize<Dictionary<string, object>>(); if (json != null && json.ContainsKey("error")) { yield return GetError(json); yield break; } yield return new AsyncSuccess(); }
public IEnumerator ListFilesByQueary(string query) { #region Check the access token is expired var check = CheckExpiration(); while (check.MoveNext()) yield return null; if (check.Current is Exception) { yield return check.Current; yield break; } #endregion var request = new UnityWebRequest( new Uri("https://www.googleapis.com/drive/v2/files?q=" + query)); request.headers["Authorization"] = "Bearer " + AccessToken; var response = new UnityWebResponse(request); while (!response.isDone) yield return null; JsonReader reader = new JsonReader(response.text); var json = reader.Deserialize<Dictionary<string, object>>(); if (json == null) { yield return new Exception(-1, "ListFiles response parsing failed."); yield break; } else if (json.ContainsKey("error")) { yield return GetError(json); yield break; } // parsing var results = new List<File>(); if (json.ContainsKey("items") && json["items"] is Dictionary<string, object>[]) { var items = json["items"] as Dictionary<string, object>[]; foreach (var item in items) { results.Add(new File(item)); } } yield return new AsyncSuccess(results); }
/// <summary> /// Insert a folder to otehr folder. /// </summary> /// <param name="parentFolder">Parent folder.</param> /// <returns>AsyncSuccess with File or Exception for error.</returns> /// <example> /// <code> /// var insert = drive.InsertFolder("new_folder_in_appdata", drive.AppData); /// yield return StartCoroutine(insert); /// </code> /// </example> public IEnumerator InsertFolder(string title, File parentFolder) { #region Check the access token is expired var check = CheckExpiration(); while (check.MoveNext()) yield return null; if (check.Current is Exception) { yield return check.Current; yield break; } #endregion var request = new UnityWebRequest("https://www.googleapis.com/drive/v2/files"); request.method = "POST"; request.headers["Authorization"] = "Bearer " + AccessToken; request.headers["Content-Type"] = "application/json"; Dictionary<string, object> data = new Dictionary<string, object>(); data["title"] = title; data["mimeType"] = "application/vnd.google-apps.folder"; if (parentFolder != null) { data["parents"] = new List<Dictionary<string, string>> { new Dictionary<string, string> { { "id", parentFolder.ID } }, }; } request.body = Encoding.UTF8.GetBytes(JsonWriter.Serialize(data)); var response = new UnityWebResponse(request); while (!response.isDone) yield return null; JsonReader reader = new JsonReader(response.text); var json = reader.Deserialize<Dictionary<string, object>>(); if (json == null) { yield return new Exception(-1, "InsertFolder response parsing failed."); yield break; } else if (json.ContainsKey("error")) { yield return GetError(json); yield break; } yield return new AsyncSuccess(new File(json)); }