/// <summary> /// client to get json string of a site's or post's comments /// </summary> /// <param name="site_or_postid">the site url. insert without http:// prefix or the post id</param> /// <param name="listType">the type of comments list to fetch</param> /// <param name="type">the comments type to return</param> /// <param name="status">the comments status to return</param> /// <param name="number">The number of categories to return. Limit: 100. Default: 20.</param> /// <param name="offset">the 0-indexed offset for the request. Default value goes to 0. Use this parameter for pagination.</param> /// <returns>raw json string for comments of a site or post</returns> private async Task <HttpResponseMessage> getComments(string site, CommentsListType listType, CommentType type, CommentStatus status, string post_id = null, int?number = null, int?offset = null) { string url = string.Empty; if (!number.HasValue) { number = 20; } if (!offset.HasValue) { offset = 0; } if (listType == CommentsListType.site) { url = siteCommentsUrl(site, type, status, number, offset); } if (listType == CommentsListType.post) { url = postCommentsUrl(site, post_id, type, status, number, offset); } HttpClient client = new HttpClient(); client.DefaultRequestHeaders.IfModifiedSince = DateTime.Now; return(await client.GetAsync(new Uri(url))); }
/// <summary> /// Wraps the returning comments into comment object. Uses JSON.net for deserialization. /// </summary> /// <param name="site">the site url. insert without http:// prefix</param> /// <param name="listType">listType declares if site or post comments shall be returned</param> /// <param name="type">the type of comments that shall be returned</param> /// <param name="status">the status of the comments that shall be returned</param> /// <param name="post_id">the post_id of which comments shall be fetched</param> /// <param name="number">The number of comments to return. Limit: 100. Default: 20.</param> /// <param name="offset">the 0-indexed offset for the request. Default value goes to 0. Use this parameter for pagination.</param> /// <returns>List of all comments matching the query</returns> public async Task <CommentsList> GetCommentsList(string site, CommentsListType listType, CommentType type, CommentStatus status, string post_id = null, int?number = null, int?offset = null) { CommentsList comments_list = new CommentsList(); var response = await getComments(site, listType, type, status, post_id, number, offset); if (response.StatusCode == System.Net.HttpStatusCode.OK) { var responseString = await response.Content.ReadAsStringAsync(); comments_list = JsonConvert.DeserializeObject <CommentsList>(responseString); if (comments_list.comments_total_count != 0) { foreach (var item in comments_list.commentsList) { if (!item.parent.ToString().Contains("false")) { //try/catch is the only possible way to get around JsonReaderException, because parent is false not null if it has none try { JObject par = (JObject)item.parent; item.parent = new CommentParent() { parent_id = (int)par["ID"], type = (string)par["type"], link = (string)par["link"] }; } catch { item.parent = null; } } } } else { Debug.WriteLine("WARNING: GetCommentsList returned 0 results. Wrong parameters?"); } } else if (response.StatusCode != System.Net.HttpStatusCode.OK) { var responseString = await response.Content.ReadAsStringAsync(); var Error = JsonConvert.DeserializeObject <apiError>(responseString); Debug.WriteLine(string.Format("ERROR on GetCommentsList: The site returned: {0}. JetPack not installed on WordPress or JetPack JSON API not active?", Error.message)); } return(comments_list); }