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

View file

@ -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

View file

@ -202,36 +202,33 @@ func (rl *rootListing) getBefore() string {
}
func (rl *rootListing) getComments() *Comments {
if rl == nil || rl.Data == nil {
return nil
}
return &Comments{
Comments: rl.Data.Things.Comments,
After: rl.Data.After,
Before: rl.Data.Before,
v := new(Comments)
if rl != nil && rl.Data != nil {
v.Comments = rl.Data.Things.Comments
v.After = rl.Data.After
v.Before = rl.Data.Before
}
return v
}
func (rl *rootListing) getLinks() *Links {
if rl == nil || rl.Data == nil {
return nil
}
return &Links{
Links: rl.Data.Things.Links,
After: rl.Data.After,
Before: rl.Data.Before,
v := new(Links)
if rl != nil && rl.Data != nil {
v.Links = rl.Data.Things.Links
v.After = rl.Data.After
v.Before = rl.Data.Before
}
return v
}
func (rl *rootListing) getSubreddits() *Subreddits {
if rl == nil || rl.Data == nil {
return nil
}
return &Subreddits{
Subreddits: rl.Data.Things.Subreddits,
After: rl.Data.After,
Before: rl.Data.Before,
v := new(Subreddits)
if rl != nil && rl.Data != nil {
v.Subreddits = rl.Data.Things.Subreddits
v.After = rl.Data.After
v.Before = rl.Data.Before
}
return v
}
// Comments is a list of comments

115
user.go
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
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)
}
// GetHotLinks returns a list of the client's hottest submissions
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.getLinks(ctx, path, opts)
return s.GetHotLinksOf(ctx, s.client.Username, opts)
}
// GetNewLinks returns a list of the client's newest submissions
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.getLinks(ctx, path, opts)
return s.GetNewLinksOf(ctx, s.client.Username, opts)
}
// GetTopLinks returns a list of the client's top submissions
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.getLinks(ctx, path, opts)
return s.GetTopLinksOf(ctx, s.client.Username, opts)
}
// GetControversialLinks returns a list of the client's most controversial submissions
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.getLinks(ctx, path, opts)
return s.GetControversialLinksOf(ctx, s.client.Username, opts)
}
// 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
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
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
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
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
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
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
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
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
@ -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) {
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)
listing, resp, err := s.getListing(ctx, path, opts)
if err != nil {
return nil, resp, err
}
return root.getLinks(), resp, nil
return listing.getLinks(), resp, nil
}
func (s *UserServiceOp) getComments(ctx context.Context, username string, sort sort, opts *ListOptions) (*Comments, *Response, error) {
path := fmt.Sprintf("user/%s/comments?sort=%s", username, sorts[sort])
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)
func (s *UserServiceOp) getComments(ctx context.Context, path string, opts *ListOptions) (*Comments, *Response, error) {
listing, resp, err := s.getListing(ctx, path, opts)
if err != nil {
return nil, resp, err
}
return root.getComments(), resp, nil
return listing.getComments(), resp, nil
}
func (s *UserServiceOp) getCommentsAndLinks(ctx context.Context, path string, opts *ListOptions) (*CommentsLinks, *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)
listing, resp, err := s.getListing(ctx, path, opts)
if err != nil {
return nil, resp, err
}
v := new(CommentsLinks)
v.Comments = root.getComments().Comments
v.Links = root.getLinks().Links
v.After = root.getAfter()
v.Before = root.getBefore()
v.Comments = listing.getComments().Comments
v.Links = listing.getLinks().Links
v.After = listing.getAfter()
v.Before = listing.getBefore()
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
}