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:
parent
977a222d66
commit
2ea893ab16
16 changed files with 476 additions and 264 deletions
|
@ -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"`
|
||||
|
|
|
@ -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.
|
||||
|
|
208
comment_test.go
208
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)
|
||||
}
|
||||
|
|
4
flair.go
4
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 {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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.
|
||||
|
|
4
multi.go
4
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"`
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
5
post.go
5
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 {
|
||||
|
|
208
post_test.go
208
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)
|
||||
}
|
||||
|
|
37
reddit.go
37
reddit.go
|
@ -91,9 +91,6 @@ 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
|
||||
|
@ -101,7 +98,6 @@ type Client struct {
|
|||
Moderation *ModerationService
|
||||
Multi *MultiService
|
||||
Post *PostService
|
||||
PostAndComment *PostAndCommentService
|
||||
Search *SearchService
|
||||
Subreddit *SubredditService
|
||||
User *UserService
|
||||
|
@ -111,10 +107,6 @@ type Client struct {
|
|||
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
|
||||
}
|
||||
|
|
|
@ -73,7 +73,6 @@ func testClientServices(t *testing.T, c *Client) {
|
|||
"Moderation",
|
||||
"Multi",
|
||||
"Post",
|
||||
"PostAndComment",
|
||||
"Search",
|
||||
"Subreddit",
|
||||
"User",
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"`
|
||||
|
|
6
user.go
6
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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue