Add by_id endpoint, tweak UserService methods

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian
2020-05-03 20:24:22 -04:00
parent c897c14b23
commit ff75539620
3 changed files with 104 additions and 97 deletions
+34 -13
View File
@@ -2,13 +2,17 @@ package geddit
import ( import (
"context" "context"
"errors"
"fmt"
"net/http" "net/http"
"strings"
) )
// ListingsService handles communication with the link (post) // ListingsService handles communication with the link (post)
// related methods of the Reddit API // related methods of the Reddit API
type ListingsService interface { 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 // ListingsServiceOp implements the Vote interface
@@ -18,9 +22,8 @@ type ListingsServiceOp struct {
var _ ListingsService = &ListingsServiceOp{} var _ ListingsService = &ListingsServiceOp{}
// Get gets a list of things based on their IDs // Get returns comments, link, and subreddits from their IDs
// Only links, comments, and subreddits are allowed func (s *ListingsServiceOp) Get(ctx context.Context, ids ...string) ([]Comment, []Link, []Subreddit, *Response, error) {
func (s *ListingsServiceOp) Get(ctx context.Context, ids ...string) (*CommentsLinksSubreddits, *Response, error) {
type query struct { type query struct {
IDs []string `url:"id,comma"` IDs []string `url:"id,comma"`
} }
@@ -28,9 +31,34 @@ func (s *ListingsServiceOp) Get(ctx context.Context, ids ...string) (*CommentsLi
path := "api/info" path := "api/info"
path, err := addOptions(path, query{ids}) path, err := addOptions(path, query{ids})
if err != nil { 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) req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@@ -42,12 +70,5 @@ func (s *ListingsServiceOp) Get(ctx context.Context, ids ...string) (*CommentsLi
return nil, resp, err return nil, resp, err
} }
v := new(CommentsLinksSubreddits) return root.getLinks().Links, resp, nil
v.Comments = root.getComments().Comments
v.Links = root.getLinks().Links
v.Subreddits = root.getSubreddits().Subreddits
return v, resp, nil
} }
// todo: do by_id next
+18 -21
View File
@@ -202,36 +202,33 @@ func (rl *rootListing) getBefore() string {
} }
func (rl *rootListing) getComments() *Comments { func (rl *rootListing) getComments() *Comments {
if rl == nil || rl.Data == nil { v := new(Comments)
return nil if rl != nil && rl.Data != nil {
} v.Comments = rl.Data.Things.Comments
return &Comments{ v.After = rl.Data.After
Comments: rl.Data.Things.Comments, v.Before = rl.Data.Before
After: rl.Data.After,
Before: rl.Data.Before,
} }
return v
} }
func (rl *rootListing) getLinks() *Links { func (rl *rootListing) getLinks() *Links {
if rl == nil || rl.Data == nil { v := new(Links)
return nil if rl != nil && rl.Data != nil {
} v.Links = rl.Data.Things.Links
return &Links{ v.After = rl.Data.After
Links: rl.Data.Things.Links, v.Before = rl.Data.Before
After: rl.Data.After,
Before: rl.Data.Before,
} }
return v
} }
func (rl *rootListing) getSubreddits() *Subreddits { func (rl *rootListing) getSubreddits() *Subreddits {
if rl == nil || rl.Data == nil { v := new(Subreddits)
return nil if rl != nil && rl.Data != nil {
} v.Subreddits = rl.Data.Things.Subreddits
return &Subreddits{ v.After = rl.Data.After
Subreddits: rl.Data.Things.Subreddits, v.Before = rl.Data.Before
After: rl.Data.After,
Before: rl.Data.Before,
} }
return v
} }
// Comments is a list of comments // Comments is a list of comments
+52 -63
View File
@@ -164,32 +164,33 @@ func (s *UserServiceOp) UsernameAvailable(ctx context.Context, username string)
// Overview returns a list of the client's comments and links // Overview returns a list of the client's comments and links
func (s *UserServiceOp) Overview(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error) { func (s *UserServiceOp) Overview(ctx context.Context, opts *ListOptions) (*CommentsLinks, *Response, error) {
path := fmt.Sprintf("user/%s/overview", s.client.Username) return s.OverviewOf(ctx, s.client.Username, opts)
}
// OverviewOf returns a list of the user's comments and links
func (s *UserServiceOp) OverviewOf(ctx context.Context, username string, opts *ListOptions) (*CommentsLinks, *Response, error) {
path := fmt.Sprintf("user/%s/overview", username)
return s.getCommentsAndLinks(ctx, path, opts) return s.getCommentsAndLinks(ctx, path, opts)
} }
// GetHotLinks returns a list of the client's hottest submissions // GetHotLinks returns a list of the client's hottest submissions
func (s *UserServiceOp) GetHotLinks(ctx context.Context, opts *ListOptions) (*Links, *Response, error) { func (s *UserServiceOp) GetHotLinks(ctx context.Context, opts *ListOptions) (*Links, *Response, error) {
path := fmt.Sprintf("user/%s/submitted?sort=%s", s.client.Username, sorts[sortHot]) return s.GetHotLinksOf(ctx, s.client.Username, opts)
return s.getLinks(ctx, path, opts)
} }
// GetNewLinks returns a list of the client's newest submissions // GetNewLinks returns a list of the client's newest submissions
func (s *UserServiceOp) GetNewLinks(ctx context.Context, opts *ListOptions) (*Links, *Response, error) { func (s *UserServiceOp) GetNewLinks(ctx context.Context, opts *ListOptions) (*Links, *Response, error) {
path := fmt.Sprintf("user/%s/submitted?sort=%s", s.client.Username, sorts[sortNew]) return s.GetNewLinksOf(ctx, s.client.Username, opts)
return s.getLinks(ctx, path, opts)
} }
// GetTopLinks returns a list of the client's top submissions // GetTopLinks returns a list of the client's top submissions
func (s *UserServiceOp) GetTopLinks(ctx context.Context, opts *ListOptions) (*Links, *Response, error) { func (s *UserServiceOp) GetTopLinks(ctx context.Context, opts *ListOptions) (*Links, *Response, error) {
path := fmt.Sprintf("user/%s/submitted?sort=%s", s.client.Username, sorts[sortTop]) return s.GetTopLinksOf(ctx, s.client.Username, opts)
return s.getLinks(ctx, path, opts)
} }
// GetControversialLinks returns a list of the client's most controversial submissions // GetControversialLinks returns a list of the client's most controversial submissions
func (s *UserServiceOp) GetControversialLinks(ctx context.Context, opts *ListOptions) (*Links, *Response, error) { func (s *UserServiceOp) GetControversialLinks(ctx context.Context, opts *ListOptions) (*Links, *Response, error) {
path := fmt.Sprintf("user/%s/submitted?sort=%s", s.client.Username, sorts[sortControversial]) return s.GetControversialLinksOf(ctx, s.client.Username, opts)
return s.getLinks(ctx, path, opts)
} }
// GetHotLinksOf returns a list of the user's hottest submissions // GetHotLinksOf returns a list of the user's hottest submissions
@@ -248,42 +249,46 @@ func (s *UserServiceOp) GetGilded(ctx context.Context, opts *ListOptions) (*Comm
// GetHotComments returns a list of the client's hottest comments // GetHotComments returns a list of the client's hottest comments
func (s *UserServiceOp) GetHotComments(ctx context.Context, opts *ListOptions) (*Comments, *Response, error) { func (s *UserServiceOp) GetHotComments(ctx context.Context, opts *ListOptions) (*Comments, *Response, error) {
return s.getComments(ctx, s.client.Username, sortHot, opts) return s.GetHotCommentsOf(ctx, s.client.Username, opts)
} }
// GetNewComments returns a list of the client's newest comments // GetNewComments returns a list of the client's newest comments
func (s *UserServiceOp) GetNewComments(ctx context.Context, opts *ListOptions) (*Comments, *Response, error) { func (s *UserServiceOp) GetNewComments(ctx context.Context, opts *ListOptions) (*Comments, *Response, error) {
return s.getComments(ctx, s.client.Username, sortNew, opts) return s.GetNewCommentsOf(ctx, s.client.Username, opts)
} }
// GetTopComments returns a list of the client's top comments // GetTopComments returns a list of the client's top comments
func (s *UserServiceOp) GetTopComments(ctx context.Context, opts *ListOptions) (*Comments, *Response, error) { func (s *UserServiceOp) GetTopComments(ctx context.Context, opts *ListOptions) (*Comments, *Response, error) {
return s.getComments(ctx, s.client.Username, sortTop, opts) return s.GetTopCommentsOf(ctx, s.client.Username, opts)
} }
// GetControversialComments returns a list of the client's most controversial comments // GetControversialComments returns a list of the client's most controversial comments
func (s *UserServiceOp) GetControversialComments(ctx context.Context, opts *ListOptions) (*Comments, *Response, error) { func (s *UserServiceOp) GetControversialComments(ctx context.Context, opts *ListOptions) (*Comments, *Response, error) {
return s.getComments(ctx, s.client.Username, sortControversial, opts) return s.GetControversialCommentsOf(ctx, s.client.Username, opts)
} }
// GetHotCommentsOf returns a list of the user's hottest comments // GetHotCommentsOf returns a list of the user's hottest comments
func (s *UserServiceOp) GetHotCommentsOf(ctx context.Context, username string, opts *ListOptions) (*Comments, *Response, error) { func (s *UserServiceOp) GetHotCommentsOf(ctx context.Context, username string, opts *ListOptions) (*Comments, *Response, error) {
return s.getComments(ctx, username, sortHot, opts) path := fmt.Sprintf("user/%s/comments?sort=%s", username, sorts[sortHot])
return s.getComments(ctx, path, opts)
} }
// GetNewCommentsOf returns a list of the user's newest comments // GetNewCommentsOf returns a list of the user's newest comments
func (s *UserServiceOp) GetNewCommentsOf(ctx context.Context, username string, opts *ListOptions) (*Comments, *Response, error) { func (s *UserServiceOp) GetNewCommentsOf(ctx context.Context, username string, opts *ListOptions) (*Comments, *Response, error) {
return s.getComments(ctx, username, sortNew, opts) path := fmt.Sprintf("user/%s/comments?sort=%s", username, sorts[sortNew])
return s.getComments(ctx, path, opts)
} }
// GetTopCommentsOf returns a list of the user's top comments // GetTopCommentsOf returns a list of the user's top comments
func (s *UserServiceOp) GetTopCommentsOf(ctx context.Context, username string, opts *ListOptions) (*Comments, *Response, error) { func (s *UserServiceOp) GetTopCommentsOf(ctx context.Context, username string, opts *ListOptions) (*Comments, *Response, error) {
return s.getComments(ctx, username, sortTop, opts) path := fmt.Sprintf("user/%s/comments?sort=%s", username, sorts[sortTop])
return s.getComments(ctx, path, opts)
} }
// GetControversialCommentsOf returns a list of the user's most controversial comments // GetControversialCommentsOf returns a list of the user's most controversial comments
func (s *UserServiceOp) GetControversialCommentsOf(ctx context.Context, username string, opts *ListOptions) (*Comments, *Response, error) { func (s *UserServiceOp) GetControversialCommentsOf(ctx context.Context, username string, opts *ListOptions) (*Comments, *Response, error) {
return s.getComments(ctx, username, sortControversial, opts) path := fmt.Sprintf("user/%s/comments?sort=%s", username, sorts[sortControversial])
return s.getComments(ctx, path, opts)
} }
// Friend creates or updates a "friend" relationship // Friend creates or updates a "friend" relationship
@@ -337,68 +342,52 @@ func (s *UserServiceOp) Unfriend(ctx context.Context, username string) (*Respons
} }
func (s *UserServiceOp) getLinks(ctx context.Context, path string, opts *ListOptions) (*Links, *Response, error) { func (s *UserServiceOp) getLinks(ctx context.Context, path string, opts *ListOptions) (*Links, *Response, error) {
path, err := addOptions(path, opts) listing, resp, err := s.getListing(ctx, path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(rootListing)
resp, err := s.client.Do(ctx, req, root)
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err
} }
return listing.getLinks(), resp, nil
return root.getLinks(), resp, nil
} }
func (s *UserServiceOp) getComments(ctx context.Context, username string, sort sort, opts *ListOptions) (*Comments, *Response, error) { func (s *UserServiceOp) getComments(ctx context.Context, path string, opts *ListOptions) (*Comments, *Response, error) {
path := fmt.Sprintf("user/%s/comments?sort=%s", username, sorts[sort]) listing, resp, err := s.getListing(ctx, path, opts)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(rootListing)
resp, err := s.client.Do(ctx, req, root)
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err
} }
return listing.getComments(), resp, nil
return root.getComments(), resp, nil
} }
func (s *UserServiceOp) getCommentsAndLinks(ctx context.Context, path string, opts *ListOptions) (*CommentsLinks, *Response, error) { func (s *UserServiceOp) getCommentsAndLinks(ctx context.Context, path string, opts *ListOptions) (*CommentsLinks, *Response, error) {
path, err := addOptions(path, opts) listing, resp, err := s.getListing(ctx, path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(rootListing)
resp, err := s.client.Do(ctx, req, root)
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err
} }
v := new(CommentsLinks) v := new(CommentsLinks)
v.Comments = root.getComments().Comments v.Comments = listing.getComments().Comments
v.Links = root.getLinks().Links v.Links = listing.getLinks().Links
v.After = root.getAfter() v.After = listing.getAfter()
v.Before = root.getBefore() v.Before = listing.getBefore()
return v, resp, nil return v, resp, nil
} }
func (s *UserServiceOp) getListing(ctx context.Context, path string, opts *ListOptions) (*rootListing, *Response, error) {
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(rootListing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root, resp, err
}