From 2ea893ab169230ea5149606a5c3b9521c1d39095 Mon Sep 17 00:00:00 2001 From: Vartan Benohanian Date: Tue, 21 Jul 2020 23:05:24 -0400 Subject: [PATCH] Use individual structs for services Made it this way so that I can use the PostAndCommentService as a child of the PostService and CommentService. Signed-off-by: Vartan Benohanian --- account.go | 4 +- comment.go | 5 +- comment_test.go | 208 +++++++++++++++++++++++++++++++++++++ flair.go | 4 +- listings.go | 4 +- moderation.go | 4 +- multi.go | 4 +- post-and-comment.go | 4 +- post-and-comment_test.go | 218 --------------------------------------- post.go | 5 +- post_test.go | 208 +++++++++++++++++++++++++++++++++++++ reddit.go | 57 +++++----- reddit_test.go | 1 - search.go | 4 +- subreddit.go | 4 +- user.go | 6 +- 16 files changed, 476 insertions(+), 264 deletions(-) delete mode 100644 post-and-comment_test.go diff --git a/account.go b/account.go index 1b765db..44fd1ac 100644 --- a/account.go +++ b/account.go @@ -10,7 +10,9 @@ import ( // related methods of the Reddit API. // // Reddit API docs: https://www.reddit.com/dev/api/#section_account -type AccountService service +type AccountService struct { + client *Client +} type rootSubredditKarma struct { Kind string `json:"kind,omitempty"` diff --git a/comment.go b/comment.go index b61c4a3..52edd3a 100644 --- a/comment.go +++ b/comment.go @@ -10,7 +10,10 @@ import ( // related methods of the Reddit API. // // Reddit API docs: https://www.reddit.com/dev/api/#section_links_and_comments -type CommentService service +type CommentService struct { + *PostAndCommentService + client *Client +} // Submit submits a comment as a reply to a post, comment, or message. // parentID is the full ID of the thing being replied to. diff --git a/comment_test.go b/comment_test.go index a72e4f1..e884080 100644 --- a/comment_test.go +++ b/comment_test.go @@ -93,3 +93,211 @@ func TestCommentService_Edit(t *testing.T) { assert.NoError(t, err) assert.Equal(t, expectedCommentSubmitOrEdit, comment) } + +func TestCommentService_Delete(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/del", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t1_test") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Comment.Delete(ctx, "t1_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestCommentService_Save(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/save", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t1_test") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Comment.Save(ctx, "t1_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestCommentService_Unsave(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/unsave", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t1_test") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Comment.Unsave(ctx, "t1_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestCommentService_EnableReplies(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/sendreplies", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t1_test") + form.Set("state", "true") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Comment.EnableReplies(ctx, "t1_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestCommentService_DisableReplies(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/sendreplies", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t1_test") + form.Set("state", "false") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Comment.DisableReplies(ctx, "t1_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestCommentService_Lock(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/lock", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t1_test") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Comment.Lock(ctx, "t1_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestCommentService_Unlock(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/unlock", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t1_test") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Comment.Unlock(ctx, "t1_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestCommentService_Upvote(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/vote", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t1_test") + form.Set("dir", "1") + form.Set("rank", "10") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Comment.Upvote(ctx, "t1_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestCommentService_Downvote(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/vote", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t1_test") + form.Set("dir", "-1") + form.Set("rank", "10") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Comment.Downvote(ctx, "t1_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestCommentService_RemoveVote(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/vote", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t1_test") + form.Set("dir", "0") + form.Set("rank", "10") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Comment.RemoveVote(ctx, "t1_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} diff --git a/flair.go b/flair.go index 4643864..186d110 100644 --- a/flair.go +++ b/flair.go @@ -10,7 +10,9 @@ import ( // related methods of the Reddit API. // // Reddit API docs: https://www.reddit.com/dev/api/#section_flair -type FlairService service +type FlairService struct { + client *Client +} // Flair is a flair on Reddit type Flair struct { diff --git a/listings.go b/listings.go index b2db142..09b1cd6 100644 --- a/listings.go +++ b/listings.go @@ -12,7 +12,9 @@ import ( // related methods of the Reddit API. // // Reddit API docs: https://www.reddit.com/dev/api/#section_listings -type ListingsService service +type ListingsService struct { + client *Client +} // Get returns posts, comments, and subreddits from their IDs. func (s *ListingsService) Get(ctx context.Context, ids ...string) ([]*Post, []*Comment, []*Subreddit, *Response, error) { diff --git a/moderation.go b/moderation.go index 16dcaeb..74e2c70 100644 --- a/moderation.go +++ b/moderation.go @@ -11,7 +11,9 @@ import ( // related methods of the Reddit API. // // Reddit API docs: https://www.reddit.com/dev/api/#section_moderation -type ModerationService service +type ModerationService struct { + client *Client +} // ModAction is an action executed by a moderator of a subreddit, such // as inviting another user to be a mod, or setting permissions. diff --git a/multi.go b/multi.go index bd5f85a..02b1233 100644 --- a/multi.go +++ b/multi.go @@ -13,7 +13,9 @@ import ( // related methods of the Reddit API. // // Reddit API docs: https://www.reddit.com/dev/api#section_multis -type MultiService service +type MultiService struct { + client *Client +} type multiRoot struct { Kind string `json:"kind,omitempty"` diff --git a/post-and-comment.go b/post-and-comment.go index 0dfa227..71b1ac0 100644 --- a/post-and-comment.go +++ b/post-and-comment.go @@ -14,7 +14,9 @@ import ( // Reddit API docs: https://www.reddit.com/dev/api/#section_links_and_comments // // todo: this is ugly, find a solution -type PostAndCommentService service +type PostAndCommentService struct { + client *Client +} type vote int diff --git a/post-and-comment_test.go b/post-and-comment_test.go deleted file mode 100644 index 614f86a..0000000 --- a/post-and-comment_test.go +++ /dev/null @@ -1,218 +0,0 @@ -package reddit - -import ( - "fmt" - "net/http" - "net/url" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestPostAndCommentService_Delete(t *testing.T) { - setup() - defer teardown() - - mux.HandleFunc("/api/del", func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, http.MethodPost, r.Method) - - form := url.Values{} - form.Set("id", "t1_test") - - err := r.ParseForm() - assert.NoError(t, err) - assert.Equal(t, form, r.PostForm) - }) - - res, err := client.PostAndComment.Delete(ctx, "t1_test") - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, res.StatusCode) -} - -func TestPostAndCommentService_Save(t *testing.T) { - setup() - defer teardown() - - mux.HandleFunc("/api/save", func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, http.MethodPost, r.Method) - - form := url.Values{} - form.Set("id", "t1_test") - - err := r.ParseForm() - assert.NoError(t, err) - assert.Equal(t, form, r.PostForm) - }) - - res, err := client.PostAndComment.Save(ctx, "t1_test") - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, res.StatusCode) -} - -func TestPostAndCommentService_Unsave(t *testing.T) { - setup() - defer teardown() - - mux.HandleFunc("/api/unsave", func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, http.MethodPost, r.Method) - - form := url.Values{} - form.Set("id", "t1_test") - - err := r.ParseForm() - assert.NoError(t, err) - assert.Equal(t, form, r.PostForm) - }) - - res, err := client.PostAndComment.Unsave(ctx, "t1_test") - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, res.StatusCode) -} - -func TestPostAndCommentService_EnableReplies(t *testing.T) { - setup() - defer teardown() - - mux.HandleFunc("/api/sendreplies", func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, http.MethodPost, r.Method) - - form := url.Values{} - form.Set("id", "t1_test") - form.Set("state", "true") - - err := r.ParseForm() - assert.NoError(t, err) - assert.Equal(t, form, r.PostForm) - }) - - res, err := client.PostAndComment.EnableReplies(ctx, "t1_test") - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, res.StatusCode) -} - -func TestPostAndCommentService_DisableReplies(t *testing.T) { - setup() - defer teardown() - - mux.HandleFunc("/api/sendreplies", func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, http.MethodPost, r.Method) - - form := url.Values{} - form.Set("id", "t1_test") - form.Set("state", "false") - - err := r.ParseForm() - assert.NoError(t, err) - assert.Equal(t, form, r.PostForm) - }) - - res, err := client.PostAndComment.DisableReplies(ctx, "t1_test") - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, res.StatusCode) -} - -func TestPostAndCommentService_Lock(t *testing.T) { - setup() - defer teardown() - - mux.HandleFunc("/api/lock", func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, http.MethodPost, r.Method) - - form := url.Values{} - form.Set("id", "t1_test") - - err := r.ParseForm() - assert.NoError(t, err) - assert.Equal(t, form, r.PostForm) - }) - - res, err := client.PostAndComment.Lock(ctx, "t1_test") - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, res.StatusCode) -} - -func TestPostAndCommentService_Unlock(t *testing.T) { - setup() - defer teardown() - - mux.HandleFunc("/api/unlock", func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, http.MethodPost, r.Method) - - form := url.Values{} - form.Set("id", "t1_test") - - err := r.ParseForm() - assert.NoError(t, err) - assert.Equal(t, form, r.PostForm) - }) - - res, err := client.PostAndComment.Unlock(ctx, "t1_test") - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, res.StatusCode) -} - -func TestPostAndCommentService_Upvote(t *testing.T) { - setup() - defer teardown() - - mux.HandleFunc("/api/vote", func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, http.MethodPost, r.Method) - - form := url.Values{} - form.Set("id", "t1_test") - form.Set("dir", fmt.Sprint(upvote)) - form.Set("rank", "10") - - err := r.ParseForm() - assert.NoError(t, err) - assert.Equal(t, form, r.PostForm) - }) - - res, err := client.PostAndComment.Upvote(ctx, "t1_test") - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, res.StatusCode) -} - -func TestPostAndCommentService_Downvote(t *testing.T) { - setup() - defer teardown() - - mux.HandleFunc("/api/vote", func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, http.MethodPost, r.Method) - - form := url.Values{} - form.Set("id", "t1_test") - form.Set("dir", fmt.Sprint(downvote)) - form.Set("rank", "10") - - err := r.ParseForm() - assert.NoError(t, err) - assert.Equal(t, form, r.PostForm) - }) - - res, err := client.PostAndComment.Downvote(ctx, "t1_test") - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, res.StatusCode) -} - -func TestPostAndCommentService_RemoveVote(t *testing.T) { - setup() - defer teardown() - - mux.HandleFunc("/api/vote", func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, http.MethodPost, r.Method) - - form := url.Values{} - form.Set("id", "t1_test") - form.Set("dir", fmt.Sprint(novote)) - form.Set("rank", "10") - - err := r.ParseForm() - assert.NoError(t, err) - assert.Equal(t, form, r.PostForm) - }) - - res, err := client.PostAndComment.RemoveVote(ctx, "t1_test") - assert.NoError(t, err) - assert.Equal(t, http.StatusOK, res.StatusCode) -} diff --git a/post.go b/post.go index 0334308..405100c 100644 --- a/post.go +++ b/post.go @@ -15,7 +15,10 @@ import ( // related methods of the Reddit API. // // Reddit API docs: https://www.reddit.com/dev/api/#section_links_and_comments -type PostService service +type PostService struct { + *PostAndCommentService + client *Client +} type submittedLinkRoot struct { JSON struct { diff --git a/post_test.go b/post_test.go index 0620d8c..759636a 100644 --- a/post_test.go +++ b/post_test.go @@ -692,3 +692,211 @@ func TestPostService_RandomFromSubscriptions(t *testing.T) { assert.Equal(t, expectedPost2, post) assert.Equal(t, expectedComments, comments) } + +func TestPostService_Delete(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/del", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t3_test") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Post.Delete(ctx, "t3_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestPostService_Save(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/save", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t3_test") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Post.Save(ctx, "t3_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestPostService_Unsave(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/unsave", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t3_test") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Post.Unsave(ctx, "t3_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestPostService_EnableReplies(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/sendreplies", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t3_test") + form.Set("state", "true") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Post.EnableReplies(ctx, "t3_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestPostService_DisableReplies(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/sendreplies", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t3_test") + form.Set("state", "false") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Post.DisableReplies(ctx, "t3_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestPostService_Lock(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/lock", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t3_test") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Post.Lock(ctx, "t3_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestPostService_Unlock(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/unlock", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t3_test") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Post.Unlock(ctx, "t3_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestPostService_Upvote(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/vote", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t3_test") + form.Set("dir", "1") + form.Set("rank", "10") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Post.Upvote(ctx, "t3_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestPostService_Downvote(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/vote", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t3_test") + form.Set("dir", "-1") + form.Set("rank", "10") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Post.Downvote(ctx, "t3_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} + +func TestPostService_RemoveVote(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/vote", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("id", "t3_test") + form.Set("dir", "0") + form.Set("rank", "10") + + err := r.ParseForm() + assert.NoError(t, err) + assert.Equal(t, form, r.PostForm) + }) + + res, err := client.Post.RemoveVote(ctx, "t3_test") + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) +} diff --git a/reddit.go b/reddit.go index cb94e18..f99a7e2 100644 --- a/reddit.go +++ b/reddit.go @@ -91,30 +91,22 @@ type Client struct { // This is the client's user ID in Reddit's database. redditID string - // Reuse a single struct instead of allocating one for each service on the heap. - common service - - Account *AccountService - Comment *CommentService - Flair *FlairService - Listings *ListingsService - Moderation *ModerationService - Multi *MultiService - Post *PostService - PostAndComment *PostAndCommentService - Search *SearchService - Subreddit *SubredditService - User *UserService + Account *AccountService + Comment *CommentService + Flair *FlairService + Listings *ListingsService + Moderation *ModerationService + Multi *MultiService + Post *PostService + Search *SearchService + Subreddit *SubredditService + User *UserService oauth2Transport *oauth2.Transport onRequestCompleted RequestCompletionCallback } -type service struct { - client *Client -} - // OnRequestCompleted sets the client's request completion callback. func (c *Client) OnRequestCompleted(rc RequestCompletionCallback) { c.onRequestCompleted = rc @@ -126,7 +118,6 @@ func newClient(httpClient *http.Client) *Client { } // todo... - // getting a random httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error { redirectURL := req.URL.String() redirectURL = strings.Replace(redirectURL, "https://www.reddit.com", defaultBaseURL, 1) @@ -145,18 +136,18 @@ func newClient(httpClient *http.Client) *Client { c := &Client{client: httpClient, BaseURL: baseURL, TokenURL: tokenURL} - c.common.client = c - c.Account = (*AccountService)(&c.common) - c.Comment = (*CommentService)(&c.common) - c.Flair = (*FlairService)(&c.common) - c.Listings = (*ListingsService)(&c.common) - c.Moderation = (*ModerationService)(&c.common) - c.Multi = (*MultiService)(&c.common) - c.Post = (*PostService)(&c.common) - c.PostAndComment = (*PostAndCommentService)(&c.common) - c.Search = (*SearchService)(&c.common) - c.Subreddit = (*SubredditService)(&c.common) - c.User = (*UserService)(&c.common) + c.Account = &AccountService{client: c} + c.Flair = &FlairService{client: c} + c.Listings = &ListingsService{client: c} + c.Moderation = &ModerationService{client: c} + c.Multi = &MultiService{client: c} + c.Search = &SearchService{client: c} + c.Subreddit = &SubredditService{client: c} + c.User = &UserService{client: c} + + postAndCommentService := &PostAndCommentService{client: c} + c.Comment = &CommentService{client: c, PostAndCommentService: postAndCommentService} + c.Post = &PostService{client: c, PostAndCommentService: postAndCommentService} return c } @@ -292,8 +283,8 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res return response, err } -// GetRedditID returns the client's Reddit ID. -func (c *Client) GetRedditID(ctx context.Context) (string, error) { +// id returns the client's Reddit ID. +func (c *Client) id(ctx context.Context) (string, error) { if c.redditID != "" { return c.redditID, nil } diff --git a/reddit_test.go b/reddit_test.go index 87fd59d..8400f52 100644 --- a/reddit_test.go +++ b/reddit_test.go @@ -73,7 +73,6 @@ func testClientServices(t *testing.T, c *Client) { "Moderation", "Multi", "Post", - "PostAndComment", "Search", "Subreddit", "User", diff --git a/search.go b/search.go index daff974..3d11870 100644 --- a/search.go +++ b/search.go @@ -18,7 +18,9 @@ import ( // behaviour, e.g. sometimes limit=1 returns nothing when it should. // // Reddit API docs: https://www.reddit.com/dev/api/#section_search -type SearchService service +type SearchService struct { + client *Client +} func newSearchOptions(opts ...SearchOptionSetter) url.Values { searchOptions := make(url.Values) diff --git a/subreddit.go b/subreddit.go index 0767fb4..e9a937c 100644 --- a/subreddit.go +++ b/subreddit.go @@ -13,7 +13,9 @@ import ( // related methods of the Reddit API. // // Reddit API docs: https://www.reddit.com/dev/api/#section_subreddits -type SubredditService service +type SubredditService struct { + client *Client +} type rootSubreddit struct { Kind string `json:"kind,omitempty"` diff --git a/user.go b/user.go index 057dd0b..ee40029 100644 --- a/user.go +++ b/user.go @@ -11,7 +11,9 @@ import ( // related methods of the Reddit API. // // Reddit API docs: https://www.reddit.com/dev/api/#section_users -type UserService service +type UserService struct { + client *Client +} type rootUser struct { Kind string `json:"kind,omitempty"` @@ -442,7 +444,7 @@ func (s *UserService) Block(ctx context.Context, username string) (*Blocked, *Re // Unblock unblocks a user. func (s *UserService) Unblock(ctx context.Context, username string) (*Response, error) { - selfID, err := s.client.GetRedditID(ctx) + selfID, err := s.client.id(ctx) if err != nil { return nil, err }