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 <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-07-21 23:05:24 -04:00
parent 977a222d66
commit 2ea893ab16
16 changed files with 476 additions and 264 deletions

View File

@ -10,7 +10,9 @@ import (
// related methods of the Reddit API. // related methods of the Reddit API.
// //
// Reddit API docs: https://www.reddit.com/dev/api/#section_account // Reddit API docs: https://www.reddit.com/dev/api/#section_account
type AccountService service type AccountService struct {
client *Client
}
type rootSubredditKarma struct { type rootSubredditKarma struct {
Kind string `json:"kind,omitempty"` Kind string `json:"kind,omitempty"`

View File

@ -10,7 +10,10 @@ import (
// related methods of the Reddit API. // related methods of the Reddit API.
// //
// Reddit API docs: https://www.reddit.com/dev/api/#section_links_and_comments // 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. // Submit submits a comment as a reply to a post, comment, or message.
// parentID is the full ID of the thing being replied to. // parentID is the full ID of the thing being replied to.

View File

@ -93,3 +93,211 @@ func TestCommentService_Edit(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expectedCommentSubmitOrEdit, comment) 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)
}

View File

@ -10,7 +10,9 @@ import (
// related methods of the Reddit API. // related methods of the Reddit API.
// //
// Reddit API docs: https://www.reddit.com/dev/api/#section_flair // 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 // Flair is a flair on Reddit
type Flair struct { type Flair struct {

View File

@ -12,7 +12,9 @@ import (
// related methods of the Reddit API. // related methods of the Reddit API.
// //
// Reddit API docs: https://www.reddit.com/dev/api/#section_listings // 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. // Get returns posts, comments, and subreddits from their IDs.
func (s *ListingsService) Get(ctx context.Context, ids ...string) ([]*Post, []*Comment, []*Subreddit, *Response, error) { func (s *ListingsService) Get(ctx context.Context, ids ...string) ([]*Post, []*Comment, []*Subreddit, *Response, error) {

View File

@ -11,7 +11,9 @@ import (
// related methods of the Reddit API. // related methods of the Reddit API.
// //
// Reddit API docs: https://www.reddit.com/dev/api/#section_moderation // 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 // ModAction is an action executed by a moderator of a subreddit, such
// as inviting another user to be a mod, or setting permissions. // as inviting another user to be a mod, or setting permissions.

View File

@ -13,7 +13,9 @@ import (
// related methods of the Reddit API. // related methods of the Reddit API.
// //
// Reddit API docs: https://www.reddit.com/dev/api#section_multis // Reddit API docs: https://www.reddit.com/dev/api#section_multis
type MultiService service type MultiService struct {
client *Client
}
type multiRoot struct { type multiRoot struct {
Kind string `json:"kind,omitempty"` Kind string `json:"kind,omitempty"`

View File

@ -14,7 +14,9 @@ import (
// Reddit API docs: https://www.reddit.com/dev/api/#section_links_and_comments // Reddit API docs: https://www.reddit.com/dev/api/#section_links_and_comments
// //
// todo: this is ugly, find a solution // todo: this is ugly, find a solution
type PostAndCommentService service type PostAndCommentService struct {
client *Client
}
type vote int type vote int

View File

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

View File

@ -15,7 +15,10 @@ import (
// related methods of the Reddit API. // related methods of the Reddit API.
// //
// Reddit API docs: https://www.reddit.com/dev/api/#section_links_and_comments // 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 { type submittedLinkRoot struct {
JSON struct { JSON struct {

View File

@ -692,3 +692,211 @@ func TestPostService_RandomFromSubscriptions(t *testing.T) {
assert.Equal(t, expectedPost2, post) assert.Equal(t, expectedPost2, post)
assert.Equal(t, expectedComments, comments) 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)
}

View File

@ -91,30 +91,22 @@ type Client struct {
// This is the client's user ID in Reddit's database. // This is the client's user ID in Reddit's database.
redditID string redditID string
// Reuse a single struct instead of allocating one for each service on the heap. Account *AccountService
common service Comment *CommentService
Flair *FlairService
Account *AccountService Listings *ListingsService
Comment *CommentService Moderation *ModerationService
Flair *FlairService Multi *MultiService
Listings *ListingsService Post *PostService
Moderation *ModerationService Search *SearchService
Multi *MultiService Subreddit *SubredditService
Post *PostService User *UserService
PostAndComment *PostAndCommentService
Search *SearchService
Subreddit *SubredditService
User *UserService
oauth2Transport *oauth2.Transport oauth2Transport *oauth2.Transport
onRequestCompleted RequestCompletionCallback onRequestCompleted RequestCompletionCallback
} }
type service struct {
client *Client
}
// OnRequestCompleted sets the client's request completion callback. // OnRequestCompleted sets the client's request completion callback.
func (c *Client) OnRequestCompleted(rc RequestCompletionCallback) { func (c *Client) OnRequestCompleted(rc RequestCompletionCallback) {
c.onRequestCompleted = rc c.onRequestCompleted = rc
@ -126,7 +118,6 @@ func newClient(httpClient *http.Client) *Client {
} }
// todo... // todo...
// getting a random
httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error { httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
redirectURL := req.URL.String() redirectURL := req.URL.String()
redirectURL = strings.Replace(redirectURL, "https://www.reddit.com", defaultBaseURL, 1) 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 := &Client{client: httpClient, BaseURL: baseURL, TokenURL: tokenURL}
c.common.client = c c.Account = &AccountService{client: c}
c.Account = (*AccountService)(&c.common) c.Flair = &FlairService{client: c}
c.Comment = (*CommentService)(&c.common) c.Listings = &ListingsService{client: c}
c.Flair = (*FlairService)(&c.common) c.Moderation = &ModerationService{client: c}
c.Listings = (*ListingsService)(&c.common) c.Multi = &MultiService{client: c}
c.Moderation = (*ModerationService)(&c.common) c.Search = &SearchService{client: c}
c.Multi = (*MultiService)(&c.common) c.Subreddit = &SubredditService{client: c}
c.Post = (*PostService)(&c.common) c.User = &UserService{client: c}
c.PostAndComment = (*PostAndCommentService)(&c.common)
c.Search = (*SearchService)(&c.common) postAndCommentService := &PostAndCommentService{client: c}
c.Subreddit = (*SubredditService)(&c.common) c.Comment = &CommentService{client: c, PostAndCommentService: postAndCommentService}
c.User = (*UserService)(&c.common) c.Post = &PostService{client: c, PostAndCommentService: postAndCommentService}
return c return c
} }
@ -292,8 +283,8 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res
return response, err return response, err
} }
// GetRedditID returns the client's Reddit ID. // id returns the client's Reddit ID.
func (c *Client) GetRedditID(ctx context.Context) (string, error) { func (c *Client) id(ctx context.Context) (string, error) {
if c.redditID != "" { if c.redditID != "" {
return c.redditID, nil return c.redditID, nil
} }

View File

@ -73,7 +73,6 @@ func testClientServices(t *testing.T, c *Client) {
"Moderation", "Moderation",
"Multi", "Multi",
"Post", "Post",
"PostAndComment",
"Search", "Search",
"Subreddit", "Subreddit",
"User", "User",

View File

@ -18,7 +18,9 @@ import (
// behaviour, e.g. sometimes limit=1 returns nothing when it should. // behaviour, e.g. sometimes limit=1 returns nothing when it should.
// //
// Reddit API docs: https://www.reddit.com/dev/api/#section_search // 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 { func newSearchOptions(opts ...SearchOptionSetter) url.Values {
searchOptions := make(url.Values) searchOptions := make(url.Values)

View File

@ -13,7 +13,9 @@ import (
// related methods of the Reddit API. // related methods of the Reddit API.
// //
// Reddit API docs: https://www.reddit.com/dev/api/#section_subreddits // Reddit API docs: https://www.reddit.com/dev/api/#section_subreddits
type SubredditService service type SubredditService struct {
client *Client
}
type rootSubreddit struct { type rootSubreddit struct {
Kind string `json:"kind,omitempty"` Kind string `json:"kind,omitempty"`

View File

@ -11,7 +11,9 @@ import (
// related methods of the Reddit API. // related methods of the Reddit API.
// //
// Reddit API docs: https://www.reddit.com/dev/api/#section_users // Reddit API docs: https://www.reddit.com/dev/api/#section_users
type UserService service type UserService struct {
client *Client
}
type rootUser struct { type rootUser struct {
Kind string `json:"kind,omitempty"` Kind string `json:"kind,omitempty"`
@ -442,7 +444,7 @@ func (s *UserService) Block(ctx context.Context, username string) (*Blocked, *Re
// Unblock unblocks a user. // Unblock unblocks a user.
func (s *UserService) Unblock(ctx context.Context, username string) (*Response, error) { 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 { if err != nil {
return nil, err return nil, err
} }