Chage NewRequest methods, add multi description kind to thing

Since Reddit's API accepts form data as the body for most of its
endpoints, it made sense to me to make the default NewRequest method
set the request body as form data (if provided of course). The
NewJSONRequest method can accept a JSON body.

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-09-09 23:02:06 -04:00
parent 34c2559707
commit def7e3bdb7
17 changed files with 157 additions and 164 deletions

View file

@ -274,7 +274,7 @@ func (s *AccountService) Settings(ctx context.Context) (*Settings, *Response, er
func (s *AccountService) UpdateSettings(ctx context.Context, settings *Settings) (*Settings, *Response, error) {
path := "api/v1/me/prefs"
req, err := s.client.NewRequest(http.MethodPatch, path, settings)
req, err := s.client.NewJSONRequest(http.MethodPatch, path, settings)
if err != nil {
return nil, nil, err
}
@ -391,7 +391,7 @@ func (s *AccountService) AddTrusted(ctx context.Context, username string) (*Resp
form.Set("api_type", "json")
form.Set("name", username)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -407,7 +407,7 @@ func (s *AccountService) RemoveTrusted(ctx context.Context, username string) (*R
form := url.Values{}
form.Set("name", username)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}

View file

@ -116,7 +116,7 @@ func (s *CollectionService) Create(ctx context.Context, createRequest *Collectio
return nil, nil, err
}
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
@ -137,7 +137,7 @@ func (s *CollectionService) Delete(ctx context.Context, id string) (*Response, e
form := url.Values{}
form.Set("collection_id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -153,7 +153,7 @@ func (s *CollectionService) AddPost(ctx context.Context, postID, collectionID st
form.Set("link_fullname", postID)
form.Set("collection_id", collectionID)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -169,7 +169,7 @@ func (s *CollectionService) RemovePost(ctx context.Context, postID, collectionID
form.Set("link_fullname", postID)
form.Set("collection_id", collectionID)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -185,7 +185,7 @@ func (s *CollectionService) ReorderPosts(ctx context.Context, collectionID strin
form.Set("collection_id", collectionID)
form.Set("link_ids", strings.Join(postIDs, ","))
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -201,7 +201,7 @@ func (s *CollectionService) UpdateTitle(ctx context.Context, id string, title st
form.Set("collection_id", id)
form.Set("title", title)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -217,7 +217,7 @@ func (s *CollectionService) UpdateDescription(ctx context.Context, id string, de
form.Set("collection_id", id)
form.Set("description", description)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -233,7 +233,7 @@ func (s *CollectionService) UpdateLayoutTimeline(ctx context.Context, id string)
form.Set("collection_id", id)
form.Set("display_layout", "TIMELINE")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -249,7 +249,7 @@ func (s *CollectionService) UpdateLayoutGallery(ctx context.Context, id string)
form.Set("collection_id", id)
form.Set("display_layout", "GALLERY")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -265,7 +265,7 @@ func (s *CollectionService) Follow(ctx context.Context, id string) (*Response, e
form.Set("collection_id", id)
form.Set("follow", "true")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -281,7 +281,7 @@ func (s *CollectionService) Unfollow(ctx context.Context, id string) (*Response,
form.Set("collection_id", id)
form.Set("follow", "false")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}

View file

@ -28,7 +28,7 @@ func (s *CommentService) Submit(ctx context.Context, parentID string, text strin
form.Set("parent", parentID)
form.Set("text", text)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
@ -52,7 +52,7 @@ func (s *CommentService) Edit(ctx context.Context, id string, text string) (*Com
form.Set("thing_id", id)
form.Set("text", text)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
@ -88,7 +88,7 @@ func (s *CommentService) LoadMoreReplies(ctx context.Context, comment *Comment)
// This was originally a GET, but with POST you can send a bigger payload
// since it's in the body and not the URI.
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}

View file

@ -104,12 +104,10 @@ func (s *EmojiService) Get(ctx context.Context, subreddit string) ([]*Emoji, []*
// Delete the emoji from the subreddit.
func (s *EmojiService) Delete(ctx context.Context, subreddit string, emoji string) (*Response, error) {
path := fmt.Sprintf("api/v1/%s/emoji/%s", subreddit, emoji)
req, err := s.client.NewRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
@ -122,7 +120,7 @@ func (s *EmojiService) SetSize(ctx context.Context, subreddit string, height, wi
form.Set("height", fmt.Sprint(height))
form.Set("width", fmt.Sprint(width))
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -133,12 +131,10 @@ func (s *EmojiService) SetSize(ctx context.Context, subreddit string, height, wi
// DisableCustomSize disables the custom emoji size in the subreddit.
func (s *EmojiService) DisableCustomSize(ctx context.Context, subreddit string) (*Response, error) {
path := fmt.Sprintf("api/v1/%s/emoji_custom_size", subreddit)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, nil)
req, err := s.client.NewRequest(http.MethodPost, path, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
@ -152,7 +148,7 @@ func (s *EmojiService) lease(ctx context.Context, subreddit, imagePath string) (
form.Set("mimetype", "image/png")
}
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return "", nil, nil, err
}
@ -191,7 +187,7 @@ func (s *EmojiService) upload(ctx context.Context, subreddit string, createReque
}
form.Set("s3_key", awsKey)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -276,7 +272,7 @@ func (s *EmojiService) Update(ctx context.Context, subreddit string, updateReque
return nil, err
}
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}

View file

@ -172,7 +172,7 @@ func (s *FlairService) Configure(ctx context.Context, subreddit string, request
}
form.Set("api_type", "json")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -188,7 +188,7 @@ func (s *FlairService) Enable(ctx context.Context, subreddit string) (*Response,
form.Set("api_type", "json")
form.Set("flair_enabled", "true")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -204,7 +204,7 @@ func (s *FlairService) Disable(ctx context.Context, subreddit string) (*Response
form.Set("api_type", "json")
form.Set("flair_enabled", "false")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -228,7 +228,7 @@ func (s *FlairService) UpsertUserTemplate(ctx context.Context, subreddit string,
form.Set("api_type", "json")
form.Set("flair_type", "USER_FLAIR")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
@ -258,7 +258,7 @@ func (s *FlairService) UpsertPostTemplate(ctx context.Context, subreddit string,
form.Set("api_type", "json")
form.Set("flair_type", "LINK_FLAIR")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
@ -280,7 +280,7 @@ func (s *FlairService) Delete(ctx context.Context, subreddit, username string) (
form.Set("api_type", "json")
form.Set("name", username)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -296,7 +296,7 @@ func (s *FlairService) DeleteTemplate(ctx context.Context, subreddit, id string)
form.Set("api_type", "json")
form.Set("flair_template_id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -312,7 +312,7 @@ func (s *FlairService) DeleteAllUserTemplates(ctx context.Context, subreddit str
form.Set("api_type", "json")
form.Set("flair_type", "USER_FLAIR")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -328,7 +328,7 @@ func (s *FlairService) DeleteAllPostTemplates(ctx context.Context, subreddit str
form.Set("api_type", "json")
form.Set("flair_type", "LINK_FLAIR")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -339,7 +339,7 @@ func (s *FlairService) DeleteAllPostTemplates(ctx context.Context, subreddit str
// ReorderUserTemplates reorders the user flair templates in the order provided in the slice.
func (s *FlairService) ReorderUserTemplates(ctx context.Context, subreddit string, ids []string) (*Response, error) {
path := fmt.Sprintf("api/v1/%s/flair_template_order/USER_FLAIR", subreddit)
req, err := s.client.NewRequest(http.MethodPatch, path, ids)
req, err := s.client.NewJSONRequest(http.MethodPatch, path, ids)
if err != nil {
return nil, err
}
@ -349,7 +349,7 @@ func (s *FlairService) ReorderUserTemplates(ctx context.Context, subreddit strin
// ReorderPostTemplates reorders the post flair templates in the order provided in the slice.
func (s *FlairService) ReorderPostTemplates(ctx context.Context, subreddit string, ids []string) (*Response, error) {
path := fmt.Sprintf("api/v1/%s/flair_template_order/LINK_FLAIR", subreddit)
req, err := s.client.NewRequest(http.MethodPatch, path, ids)
req, err := s.client.NewJSONRequest(http.MethodPatch, path, ids)
if err != nil {
return nil, err
}

View file

@ -20,12 +20,10 @@ type GoldService struct {
// 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)
}
@ -41,7 +39,7 @@ func (s *GoldService) Give(ctx context.Context, username string, months int) (*R
form := url.Values{}
form.Set("months", fmt.Sprint(months))
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}

View file

@ -118,12 +118,10 @@ type SendMessageRequest struct {
// This endpoint is heavily rate limited.
func (s *MessageService) ReadAll(ctx context.Context) (*Response, error) {
path := "api/read_all_messages"
req, err := s.client.NewRequest(http.MethodPost, path, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
@ -138,7 +136,7 @@ func (s *MessageService) Read(ctx context.Context, ids ...string) (*Response, er
form := url.Values{}
form.Set("id", strings.Join(ids, ","))
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -157,7 +155,7 @@ func (s *MessageService) Unread(ctx context.Context, ids ...string) (*Response,
form := url.Values{}
form.Set("id", strings.Join(ids, ","))
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -172,7 +170,7 @@ func (s *MessageService) Block(ctx context.Context, id string) (*Response, error
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -191,7 +189,7 @@ func (s *MessageService) Collapse(ctx context.Context, ids ...string) (*Response
form := url.Values{}
form.Set("id", strings.Join(ids, ","))
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -210,7 +208,7 @@ func (s *MessageService) Uncollapse(ctx context.Context, ids ...string) (*Respon
form := url.Values{}
form.Set("id", strings.Join(ids, ","))
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -225,7 +223,7 @@ func (s *MessageService) Delete(ctx context.Context, id string) (*Response, erro
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -247,7 +245,7 @@ func (s *MessageService) Send(ctx context.Context, sendRequest *SendMessageReque
}
form.Set("api_type", "json")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}

View file

@ -116,7 +116,7 @@ func (s *ModerationService) AcceptInvite(ctx context.Context, subreddit string)
form := url.Values{}
form.Set("api_type", "json")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -131,7 +131,7 @@ func (s *ModerationService) Approve(ctx context.Context, id string) (*Response,
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -147,7 +147,7 @@ func (s *ModerationService) Remove(ctx context.Context, id string) (*Response, e
form.Set("id", id)
form.Set("spam", "false")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -163,7 +163,7 @@ func (s *ModerationService) RemoveSpam(ctx context.Context, id string) (*Respons
form.Set("id", id)
form.Set("spam", "true")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -178,7 +178,7 @@ func (s *ModerationService) Leave(ctx context.Context, subredditID string) (*Res
form := url.Values{}
form.Set("id", subredditID)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -193,7 +193,7 @@ func (s *ModerationService) LeaveContributor(ctx context.Context, subredditID st
form := url.Values{}
form.Set("id", subredditID)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -259,7 +259,7 @@ func (s *ModerationService) IgnoreReports(ctx context.Context, id string) (*Resp
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -274,7 +274,7 @@ func (s *ModerationService) UnignoreReports(ctx context.Context, id string) (*Re
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -293,7 +293,7 @@ func (s *ModerationService) Invite(ctx context.Context, subreddit string, userna
form.Set("type", "moderator_invite")
form.Set("permissions", permissions.String())
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -317,7 +317,7 @@ func (s *ModerationService) SetPermissions(ctx context.Context, subreddit string
form.Set("type", "moderator_invite")
form.Set("permissions", permissions.String())
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -338,7 +338,7 @@ func (s *ModerationService) Ban(ctx context.Context, subreddit string, username
form.Set("name", username)
form.Set("type", "banned")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -364,7 +364,7 @@ func (s *ModerationService) BanWiki(ctx context.Context, subreddit string, usern
form.Set("name", username)
form.Set("type", "wikibanned")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -415,7 +415,7 @@ func (s *ModerationService) createRelationship(ctx context.Context, subreddit, u
form.Set("name", username)
form.Set("type", relationship)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -431,7 +431,7 @@ func (s *ModerationService) deleteRelationship(ctx context.Context, subreddit, u
form.Set("name", username)
form.Set("type", relationship)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -449,7 +449,7 @@ func (s *ModerationService) Distinguish(ctx context.Context, id string) (*Respon
form.Set("how", "yes")
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -468,7 +468,7 @@ func (s *ModerationService) DistinguishAndSticky(ctx context.Context, id string)
form.Set("sticky", "true")
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -485,7 +485,7 @@ func (s *ModerationService) Undistinguish(ctx context.Context, id string) (*Resp
form.Set("how", "no")
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}

View file

@ -109,15 +109,12 @@ func (r *MultiCreateOrUpdateRequest) Form() url.Values {
}
type rootMultiDescription struct {
Data struct {
Body string `json:"body_md"`
} `json:"data"`
Body string `json:"body_md"`
}
// Get the multireddit from its url path.
func (s *MultiService) Get(ctx context.Context, multiPath string) (*Multi, *Response, error) {
path := fmt.Sprintf("api/multi/%s", multiPath)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
@ -136,7 +133,6 @@ func (s *MultiService) Get(ctx context.Context, multiPath string) (*Multi, *Resp
// Mine returns your multireddits.
func (s *MultiService) Mine(ctx context.Context) ([]*Multi, *Response, error) {
path := "api/multi/mine"
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
@ -155,7 +151,6 @@ func (s *MultiService) Mine(ctx context.Context) ([]*Multi, *Response, error) {
// Or, if the user is you, all of your multireddits.
func (s *MultiService) Of(ctx context.Context, username string) ([]*Multi, *Response, error) {
path := fmt.Sprintf("api/multi/user/%s", username)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
@ -182,7 +177,7 @@ func (s *MultiService) Copy(ctx context.Context, copyRequest *MultiCopyRequest)
return nil, nil, err
}
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
@ -204,8 +199,7 @@ func (s *MultiService) Create(ctx context.Context, createRequest *MultiCreateOrU
}
path := "api/multi"
req, err := s.client.NewRequestWithForm(http.MethodPost, path, createRequest.Form())
req, err := s.client.NewRequest(http.MethodPost, path, createRequest.Form())
if err != nil {
return nil, nil, err
}
@ -228,8 +222,7 @@ func (s *MultiService) Update(ctx context.Context, multiPath string, updateReque
}
path := fmt.Sprintf("api/multi/%s", multiPath)
req, err := s.client.NewRequestWithForm(http.MethodPut, path, updateRequest.Form())
req, err := s.client.NewRequest(http.MethodPut, path, updateRequest.Form())
if err != nil {
return nil, nil, err
}
@ -247,17 +240,15 @@ func (s *MultiService) Update(ctx context.Context, multiPath string, updateReque
// Delete a multireddit.
func (s *MultiService) Delete(ctx context.Context, multiPath string) (*Response, error) {
path := fmt.Sprintf("api/multi/%s", multiPath)
req, err := s.client.NewRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// GetDescription gets a multireddit's description.
func (s *MultiService) GetDescription(ctx context.Context, multiPath string) (string, *Response, error) {
// Description gets a multireddit's description.
func (s *MultiService) Description(ctx context.Context, multiPath string) (string, *Response, error) {
path := fmt.Sprintf("api/multi/%s/description", multiPath)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
@ -265,13 +256,14 @@ func (s *MultiService) GetDescription(ctx context.Context, multiPath string) (st
return "", nil, err
}
root := new(rootMultiDescription)
root := new(thing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return "", resp, err
}
return root.Data.Body, resp, nil
multiDescription, _ := root.MultiDescription()
return multiDescription, resp, nil
}
// UpdateDescription updates a multireddit's description.
@ -281,18 +273,19 @@ func (s *MultiService) UpdateDescription(ctx context.Context, multiPath string,
form := url.Values{}
form.Set("model", fmt.Sprintf(`{"body_md":"%s"}`, description))
req, err := s.client.NewRequestWithForm(http.MethodPut, path, form)
req, err := s.client.NewRequest(http.MethodPut, path, form)
if err != nil {
return "", nil, err
}
root := new(rootMultiDescription)
root := new(thing)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return "", resp, err
}
return root.Data.Body, resp, nil
multiDescription, _ := root.MultiDescription()
return multiDescription, resp, nil
}
// AddSubreddit adds a subreddit to a multireddit.
@ -302,7 +295,7 @@ func (s *MultiService) AddSubreddit(ctx context.Context, multiPath string, subre
form := url.Values{}
form.Set("model", fmt.Sprintf(`{"name":"%s"}`, subreddit))
req, err := s.client.NewRequestWithForm(http.MethodPut, path, form)
req, err := s.client.NewRequest(http.MethodPut, path, form)
if err != nil {
return nil, err
}
@ -313,11 +306,9 @@ func (s *MultiService) AddSubreddit(ctx context.Context, multiPath string, subre
// DeleteSubreddit removes a subreddit from a multireddit.
func (s *MultiService) DeleteSubreddit(ctx context.Context, multiPath string, subreddit string) (*Response, error) {
path := fmt.Sprintf("api/multi/%s/r/%s", multiPath, subreddit)
req, err := s.client.NewRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}

View file

@ -217,7 +217,7 @@ func TestMultiService_Delete(t *testing.T) {
require.NoError(t, err)
}
func TestMultiService_GetDescription(t *testing.T) {
func TestMultiService_Description(t *testing.T) {
client, mux, teardown := setup()
defer teardown()
@ -229,7 +229,7 @@ func TestMultiService_GetDescription(t *testing.T) {
fmt.Fprint(w, blob)
})
description, _, err := client.Multi.GetDescription(ctx, "user/testuser/m/testmulti")
description, _, err := client.Multi.Description(ctx, "user/testuser/m/testmulti")
require.NoError(t, err)
require.Equal(t, "hello world", description)
}

View file

@ -32,7 +32,7 @@ func (s *postAndCommentService) Delete(ctx context.Context, id string) (*Respons
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -47,7 +47,7 @@ func (s *postAndCommentService) Save(ctx context.Context, id string) (*Response,
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -62,7 +62,7 @@ func (s *postAndCommentService) Unsave(ctx context.Context, id string) (*Respons
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -78,7 +78,7 @@ func (s *postAndCommentService) EnableReplies(ctx context.Context, id string) (*
form.Set("id", id)
form.Set("state", "true")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -94,7 +94,7 @@ func (s *postAndCommentService) DisableReplies(ctx context.Context, id string) (
form.Set("id", id)
form.Set("state", "false")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -109,7 +109,7 @@ func (s *postAndCommentService) Lock(ctx context.Context, id string) (*Response,
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -124,7 +124,7 @@ func (s *postAndCommentService) Unlock(ctx context.Context, id string) (*Respons
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -140,7 +140,7 @@ func (s *postAndCommentService) vote(ctx context.Context, id string, vote vote)
form.Set("dir", fmt.Sprint(vote))
form.Set("rank", "10")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -163,7 +163,7 @@ func (s *postAndCommentService) RemoveVote(ctx context.Context, id string) (*Res
return s.vote(ctx, id, novote)
}
// Report reports a post or comment.
// Report a post or comment.
// The reason must not be longer than 100 characters.
func (s *postAndCommentService) Report(ctx context.Context, id string, reason string) (*Response, error) {
path := "api/report"
@ -173,7 +173,7 @@ func (s *postAndCommentService) Report(ctx context.Context, id string, reason st
form.Set("thing_id", id)
form.Set("reason", reason)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}

View file

@ -123,7 +123,7 @@ func (s *PostService) submit(ctx context.Context, v interface{}) (*Submitted, *R
}
form.Set("api_type", "json")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
@ -165,7 +165,7 @@ func (s *PostService) Edit(ctx context.Context, id string, text string) (*Post,
form.Set("thing_id", id)
form.Set("text", text)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
@ -190,7 +190,7 @@ func (s *PostService) Hide(ctx context.Context, ids ...string) (*Response, error
form := url.Values{}
form.Set("id", strings.Join(ids, ","))
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -209,7 +209,7 @@ func (s *PostService) Unhide(ctx context.Context, ids ...string) (*Response, err
form := url.Values{}
form.Set("id", strings.Join(ids, ","))
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -224,7 +224,7 @@ func (s *PostService) MarkNSFW(ctx context.Context, id string) (*Response, error
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -239,7 +239,7 @@ func (s *PostService) UnmarkNSFW(ctx context.Context, id string) (*Response, err
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -254,7 +254,7 @@ func (s *PostService) Spoiler(ctx context.Context, id string) (*Response, error)
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -269,7 +269,7 @@ func (s *PostService) Unspoiler(ctx context.Context, id string) (*Response, erro
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -292,7 +292,7 @@ func (s *PostService) Sticky(ctx context.Context, id string, bottom bool) (*Resp
form.Set("num", "1")
}
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -309,7 +309,7 @@ func (s *PostService) Unsticky(ctx context.Context, id string) (*Response, error
form.Set("id", id)
form.Set("state", "false")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -341,7 +341,7 @@ func (s *PostService) PinToProfile(ctx context.Context, id string) (*Response, e
form.Set("to_profile", "true")
// form.Set("num", fmt.Sprint(pos))
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -359,7 +359,7 @@ func (s *PostService) UnpinFromProfile(ctx context.Context, id string) (*Respons
form.Set("state", "false")
form.Set("to_profile", "true")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -377,7 +377,7 @@ func (s *PostService) setSuggestedSort(ctx context.Context, id string, sort stri
form.Set("id", id)
form.Set("sort", sort)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -441,7 +441,7 @@ func (s *PostService) EnableContestMode(ctx context.Context, id string) (*Respon
form.Set("id", id)
form.Set("state", "true")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -458,7 +458,7 @@ func (s *PostService) DisableContestMode(ctx context.Context, id string) (*Respo
form.Set("id", id)
form.Set("state", "false")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -488,7 +488,7 @@ func (s *PostService) LoadMoreComments(ctx context.Context, pc *PostAndComments)
// This was originally a GET, but with POST you can send a bigger payload
// since it's in the body and not the URI.
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -575,7 +575,7 @@ func (s *PostService) MarkVisited(ctx context.Context, ids ...string) (*Response
form := url.Values{}
form.Set("links", strings.Join(ids, ","))
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}

View file

@ -217,10 +217,36 @@ func (c *Client) UserAgent() string {
return c.userAgent
}
// NewRequest creates an API request with a JSON body.
// NewRequest creates an API request with form data as the body.
// The path is the relative URL which will be resolves to the BaseURL of the Client.
// It should always be specified without a preceding slash.
func (c *Client) NewRequest(method string, path string, form url.Values) (*http.Request, error) {
u, err := c.BaseURL.Parse(path)
if err != nil {
return nil, err
}
var body io.Reader
if form != nil {
body = strings.NewReader(form.Encode())
}
req, err := http.NewRequest(method, u.String(), body)
if err != nil {
return nil, err
}
c.appendJSONExtensionToRequestURLPath(req)
req.Header.Add(headerContentType, mediaTypeForm)
req.Header.Add(headerAccept, mediaTypeJSON)
return req, nil
}
// NewJSONRequest creates an API request with a JSON body.
// The path is the relative URL which will be resolved to the BaseURL of the Client.
// It should always be specified without a preceding slash.
func (c *Client) NewRequest(method string, path string, body interface{}) (*http.Request, error) {
func (c *Client) NewJSONRequest(method string, path string, body interface{}) (*http.Request, error) {
u, err := c.BaseURL.Parse(path)
if err != nil {
return nil, err
@ -247,27 +273,6 @@ func (c *Client) NewRequest(method string, path string, body interface{}) (*http
return req, nil
}
// NewRequestWithForm creates an API request with form data.
// The path is the relative URL which will be resolves to the BaseURL of the Client.
// It should always be specified without a preceding slash.
func (c *Client) NewRequestWithForm(method string, path string, form url.Values) (*http.Request, error) {
u, err := c.BaseURL.Parse(path)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, u.String(), strings.NewReader(form.Encode()))
if err != nil {
return nil, err
}
c.appendJSONExtensionToRequestURLPath(req)
req.Header.Add(headerContentType, mediaTypeForm)
req.Header.Add(headerAccept, mediaTypeJSON)
return req, nil
}
// Response is a Reddit response. This wraps the standard http.Response returned from Reddit.
type Response struct {
*http.Response

View file

@ -187,12 +187,10 @@ func (s *SubredditService) GetSticky2(ctx context.Context, subreddit string) (*P
func (s *SubredditService) handleSubscription(ctx context.Context, form url.Values) (*Response, error) {
path := "api/subscribe"
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
@ -237,7 +235,7 @@ func (s *SubredditService) Favorite(ctx context.Context, subreddit string) (*Res
form.Set("make_favorite", "true")
form.Set("api_type", "json")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -254,7 +252,7 @@ func (s *SubredditService) Unfavorite(ctx context.Context, subreddit string) (*R
form.Set("make_favorite", "false")
form.Set("api_type", "json")
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -425,7 +423,7 @@ func (s *SubredditService) random(ctx context.Context, nsfw bool) (*Subreddit, *
root := new(struct {
Data struct {
Children []struct {
Children [1]struct {
Data struct {
Subreddit *Subreddit `json:"sr_detail"`
} `json:"data"`
@ -437,12 +435,8 @@ func (s *SubredditService) random(ctx context.Context, nsfw bool) (*Subreddit, *
return nil, resp, err
}
var sr *Subreddit
if len(root.Data.Children) > 0 {
sr = root.Data.Children[0].Data.Subreddit
}
return sr, resp, nil
subreddit := root.Data.Children[0].Data.Subreddit
return subreddit, resp, nil
}
// Random returns a random SFW subreddit.

View file

@ -19,6 +19,7 @@ const (
kindMore = "more"
kindModAction = "modaction"
kindMulti = "LabeledMulti"
kindMultiDescription = "LabeledMultiDescription"
kindWikiPage = "wikipage"
kindWikiPageListing = "wikipagelisting"
kindWikiPageSettings = "wikipagesettings"
@ -91,6 +92,8 @@ func (t *thing) UnmarshalJSON(b []byte) error {
v = new(ModAction)
case kindMulti:
v = new(Multi)
case kindMultiDescription:
v = new(rootMultiDescription)
case kindTrophy:
v = new(Trophy)
case kindTrophyList:
@ -156,6 +159,14 @@ func (t *thing) Multi() (v *Multi, ok bool) {
return
}
func (t *thing) MultiDescription() (s string, ok bool) {
v, ok := t.Data.(*rootMultiDescription)
if ok {
s = v.Body
}
return
}
func (t *thing) Trophy() (v *Trophy, ok bool) {
v, ok = t.Data.(*Trophy)
return

View file

@ -366,7 +366,7 @@ func (s *UserService) Friend(ctx context.Context, username string) (*Relationshi
}{username}
path := fmt.Sprintf("api/v1/me/friends/%s", username)
req, err := s.client.NewRequest(http.MethodPut, path, body)
req, err := s.client.NewJSONRequest(http.MethodPut, path, body)
if err != nil {
return nil, nil, err
}
@ -397,7 +397,7 @@ func (s *UserService) Block(ctx context.Context, username string) (*Blocked, *Re
form := url.Values{}
form.Set("name", username)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
@ -418,7 +418,7 @@ func (s *UserService) BlockByID(ctx context.Context, id string) (*Blocked, *Resp
form := url.Values{}
form.Set("account_id", id)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
@ -446,7 +446,7 @@ func (s *UserService) Unblock(ctx context.Context, username string) (*Response,
form.Set("type", "enemy")
form.Set("container", selfID)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -468,7 +468,7 @@ func (s *UserService) UnblockByID(ctx context.Context, id string) (*Response, er
form.Set("type", "enemy")
form.Set("container", selfID)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}

View file

@ -244,7 +244,7 @@ func (s *WikiService) Edit(ctx context.Context, editRequest *WikiPageEditRequest
}
path := fmt.Sprintf("r/%s/api/wiki/edit", editRequest.Subreddit)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -260,7 +260,7 @@ func (s *WikiService) Revert(ctx context.Context, subreddit, page, revisionID st
form.Set("page", page)
form.Set("revision", revisionID)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -299,7 +299,7 @@ func (s *WikiService) UpdateSettings(ctx context.Context, subreddit, page string
}
path := fmt.Sprintf("r/%s/wiki/settings/%s", subreddit, page)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, nil, err
}
@ -346,7 +346,7 @@ func (s *WikiService) ToggleVisibility(ctx context.Context, subreddit, page, rev
form.Set("page", page)
form.Set("revision", revisionID)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return false, nil, err
}
@ -416,7 +416,7 @@ func (s *WikiService) Allow(ctx context.Context, subreddit, page, username strin
form.Set("page", page)
form.Set("username", username)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
@ -432,7 +432,7 @@ func (s *WikiService) Deny(ctx context.Context, subreddit, page, username string
form.Set("page", page)
form.Set("username", username)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}