Add methods to get random posts and subreddits

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian
2020-07-20 21:03:57 -04:00
parent 5c376a1af4
commit 465e96353c
4 changed files with 104 additions and 1 deletions
+32
View File
@@ -501,3 +501,35 @@ func addCommentToReplies(parent *Comment, comment *Comment) {
addCommentToReplies(reply, comment)
}
}
// RandomFromSubreddits returns a random post and its comments from the subreddits.
// If no subreddits are provided, it will run the query against your subscriptions.
func (s *PostService) RandomFromSubreddits(ctx context.Context, subreddits ...string) (*Post, []*Comment, *Response, error) {
path := "random"
if len(subreddits) > 0 {
path = fmt.Sprintf("r/%s/random", strings.Join(subreddits, "+"))
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, nil, err
}
root := new(postAndComments)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, nil, resp, err
}
return root.Post, root.Comments, resp, nil
}
// Random returns a random post and its comments from all of Reddit.
func (s *PostService) Random(ctx context.Context) (*Post, []*Comment, *Response, error) {
return s.RandomFromSubreddits(ctx, "all")
}
// RandomFromSubscriptions returns a random post and its comments from your subscriptions.
func (s *PostService) RandomFromSubscriptions(ctx context.Context) (*Post, []*Comment, *Response, error) {
return s.RandomFromSubreddits(ctx)
}