forked from Sadzeih/go-reddit
/
listings.go
40 lines (34 loc) · 1.03 KB
/
listings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package reddit
import (
"context"
"fmt"
"strings"
)
// ListingsService handles communication with the listing
// related methods of the Reddit API.
//
// Reddit API docs: https://www.reddit.com/dev/api/#section_listings
type ListingsService struct {
client *Client
}
// Get posts, comments, and subreddits from their full IDs.
func (s *ListingsService) Get(ctx context.Context, ids ...string) ([]*Post, []*Comment, []*Subreddit, *Response, error) {
path := "api/info"
params := struct {
IDs []string `url:"id,omitempty,comma"`
}{ids}
l, resp, err := s.client.getListing(ctx, path, params)
if err != nil {
return nil, nil, nil, resp, err
}
return l.Posts(), l.Comments(), l.Subreddits(), resp, nil
}
// GetPosts returns posts from their full IDs.
func (s *ListingsService) GetPosts(ctx context.Context, ids ...string) ([]*Post, *Response, error) {
path := fmt.Sprintf("by_id/%s", strings.Join(ids, ","))
l, resp, err := s.client.getListing(ctx, path, nil)
if err != nil {
return nil, resp, err
}
return l.Posts(), resp, nil
}