コード例 #1
0
        /// <summary>
        /// Return a listing of things specified by their fullnames.
        /// Only Links, Comments, and Subreddits are allowed.
        /// </summary>
        /// <param name="id">A comma-separated list of thing fullnames</param>
        /// <param name="subreddit">The subreddit with the listing.</param>
        /// <returns>The requested listings.</returns>
        public Info Info(string id, string subreddit = null)
        {
            RestRequest restRequest = PrepareRequest(Sr(subreddit) + "api/info");

            restRequest.AddParameter("id", id);

            DynamicListingContainer res = JsonConvert.DeserializeObject <DynamicListingContainer>(ExecuteRequest(restRequest));

            List <Post>      posts      = new List <Post>();
            List <Comment>   comments   = new List <Comment>();
            List <Subreddit> subreddits = new List <Subreddit>();

            foreach (DynamicListingChild child in res.Data.Children)
            {
                switch (child.Kind)
                {
                case "t3":
                    posts.Add(JsonConvert.DeserializeObject <Post>(JsonConvert.SerializeObject(child.Data)));
                    break;

                case "t1":
                    comments.Add(JsonConvert.DeserializeObject <Comment>(JsonConvert.SerializeObject(child.Data)));
                    break;

                case "t5":
                    subreddits.Add(JsonConvert.DeserializeObject <Subreddit>(JsonConvert.SerializeObject(child.Data)));
                    break;
                }
            }

            return(new Info(posts, comments, subreddits));
        }
コード例 #2
0
        // Regarding loadReplies:  https://www.reddit.com/r/redditdev/comments/dqz2jy/api_bug_the_apiinfo_endpoint_does_not_populate/
        /// <summary>
        /// Return a listing of things specified by their fullnames.
        /// Only Links, Comments, and Subreddits are allowed.
        /// </summary>
        /// <param name="id">A comma-separated list of thing fullnames</param>
        /// <param name="subreddit">The subreddit with the listing</param>
        /// <param name="loadReplies">The Info endpoint doesn't include replies; if loadReplies is true, an additional API call will be triggered to retrieve the replies (default: true)</param>
        /// <returns>The requested listings.</returns>
        public Info Info(string id = null, string subreddit = null, bool loadReplies = true, string url = null)
        {
            RestRequest restRequest = PrepareRequest(Sr(subreddit) + "api/info");

            if (id != null)
            {
                restRequest.AddParameter("id", id);
            }
            if (url != null)
            {
                restRequest.AddParameter("url", url);
            }

            DynamicListingContainer res = JsonConvert.DeserializeObject <DynamicListingContainer>(ExecuteRequest(restRequest));

            List <Post>      posts      = new List <Post>();
            List <Comment>   comments   = new List <Comment>();
            List <Subreddit> subreddits = new List <Subreddit>();

            foreach (DynamicListingChild child in res.Data.Children)
            {
                switch (child.Kind)
                {
                case "t3":
                    posts.Add(JsonConvert.DeserializeObject <Post>(JsonConvert.SerializeObject(child.Data)));
                    break;

                case "t1":
                    Comment comment = JsonConvert.DeserializeObject <Comment>(JsonConvert.SerializeObject(child.Data));
                    if (loadReplies)
                    {
                        CommentContainer commentContainer = Common.GetComments(comment.ParentId.Substring(3), new ListingsGetCommentsInput(comment: comment.Id), comment.Subreddit);
                        if (commentContainer != null &&
                            commentContainer.Data != null &&
                            commentContainer.Data.Children != null &&
                            !commentContainer.Data.Children.Count.Equals(0) &&
                            commentContainer.Data.Children[0].Data != null)
                        {
                            comment.Replies = commentContainer.Data.Children[0].Data.Replies;
                        }
                    }

                    comments.Add(comment);
                    break;

                case "t5":
                    subreddits.Add(JsonConvert.DeserializeObject <Subreddit>(JsonConvert.SerializeObject(child.Data)));
                    break;
                }
            }

            return(new Info(posts, comments, subreddits));
        }