Rename methods, add comments to clarify ListOptions

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-08-04 16:42:42 -04:00
parent 324c398145
commit 23d572046c
4 changed files with 51 additions and 46 deletions

View file

@ -27,7 +27,7 @@ func run() (err error) {
// Let's get the top 200 posts of r/golang.
// Reddit returns a maximum of 100 posts at a time,
// so we'll need to separate this into 2 requests.
result, _, err := client.Subreddit.Top(ctx, "golang", reddit.SetLimit(100), reddit.FromAllTime)
result, _, err := client.Subreddit.TopPosts(ctx, "golang", reddit.SetLimit(100), reddit.FromAllTime)
if err != nil {
return
}
@ -38,7 +38,7 @@ func run() (err error) {
// The SetAfter option sets the id of an item that Reddit
// will use as an anchor point for the returned listing.
result, _, err = client.Subreddit.Top(ctx, "golang", reddit.SetLimit(100), reddit.FromAllTime, reddit.SetAfter(result.After))
result, _, err = client.Subreddit.TopPosts(ctx, "golang", reddit.SetLimit(100), reddit.FromAllTime, reddit.SetAfter(result.After))
if err != nil {
return
}

View file

@ -343,16 +343,21 @@ func CheckResponse(r *http.Response) error {
return errorResponse
}
// ListOptions are the optional parameters to the various endpoints that return lists.
// ListOptions specifies the optional parameters to various API calls that return a listing.
type ListOptions struct {
// For getting submissions
// all, year, month, week, day, hour
Timespan string `url:"t,omitempty"`
// Maximum number of items to be returned.
// Generally, the default is 25 and max is 100.
Limit int `url:"limit,omitempty"`
// Common for all listing endpoints
// The full ID of an item in the listing to use
// as the anchor point of the list. Only items
// appearing after it will be returned.
After string `url:"after,omitempty"`
// The full ID of an item in the listing to use
// as the anchor point of the list. Only items
// appearing before it will be returned.
Before string `url:"before,omitempty"`
Limit int `url:"limit,omitempty"` // default: 25, max: 100
}
func addOptions(s string, opt interface{}) (string, error) {

View file

@ -63,40 +63,40 @@ func (s *SubredditService) getPosts(ctx context.Context, sort string, subreddit
return root.getPosts(), resp, nil
}
// Hot returns the hottest posts from the specified subreddit.
// HotPosts returns the hottest posts from the specified subreddit.
// To search through multiple, separate the names with a plus (+), e.g. "golang+test".
// If none are defined, it returns the ones from your subscribed subreddits.
// Note: when looking for hot posts in a subreddit, it will include the stickied
// posts (if any) PLUS posts from the limit parameter (25 by default).
func (s *SubredditService) Hot(ctx context.Context, subreddit string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
func (s *SubredditService) HotPosts(ctx context.Context, subreddit string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
return s.getPosts(ctx, "hot", subreddit, opts...)
}
// New returns the newest posts from the specified subreddit.
// NewPosts returns the newest posts from the specified subreddit.
// To search through multiple, separate the names with a plus (+), e.g. "golang+test".
// If none are defined, it returns the ones from your subscribed subreddits.
func (s *SubredditService) New(ctx context.Context, subreddit string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
func (s *SubredditService) NewPosts(ctx context.Context, subreddit string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
return s.getPosts(ctx, "new", subreddit, opts...)
}
// Rising returns the rising posts from the specified subreddit.
// RisingPosts returns the rising posts from the specified subreddit.
// To search through multiple, separate the names with a plus (+), e.g. "golang+test".
// If none are defined, it returns the ones from your subscribed subreddits.
func (s *SubredditService) Rising(ctx context.Context, subreddit string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
func (s *SubredditService) RisingPosts(ctx context.Context, subreddit string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
return s.getPosts(ctx, "rising", subreddit, opts...)
}
// Controversial returns the most controversial posts from the specified subreddit.
// ControversialPosts returns the most controversial posts from the specified subreddit.
// To search through multiple, separate the names with a plus (+), e.g. "golang+test".
// If none are defined, it returns the ones from your subscribed subreddits.
func (s *SubredditService) Controversial(ctx context.Context, subreddit string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
func (s *SubredditService) ControversialPosts(ctx context.Context, subreddit string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
return s.getPosts(ctx, "controversial", subreddit, opts...)
}
// Top returns the top posts from the specified subreddit.
// TopPosts returns the top posts from the specified subreddit.
// To search through multiple, separate the names with a plus (+), e.g. "golang+test".
// If none are defined, it returns the ones from your subscribed subreddits.
func (s *SubredditService) Top(ctx context.Context, subreddit string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
func (s *SubredditService) TopPosts(ctx context.Context, subreddit string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
return s.getPosts(ctx, "top", subreddit, opts...)
}
@ -121,39 +121,39 @@ func (s *SubredditService) Get(ctx context.Context, name string) (*Subreddit, *R
return root.Data, resp, nil
}
// GetPopular returns popular subreddits.
// todo: use search opts for this
func (s *SubredditService) GetPopular(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
// Popular returns popular subreddits.
func (s *SubredditService) Popular(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
return s.getSubreddits(ctx, "subreddits/popular", opts)
}
// GetNew returns new subreddits.
func (s *SubredditService) GetNew(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
// New returns new subreddits.
func (s *SubredditService) New(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
return s.getSubreddits(ctx, "subreddits/new", opts)
}
// GetGold returns gold subreddits.
func (s *SubredditService) GetGold(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
// Gold returns gold subreddits (i.e. only accessible to users with gold).
// It seems like it returns an empty list if you don't have gold.
func (s *SubredditService) Gold(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
return s.getSubreddits(ctx, "subreddits/gold", opts)
}
// GetDefault returns default subreddits.
func (s *SubredditService) GetDefault(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
// Default returns default subreddits.
func (s *SubredditService) Default(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
return s.getSubreddits(ctx, "subreddits/default", opts)
}
// GetSubscribed returns the list of subreddits you are subscribed to.
func (s *SubredditService) GetSubscribed(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
// Subscribed returns the list of subreddits you are subscribed to.
func (s *SubredditService) Subscribed(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
return s.getSubreddits(ctx, "subreddits/mine/subscriber", opts)
}
// GetApproved returns the list of subreddits you are an approved user in.
func (s *SubredditService) GetApproved(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
// Approved returns the list of subreddits you are an approved user in.
func (s *SubredditService) Approved(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
return s.getSubreddits(ctx, "subreddits/mine/contributor", opts)
}
// GetModerated returns the list of subreddits you are a moderator of.
func (s *SubredditService) GetModerated(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
// Moderated returns the list of subreddits you are a moderator of.
func (s *SubredditService) Moderated(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
return s.getSubreddits(ctx, "subreddits/mine/moderator", opts)
}

View file

@ -231,7 +231,7 @@ func TestSubredditService_Hot(t *testing.T) {
fmt.Fprint(w, blob)
})
posts, _, err := client.Subreddit.Hot(ctx, "test")
posts, _, err := client.Subreddit.HotPosts(ctx, "test")
assert.NoError(t, err)
assert.Equal(t, expectedPosts, posts)
}
@ -248,7 +248,7 @@ func TestSubredditService_New(t *testing.T) {
fmt.Fprint(w, blob)
})
posts, _, err := client.Subreddit.New(ctx, "test")
posts, _, err := client.Subreddit.NewPosts(ctx, "test")
assert.NoError(t, err)
assert.Equal(t, expectedPosts, posts)
}
@ -265,7 +265,7 @@ func TestSubredditService_Rising(t *testing.T) {
fmt.Fprint(w, blob)
})
posts, _, err := client.Subreddit.Rising(ctx, "test")
posts, _, err := client.Subreddit.RisingPosts(ctx, "test")
assert.NoError(t, err)
assert.Equal(t, expectedPosts, posts)
}
@ -282,7 +282,7 @@ func TestSubredditService_Controversial(t *testing.T) {
fmt.Fprint(w, blob)
})
posts, _, err := client.Subreddit.Controversial(ctx, "test")
posts, _, err := client.Subreddit.ControversialPosts(ctx, "test")
assert.NoError(t, err)
assert.Equal(t, expectedPosts, posts)
}
@ -299,7 +299,7 @@ func TestSubredditService_Top(t *testing.T) {
fmt.Fprint(w, blob)
})
posts, _, err := client.Subreddit.Top(ctx, "test")
posts, _, err := client.Subreddit.TopPosts(ctx, "test")
assert.NoError(t, err)
assert.Equal(t, expectedPosts, posts)
}
@ -336,7 +336,7 @@ func TestSubredditService_GetPopular(t *testing.T) {
fmt.Fprint(w, blob)
})
subreddits, _, err := client.Subreddit.GetPopular(ctx, nil)
subreddits, _, err := client.Subreddit.Popular(ctx, nil)
assert.NoError(t, err)
assert.Equal(t, expectedSubreddits, subreddits)
}
@ -353,7 +353,7 @@ func TestSubredditService_GetNew(t *testing.T) {
fmt.Fprint(w, blob)
})
subreddits, _, err := client.Subreddit.GetNew(ctx, nil)
subreddits, _, err := client.Subreddit.New(ctx, nil)
assert.NoError(t, err)
assert.Equal(t, expectedSubreddits, subreddits)
}
@ -370,7 +370,7 @@ func TestSubredditService_GetGold(t *testing.T) {
fmt.Fprint(w, blob)
})
subreddits, _, err := client.Subreddit.GetGold(ctx, nil)
subreddits, _, err := client.Subreddit.Gold(ctx, nil)
assert.NoError(t, err)
assert.Equal(t, expectedSubreddits, subreddits)
}
@ -387,7 +387,7 @@ func TestSubredditService_GetDefault(t *testing.T) {
fmt.Fprint(w, blob)
})
subreddits, _, err := client.Subreddit.GetDefault(ctx, nil)
subreddits, _, err := client.Subreddit.Default(ctx, nil)
assert.NoError(t, err)
assert.Equal(t, expectedSubreddits, subreddits)
}
@ -404,7 +404,7 @@ func TestSubredditService_GetSubscribed(t *testing.T) {
fmt.Fprint(w, blob)
})
subreddits, _, err := client.Subreddit.GetSubscribed(ctx, nil)
subreddits, _, err := client.Subreddit.Subscribed(ctx, nil)
assert.NoError(t, err)
assert.Equal(t, expectedSubreddits, subreddits)
}
@ -421,7 +421,7 @@ func TestSubredditService_GetApproved(t *testing.T) {
fmt.Fprint(w, blob)
})
subreddits, _, err := client.Subreddit.GetApproved(ctx, nil)
subreddits, _, err := client.Subreddit.Approved(ctx, nil)
assert.NoError(t, err)
assert.Equal(t, expectedSubreddits, subreddits)
}
@ -438,7 +438,7 @@ func TestSubredditService_GetModerated(t *testing.T) {
fmt.Fprint(w, blob)
})
subreddits, _, err := client.Subreddit.GetModerated(ctx, nil)
subreddits, _, err := client.Subreddit.Moderated(ctx, nil)
assert.NoError(t, err)
assert.Equal(t, expectedSubreddits, subreddits)
}