public static OAuth20Token FromJson(String json) { var parser = new JsonHelper(); if (!parser.ParseJsonMessage(json)) return null; var accessToken = parser.GetProperty("access_token"); var refreshToken = parser.GetProperty("refresh_token"); if (String.IsNullOrEmpty(accessToken) || String.IsNullOrEmpty(refreshToken)) return null; var token = new OAuth20Token { AccessToken = accessToken, RefreshToken = refreshToken, ClientID = parser.GetProperty("client_id"), ClientSecret = parser.GetProperty("client_secret"), RedirectUri = parser.GetProperty("redirect_uri"), }; double expiresIn; if (double.TryParse(parser.GetProperty("expires_in"), out expiresIn)) token.ExpiresIn = expiresIn; DateTime timestamp; if (DateTime.TryParse(parser.GetProperty("timestamp"), out timestamp)) token.Timestamp = timestamp; return token; }
internal DropBoxQuotaInfo(String jmstext) { var jh = new JsonHelper(); if (jh.ParseJsonMessage(jmstext)) { SharedBytes = Convert.ToUInt64(jh.GetProperty("shared")); QuotaBytes = Convert.ToUInt64(jh.GetProperty("quota")); NormalBytes = Convert.ToUInt64(jh.GetProperty("normal")); } }
public DropBoxToken(String jsonString) : base("", "") { var jh = new JsonHelper(); if (jh.ParseJsonMessage(jsonString)) { TokenSecret = jh.GetProperty("secret"); TokenKey = jh.GetProperty("token"); } }
public static BaseFileEntry UpdateObjectFromJsonString(String jsonMessage, BaseFileEntry objectToUpdate, IStorageProviderService service, IStorageProviderSession session) { // verify if we have a directory or a file var jc = new JsonHelper(); if (!jc.ParseJsonMessage(jsonMessage)) throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidFileOrDirectoryName); var isDir = jc.GetBooleanProperty("is_dir"); // create the entry BaseFileEntry dbentry; Boolean bEntryOk; if (isDir) { if (objectToUpdate == null) dbentry = new BaseDirectoryEntry("Name", 0, DateTime.Now, service, session); else dbentry = objectToUpdate as BaseDirectoryEntry; bEntryOk = BuildDirectyEntry(dbentry as BaseDirectoryEntry, jc, service, session); } else { if (objectToUpdate == null) dbentry = new BaseFileEntry("Name", 0, DateTime.Now, service, session); else dbentry = objectToUpdate; bEntryOk = BuildFileEntry(dbentry, jc); } // parse the childs and fill the entry as self if (!bEntryOk) throw new SharpBoxException(SharpBoxErrorCodes.ErrorCouldNotContactStorageService); // set the is deleted flag try { // try to read the is_deleted property dbentry.IsDeleted = jc.GetBooleanProperty("is_deleted"); } catch (Exception) { // the is_deleted proprty is missing (so it's not a deleted file or folder) dbentry.IsDeleted = false; } // return the child return dbentry; }
internal DropBoxAccountInfo(String jmstext) { var jh = new JsonHelper(); if (!jh.ParseJsonMessage(jmstext)) return; Country = jh.GetProperty("country"); DisplayName = jh.GetProperty("display_name"); UserId = jh.GetPropertyInt("uid"); var quotainfo = jh.GetSubObjectString("quota_info"); if (quotainfo != null) QuotaInfo = new DropBoxQuotaInfo(quotainfo.ToString()); }
private static Boolean BuildDirectyEntry(BaseDirectoryEntry dirEntry, JsonHelper jh, IStorageProviderService service, IStorageProviderSession session) { // build the file entry part if (!BuildFileEntry(dirEntry, jh)) return false; // now take the content var content = jh.GetListProperty("contents"); if (content.Count == 0) return true; // remove all childs dirEntry.ClearChilds(); // add the childs foreach (var jsonContent in content) { // parse the item var jc = new JsonHelper(); if (!jc.ParseJsonMessage(jsonContent)) continue; // check if we have a directory var isDir = jc.GetBooleanProperty("is_dir"); BaseFileEntry fentry; if (isDir) { fentry = new BaseDirectoryEntry("Name", 0, DateTime.Now, service, session); } else { fentry = new BaseFileEntry("Name", 0, DateTime.Now, service, session); } // build the file attributes BuildFileEntry(fentry, jc); // establish parent child realtionship dirEntry.AddChild(fentry); } // set the length dirEntry.Length = dirEntry.Count; // go ahead return true; }
private static JsonHelper CreateParser(String json) { var parser = new JsonHelper(); if (!parser.ParseJsonMessage(json)) throw new SkyDriveParserException("Can not parse this JSON message"); return parser; }
public override void UploadChunk(IStorageProviderSession session, IResumableUploadSession uploadSession, Stream stream, long chunkLength) { if (uploadSession.Status == ResumableUploadSessionStatus.Completed || uploadSession.Status == ResumableUploadSessionStatus.Aborted) throw new InvalidOperationException("Upload session was either completed or aborted."); if (stream == null) throw new ArgumentNullException("stream"); var requestParams = new Dictionary<string, string>(); if (uploadSession.Status == ResumableUploadSessionStatus.Started) { requestParams.Add("upload_id", uploadSession.GetItem<string>("UploadId")); requestParams.Add("offset", uploadSession.BytesTransfered.ToString()); } var request = new OAuthService().CreateWebRequest(GetUrlString(DropBoxChunkedUpload, session.ServiceConfiguration), "PUT", null, null, ((DropBoxStorageProviderSession)session).Context, (DropBoxToken)session.SessionToken, requestParams); request.ContentLength = chunkLength; using (var requestStream = request.GetRequestStream()) { stream.CopyTo(requestStream); } using (var response = request.GetResponse()) using (var responseStream = response.GetResponseStream()) { if (responseStream == null) return; var json = new JsonHelper(); json.ParseJsonMessage(new StreamReader(responseStream).ReadToEnd()); var uplSession = (ResumableUploadSession)uploadSession; uplSession["UploadId"] = json.GetProperty("upload_id"); uplSession["Expired"] = json.GetDateTimeProperty("expired"); uplSession.BytesTransfered += chunkLength; uplSession.Status = ResumableUploadSessionStatus.Started; if (uplSession.BytesToTransfer == uploadSession.BytesTransfered) { CommitUploadSession(session, uploadSession); } } }