2a1806ec33
Now, instead of returning an object containing a list of results + the anchors, we return just the list. The anchors are available in the response object. Much cleaner this way in my opinion go-github and godo do it this way too. They include some meta information in the returned response objects Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
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
|
|
}
|