diff --git a/reddit/gold.go b/reddit/gold.go new file mode 100644 index 0000000..e25b367 --- /dev/null +++ b/reddit/gold.go @@ -0,0 +1,50 @@ +package reddit + +import ( + "context" + "errors" + "fmt" + "net/http" + "net/url" +) + +// GoldService handles communication with the gold +// related methods of the Reddit API. +// +// Reddit API docs: https://www.reddit.com/dev/api/#section_collections +type GoldService struct { + client *Client +} + +// Gild the post or comment via its full ID. +// This requires you to own Reddit coins and will consume them. +func (s *GoldService) Gild(ctx context.Context, id string) (*Response, error) { + path := fmt.Sprintf("api/v1/gold/gild/%s", id) + + req, err := s.client.NewRequest(http.MethodPost, path, nil) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} + +// Give the user between 1 and 36 (inclusive) months of gold. +// This requires you to own Reddit coins and will consume them. +func (s *GoldService) Give(ctx context.Context, username string, months int) (*Response, error) { + if months < 1 || months > 36 { + return nil, errors.New("months: must be between 1 and 36 (inclusive)") + } + + path := fmt.Sprintf("api/v1/gold/give/%s", username) + + form := url.Values{} + form.Set("months", fmt.Sprint(months)) + + req, err := s.client.NewRequestWithForm(http.MethodPost, path, form) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} diff --git a/reddit/gold_test.go b/reddit/gold_test.go new file mode 100644 index 0000000..47329bf --- /dev/null +++ b/reddit/gold_test.go @@ -0,0 +1,46 @@ +package reddit + +import ( + "net/http" + "net/url" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestGoldService_Gild(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/v1/gold/gild/t1_test", func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, http.MethodPost, r.Method) + }) + + _, err := client.Gold.Gild(ctx, "t1_test") + require.NoError(t, err) +} + +func TestGoldService_Give(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/api/v1/gold/give/testuser", func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, http.MethodPost, r.Method) + + form := url.Values{} + form.Set("months", "1") + + err := r.ParseForm() + require.NoError(t, err) + require.Equal(t, form, r.Form) + }) + + _, err := client.Gold.Give(ctx, "testuser", 0) + require.EqualError(t, err, "months: must be between 1 and 36 (inclusive)") + + _, err = client.Gold.Give(ctx, "testuser", 37) + require.EqualError(t, err, "months: must be between 1 and 36 (inclusive)") + + _, err = client.Gold.Give(ctx, "testuser", 1) + require.NoError(t, err) +} diff --git a/reddit/reddit.go b/reddit/reddit.go index 635c265..d5c95cc 100644 --- a/reddit/reddit.go +++ b/reddit/reddit.go @@ -96,6 +96,7 @@ type Client struct { Comment *CommentService Emoji *EmojiService Flair *FlairService + Gold *GoldService Listings *ListingsService Message *MessageService Moderation *ModerationService @@ -145,6 +146,7 @@ func newClient(httpClient *http.Client) *Client { c.Collection = &CollectionService{client: c} c.Emoji = &EmojiService{client: c} c.Flair = &FlairService{client: c} + c.Gold = &GoldService{client: c} c.Listings = &ListingsService{client: c} c.Message = &MessageService{client: c} c.Moderation = &ModerationService{client: c} diff --git a/reddit/reddit_test.go b/reddit/reddit_test.go index ff05254..30812c8 100644 --- a/reddit/reddit_test.go +++ b/reddit/reddit_test.go @@ -71,6 +71,7 @@ func testClientServices(t *testing.T, c *Client) { "Comment", "Emoji", "Flair", + "Gold", "Listings", "Message", "Moderation",