Add by_id endpoint, tweak UserService methods
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
+34
-13
@@ -2,13 +2,17 @@ package geddit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ListingsService handles communication with the link (post)
|
||||
// related methods of the Reddit API
|
||||
type ListingsService interface {
|
||||
Get(ctx context.Context, ids ...string) (*CommentsLinksSubreddits, *Response, error)
|
||||
Get(ctx context.Context, ids ...string) ([]Comment, []Link, []Subreddit, *Response, error)
|
||||
GetLinks(ctx context.Context, ids ...string) ([]Link, *Response, error)
|
||||
}
|
||||
|
||||
// ListingsServiceOp implements the Vote interface
|
||||
@@ -18,9 +22,8 @@ type ListingsServiceOp struct {
|
||||
|
||||
var _ ListingsService = &ListingsServiceOp{}
|
||||
|
||||
// Get gets a list of things based on their IDs
|
||||
// Only links, comments, and subreddits are allowed
|
||||
func (s *ListingsServiceOp) Get(ctx context.Context, ids ...string) (*CommentsLinksSubreddits, *Response, error) {
|
||||
// Get returns comments, link, and subreddits from their IDs
|
||||
func (s *ListingsServiceOp) Get(ctx context.Context, ids ...string) ([]Comment, []Link, []Subreddit, *Response, error) {
|
||||
type query struct {
|
||||
IDs []string `url:"id,comma"`
|
||||
}
|
||||
@@ -28,9 +31,34 @@ func (s *ListingsServiceOp) Get(ctx context.Context, ids ...string) (*CommentsLi
|
||||
path := "api/info"
|
||||
path, err := addOptions(path, query{ids})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, nil, nil, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
||||
if err != nil {
|
||||
return nil, nil, nil, nil, err
|
||||
}
|
||||
|
||||
root := new(rootListing)
|
||||
resp, err := s.client.Do(ctx, req, root)
|
||||
if err != nil {
|
||||
return nil, nil, nil, resp, err
|
||||
}
|
||||
|
||||
comments := root.getComments().Comments
|
||||
links := root.getLinks().Links
|
||||
subreddits := root.getSubreddits().Subreddits
|
||||
|
||||
return comments, links, subreddits, resp, nil
|
||||
}
|
||||
|
||||
// GetLinks returns links from their IDs
|
||||
func (s *ListingsServiceOp) GetLinks(ctx context.Context, ids ...string) ([]Link, *Response, error) {
|
||||
if len(ids) == 0 {
|
||||
return nil, nil, errors.New("must provide at least 1 id")
|
||||
}
|
||||
|
||||
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
|
||||
@@ -42,12 +70,5 @@ func (s *ListingsServiceOp) Get(ctx context.Context, ids ...string) (*CommentsLi
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
v := new(CommentsLinksSubreddits)
|
||||
v.Comments = root.getComments().Comments
|
||||
v.Links = root.getLinks().Links
|
||||
v.Subreddits = root.getSubreddits().Subreddits
|
||||
|
||||
return v, resp, nil
|
||||
return root.getLinks().Links, resp, nil
|
||||
}
|
||||
|
||||
// todo: do by_id next
|
||||
|
||||
Reference in New Issue
Block a user