public async void GetTopicRepliesAsync(string topicId, string groupId, Dictionary<string, string> parameters = null) { if (topicRepliesFetchingQueue.Contains(topicId)) return; topicRepliesFetchingQueue.Add(topicId); string timestamp = DateTimeUtils.GetTimestamp(); string nonce = Guid.NewGuid().ToString().Replace("-", null); Dictionary<string, string> paramDict = new Dictionary<string, string>(); paramDict["method"] = "flickr.groups.discuss.replies.getList"; paramDict["format"] = "json"; paramDict["nojsoncallback"] = "1"; paramDict["oauth_consumer_key"] = consumerKey; paramDict["oauth_nonce"] = nonce; paramDict["oauth_signature_method"] = "HMAC-SHA1"; paramDict["oauth_timestamp"] = timestamp; paramDict["oauth_token"] = AccessToken; paramDict["oauth_version"] = "1.0"; paramDict["topic_id"] = topicId; if (parameters != null) { foreach (var entry in parameters) { paramDict[entry.Key] = entry.Value; } } string paramString = GenerateParamString(paramDict); string signature = GenerateSignature("GET", AccessTokenSecret, "https://api.flickr.com/services/rest", paramString); string requestUrl = "https://api.flickr.com/services/rest?" + paramString + "&oauth_signature=" + signature; HttpWebResponse response = await DispatchRequest("GET", requestUrl, null).ConfigureAwait(false); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { topicRepliesFetchingQueue.Remove(topicId); if (response.StatusCode != HttpStatusCode.OK) { HandleHTTPException(response); return; } string jsonString = await reader.ReadToEndAsync().ConfigureAwait(false); if (!TryHandleResponseException(jsonString, () => { GetTopicRepliesAsync(topicId, groupId, parameters); })) return; GetTopicRepliesEventArgs args = new GetTopicRepliesEventArgs(); args.TopicId = topicId; args.Response = jsonString; args.GroupId = groupId; TopicRepliesReturned.DispatchEvent(this, args); } }
private void OnTopicRepliesReturned(object sender, GetTopicRepliesEventArgs e) { if (!Cinderella.CinderellaCore.GroupCache.ContainsKey(e.GroupId)) return; FlickrGroup group = Cinderella.CinderellaCore.GroupCache[e.GroupId]; if (!group.TopicCache.ContainsKey(e.TopicId)) return; Topic topic = group.TopicCache[e.TopicId]; JObject rawJson = JObject.Parse(e.Response); JObject rootJson = (JObject)rawJson["replies"]; JObject topicJson = (JObject)rootJson["topic"]; int TotalCount = int.Parse(topicJson["total"].ToString()); int page = int.Parse(topicJson["page"].ToString()); int numPages = int.Parse(topicJson["pages"].ToString()); int perPage = int.Parse(topicJson["per_page"].ToString()); List<TopicReply> newReplies = new List<TopicReply>(); if (TotalCount > 0) { foreach (var entry in rootJson["reply"]) { JObject json = (JObject)entry; TopicReply reply = TopicReplyFactory.TopicReplyWithJObject(json, topic); if (!topic.Replies.Contains(reply)) { topic.Replies.Add(reply); newReplies.Add(reply); } } } // Dispatch event TopicRepliesUpdatedEventArgs evt = new TopicRepliesUpdatedEventArgs(); evt.GroupId = group.ResourceId; evt.TopicId = topic.ResourceId; evt.Page = page; evt.PageCount = numPages; evt.PerPage = perPage; evt.NewReplies = newReplies; TopicRepliesUpdated.DispatchEvent(this, evt); }