static async Task SaveBatches(Stack <LCBatch> batches) { while (batches.Count > 0) { LCBatch batch = batches.Pop(); // 特殊处理 File 依赖 IEnumerable <LCFile> dirtyFiles = batch.objects.Where(item => (item is LCFile) && item.IsDirty) .Cast <LCFile>(); foreach (LCFile file in dirtyFiles) { await file.Save(); } List <LCObject> dirtyObjects = batch.objects.Where(item => item.IsDirty) .ToList(); if (dirtyObjects.Count == 0) { continue; } List <Dictionary <string, object> > requestList = dirtyObjects.Select(item => { string path = item.ObjectId == null ? $"/1.1/classes/{item.ClassName}" : $"/1.1/classes/{item.ClassName}/{item.ClassName}"; string method = item.ObjectId == null ? "POST" : "PUT"; Dictionary <string, object> body = LCEncoder.Encode(item.operationDict) as Dictionary <string, object>; return(new Dictionary <string, object> { { "path", path }, { "method", method }, { "body", body } }); }).ToList(); Dictionary <string, object> data = new Dictionary <string, object> { { "requests", LCEncoder.Encode(requestList) } }; List <Dictionary <string, object> > results = await LCCore.HttpClient.Post <List <Dictionary <string, object> > >("batch", data : data); List <LCObjectData> resultList = results.Select(item => { if (item.TryGetValue("error", out object error)) { Dictionary <string, object> err = error as Dictionary <string, object>; int code = (int)err["code"]; string message = (string)err["error"]; throw new LCException(code, message as string); } return(LCObjectData.Decode(item["success"] as IDictionary)); }).ToList(); for (int i = 0; i < dirtyObjects.Count; i++) { LCObject obj = dirtyObjects[i]; LCObjectData objData = resultList[i]; obj.Merge(objData); } } }
/// <summary> /// Deserializes a JSON string to a LCObject. /// </summary> /// <param name="json"></param> /// <returns></returns> public static LCObject ParseObject(string json) { LCObjectData objectData = LCObjectData.Decode(JsonConvert.DeserializeObject <Dictionary <string, object> >(json)); LCObject obj = Create(objectData.ClassName); obj.Merge(objectData); return(obj); }
/// <summary> /// Gets the followers and followees of the currently logged in user. /// </summary> /// <param name="includeFollower"></param> /// <param name="includeFollowee"></param> /// <param name="returnCount"></param> /// <returns></returns> public async Task <LCFollowersAndFollowees> GetFollowersAndFollowees(bool includeFollower = false, bool includeFollowee = false, bool returnCount = false) { Dictionary <string, object> queryParams = new Dictionary <string, object>(); if (returnCount) { queryParams["count"] = 1; } if (includeFollower || includeFollowee) { List <string> includes = new List <string>(); if (includeFollower) { includes.Add("follower"); } if (includeFollowee) { includes.Add("followee"); } queryParams["include"] = string.Join(",", includes); } string path = $"users/{ObjectId}/followersAndFollowees"; Dictionary <string, object> response = await LCCore.HttpClient.Get <Dictionary <string, object> >(path, queryParams : queryParams); LCFollowersAndFollowees result = new LCFollowersAndFollowees(); if (response.TryGetValue("followers", out object followersObj) && (followersObj is List <object> followers)) { result.Followers = new List <LCObject>(); foreach (object followerObj in followers) { LCObjectData objectData = LCObjectData.Decode(followerObj as IDictionary); LCObject follower = new LCObject("_Follower"); follower.Merge(objectData); result.Followers.Add(follower); } } if (response.TryGetValue("followees", out object followeesObj) && (followeesObj is List <object> followees)) { result.Followees = new List <LCObject>(); foreach (object followeeObj in followees) { LCObjectData objectData = LCObjectData.Decode(followeeObj as IDictionary); LCObject followee = new LCObject("_Followee"); followee.Merge(objectData); result.Followees.Add(followee); } } if (response.TryGetValue("followers_count", out object followersCountObj) && (followersCountObj is int followersCount)) { result.FollowersCount = followersCount; } if (response.TryGetValue("followees_count", out object followeesCountObj) && (followeesCountObj is int followeesCount)) { result.FolloweesCount = followeesCount; } return(result); }