snoobert/reddit/listings.go

53 lines
1.3 KiB
Go
Raw Normal View History

package reddit
import (
"context"
"fmt"
"net/http"
"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 := fmt.Sprintf("api/info?id=%s", strings.Join(ids, ","))
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, nil, nil, err
}
root := new(listing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, nil, nil, resp, err
}
return root.Posts, root.Comments, root.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, ","))
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(listing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Posts, resp, nil
}