Add method to create request with form data
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
parent
4094044593
commit
8f95f3880a
10 changed files with 40 additions and 41 deletions
|
@ -397,7 +397,7 @@ func (s *AccountService) AddTrusted(ctx context.Context, username string) (*Resp
|
|||
form.Set("name", username)
|
||||
// todo: you can also do this with the user id. form.Set("id", id). should we? or is this enough?
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -414,7 +414,7 @@ func (s *AccountService) RemoveTrusted(ctx context.Context, username string) (*R
|
|||
form.Set("name", username)
|
||||
// todo: you can also do this with the user id. form.Set("id", id). should we? or is this enough?
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
11
comment.go
11
comment.go
|
@ -3,6 +3,7 @@ package reddit
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
@ -25,7 +26,7 @@ func (s *CommentService) Submit(ctx context.Context, id string, text string) (*C
|
|||
form.Set("parent", id)
|
||||
form.Set("text", text)
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -54,7 +55,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.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -80,7 +81,7 @@ func (s *CommentService) Delete(ctx context.Context, id string) (*Response, erro
|
|||
form := url.Values{}
|
||||
form.Set("id", id)
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -104,7 +105,7 @@ func (s *CommentService) Save(ctx context.Context, id string) (*Response, error)
|
|||
form := url.Values{}
|
||||
form.Set("id", id)
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -128,7 +129,7 @@ func (s *CommentService) Unsave(ctx context.Context, id string) (*Response, erro
|
|||
form := url.Values{}
|
||||
form.Set("id", id)
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
10
errors.go
10
errors.go
|
@ -6,19 +6,19 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
// RedditError is an error coming from Reddit
|
||||
type RedditError struct {
|
||||
// APIError is an error coming from Reddit
|
||||
type APIError struct {
|
||||
Label string
|
||||
Reason string
|
||||
Field string
|
||||
}
|
||||
|
||||
func (e *RedditError) Error() string {
|
||||
func (e *APIError) Error() string {
|
||||
return fmt.Sprintf("field %q caused %s: %s", e.Field, e.Label, e.Reason)
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the json.Unmarshaler interface.
|
||||
func (e *RedditError) UnmarshalJSON(data []byte) error {
|
||||
func (e *APIError) UnmarshalJSON(data []byte) error {
|
||||
var info []string
|
||||
|
||||
err := json.Unmarshal(data, &info)
|
||||
|
@ -43,7 +43,7 @@ type JSONErrorResponse struct {
|
|||
Response *http.Response `json:"-"`
|
||||
|
||||
JSON *struct {
|
||||
Errors []RedditError `json:"errors,omitempty"`
|
||||
Errors []APIError `json:"errors,omitempty"`
|
||||
} `json:"json,omitempty"`
|
||||
}
|
||||
|
||||
|
|
|
@ -176,7 +176,7 @@ func NewClient(httpClient *http.Client, opts ...Opt) (c *Client, err error) {
|
|||
// NewRequest creates an API request.
|
||||
// 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, path string, body interface{}) (*http.Request, error) {
|
||||
func (c *Client) NewRequest(method string, path string, body interface{}) (*http.Request, error) {
|
||||
u, err := c.BaseURL.Parse(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -202,16 +202,16 @@ func (c *Client) NewRequest(method, path string, body interface{}) (*http.Reques
|
|||
return req, nil
|
||||
}
|
||||
|
||||
// NewPostForm creates an API request with a POST form.
|
||||
// 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) NewPostForm(path string, form url.Values) (*http.Request, error) {
|
||||
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(http.MethodPost, u.String(), strings.NewReader(form.Encode()))
|
||||
req, err := http.NewRequest(method, u.String(), strings.NewReader(form.Encode()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
16
multi.go
16
multi.go
|
@ -198,7 +198,7 @@ func (s *MultiService) Copy(ctx context.Context, copyRequest *MultiCopyRequest)
|
|||
|
||||
path := "api/multi/copy"
|
||||
|
||||
req, err := s.client.NewPostForm(path, copyRequest.Form())
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, copyRequest.Form())
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -220,7 +220,7 @@ func (s *MultiService) Create(ctx context.Context, createRequest *MultiCreateOrU
|
|||
|
||||
path := "api/multi"
|
||||
|
||||
req, err := s.client.NewPostForm(path, createRequest.Form())
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, createRequest.Form())
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -243,12 +243,10 @@ func (s *MultiService) Update(ctx context.Context, multiPath string, updateReque
|
|||
|
||||
path := fmt.Sprintf("api/multi/%s", multiPath)
|
||||
|
||||
req, err := s.client.NewPostForm(path, updateRequest.Form())
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPut, path, updateRequest.Form())
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
// todo: fix this
|
||||
req.Method = http.MethodPut
|
||||
|
||||
root := new(multiRoot)
|
||||
resp, err := s.client.Do(ctx, req, root)
|
||||
|
@ -296,12 +294,10 @@ 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.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPut, path, form)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
// todo: fix this
|
||||
req.Method = http.MethodPut
|
||||
|
||||
root := new(rootMultiDescription)
|
||||
resp, err := s.client.Do(ctx, req, root)
|
||||
|
@ -319,12 +315,10 @@ 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.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPut, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// todo: fix this
|
||||
req.Method = http.MethodPut
|
||||
|
||||
return s.client.Do(ctx, req, nil)
|
||||
}
|
||||
|
|
19
post.go
19
post.go
|
@ -3,6 +3,7 @@ package reddit
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
|
@ -82,7 +83,7 @@ func (s *PostService) submit(ctx context.Context, v interface{}) (*Submitted, *R
|
|||
}
|
||||
form.Set("api_type", "json")
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -104,7 +105,7 @@ func (s *PostService) EnableReplies(ctx context.Context, id string) (*Response,
|
|||
form.Set("id", id)
|
||||
form.Set("state", "true")
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -120,7 +121,7 @@ func (s *PostService) DisableReplies(ctx context.Context, id string) (*Response,
|
|||
form.Set("id", id)
|
||||
form.Set("state", "false")
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -135,7 +136,7 @@ func (s *PostService) MarkNSFW(ctx context.Context, id string) (*Response, error
|
|||
form := url.Values{}
|
||||
form.Set("id", id)
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -150,7 +151,7 @@ func (s *PostService) UnmarkNSFW(ctx context.Context, id string) (*Response, err
|
|||
form := url.Values{}
|
||||
form.Set("id", id)
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -165,7 +166,7 @@ func (s *PostService) Spoiler(ctx context.Context, id string) (*Response, error)
|
|||
form := url.Values{}
|
||||
form.Set("id", id)
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -180,7 +181,7 @@ func (s *PostService) Unspoiler(ctx context.Context, id string) (*Response, erro
|
|||
form := url.Values{}
|
||||
form.Set("id", id)
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -199,7 +200,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.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -218,7 +219,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.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package reddit
|
|||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
|
@ -25,7 +26,7 @@ func (s *PrivateMessageServiceOp) BlockUser(ctx context.Context, messageID strin
|
|||
form := url.Values{}
|
||||
form.Set("id", messageID)
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
@ -177,7 +177,8 @@ func (s *SubredditService) SearchSubredditInfo(ctx context.Context, query string
|
|||
|
||||
func (s *SubredditService) handleSubscription(ctx context.Context, form url.Values) (*Response, error) {
|
||||
path := "api/subscribe"
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
4
user.go
4
user.go
|
@ -400,7 +400,7 @@ func (s *UserService) Block(ctx context.Context, username string) (*Blocked, *Re
|
|||
form := url.Values{}
|
||||
form.Set("name", username)
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -449,7 +449,7 @@ func (s *UserService) Unblock(ctx context.Context, username string) (*Response,
|
|||
form.Set("type", "enemy")
|
||||
form.Set("container", selfID)
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
3
vote.go
3
vote.go
|
@ -3,6 +3,7 @@ package reddit
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
|
@ -29,7 +30,7 @@ func (s *VoteService) vote(ctx context.Context, id string, vote vote) (*Response
|
|||
form.Set("dir", fmt.Sprint(vote))
|
||||
form.Set("rank", "10")
|
||||
|
||||
req, err := s.client.NewPostForm(path, form)
|
||||
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue