Create GoldService
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
parent
b89df0b64f
commit
8752bdd2d6
4 changed files with 99 additions and 0 deletions
50
reddit/gold.go
Normal file
50
reddit/gold.go
Normal file
|
@ -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)
|
||||
}
|
46
reddit/gold_test.go
Normal file
46
reddit/gold_test.go
Normal file
|
@ -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)
|
||||
}
|
|
@ -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}
|
||||
|
|
|
@ -71,6 +71,7 @@ func testClientServices(t *testing.T, c *Client) {
|
|||
"Comment",
|
||||
"Emoji",
|
||||
"Flair",
|
||||
"Gold",
|
||||
"Listings",
|
||||
"Message",
|
||||
"Moderation",
|
||||
|
|
Loading…
Reference in a new issue