2020-07-11 13:49:07 -04:00
|
|
|
package reddit
|
2020-04-29 15:59:18 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-03 20:24:22 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-04-29 15:59:18 -04:00
|
|
|
)
|
|
|
|
|
2020-06-27 23:53:59 -04:00
|
|
|
// ListingsService handles communication with the listing
|
2020-06-22 21:52:34 -04:00
|
|
|
// related methods of the Reddit API.
|
2020-06-27 23:53:59 -04:00
|
|
|
//
|
|
|
|
// Reddit API docs: https://www.reddit.com/dev/api/#section_listings
|
2020-07-21 23:05:24 -04:00
|
|
|
type ListingsService struct {
|
|
|
|
client *Client
|
|
|
|
}
|
2020-04-29 15:59:18 -04:00
|
|
|
|
2020-08-27 18:49:30 -04:00
|
|
|
// Get posts, comments, and subreddits from their full IDs.
|
2020-07-19 22:03:37 -04:00
|
|
|
func (s *ListingsService) Get(ctx context.Context, ids ...string) ([]*Post, []*Comment, []*Subreddit, *Response, error) {
|
2020-09-07 21:24:14 -04:00
|
|
|
path := "api/info"
|
|
|
|
params := struct {
|
|
|
|
IDs []string `url:"id,omitempty,comma"`
|
|
|
|
}{ids}
|
2020-04-29 15:59:18 -04:00
|
|
|
|
2020-09-07 21:24:14 -04:00
|
|
|
l, resp, err := s.client.getListing(ctx, path, params)
|
2020-04-29 15:59:18 -04:00
|
|
|
if err != nil {
|
2020-05-03 20:24:22 -04:00
|
|
|
return nil, nil, nil, resp, err
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-09-07 21:24:14 -04:00
|
|
|
return l.Posts(), l.Comments(), l.Subreddits(), resp, nil
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:52:34 -04:00
|
|
|
// GetPosts returns posts from their full IDs.
|
2020-07-19 22:03:37 -04:00
|
|
|
func (s *ListingsService) GetPosts(ctx context.Context, ids ...string) ([]*Post, *Response, error) {
|
2020-05-03 20:24:22 -04:00
|
|
|
path := fmt.Sprintf("by_id/%s", strings.Join(ids, ","))
|
2020-09-07 21:24:14 -04:00
|
|
|
l, resp, err := s.client.getListing(ctx, path, nil)
|
2020-05-03 20:24:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2020-09-07 21:24:14 -04:00
|
|
|
return l.Posts(), resp, nil
|
2020-05-03 20:24:22 -04:00
|
|
|
}
|