From 8f95f3880a36864bc59fba23bcaf0111ae7710f6 Mon Sep 17 00:00:00 2001 From: Vartan Benohanian Date: Sat, 11 Jul 2020 14:11:41 -0400 Subject: [PATCH] Add method to create request with form data Signed-off-by: Vartan Benohanian --- account.go | 4 ++-- comment.go | 11 ++++++----- errors.go | 10 +++++----- geddit.go | 8 ++++---- multi.go | 16 +++++----------- post.go | 19 ++++++++++--------- private-messages.go | 3 ++- subreddit.go | 3 ++- user.go | 4 ++-- vote.go | 3 ++- 10 files changed, 40 insertions(+), 41 deletions(-) diff --git a/account.go b/account.go index 6400e63..b9b34f0 100644 --- a/account.go +++ b/account.go @@ -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 } diff --git a/comment.go b/comment.go index 633e199..343916e 100644 --- a/comment.go +++ b/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 } diff --git a/errors.go b/errors.go index f0458ac..744a762 100644 --- a/errors.go +++ b/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"` } diff --git a/geddit.go b/geddit.go index 2f46885..90cb304 100644 --- a/geddit.go +++ b/geddit.go @@ -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 } diff --git a/multi.go b/multi.go index 4e2d0d7..bd5f85a 100644 --- a/multi.go +++ b/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) } diff --git a/post.go b/post.go index 948dded..70a902e 100644 --- a/post.go +++ b/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 } diff --git a/private-messages.go b/private-messages.go index f27b762..3799d39 100644 --- a/private-messages.go +++ b/private-messages.go @@ -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 } diff --git a/subreddit.go b/subreddit.go index 55830ec..3761ee2 100644 --- a/subreddit.go +++ b/subreddit.go @@ -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 } diff --git a/user.go b/user.go index 9752278..6729eea 100644 --- a/user.go +++ b/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 } diff --git a/vote.go b/vote.go index a20411f..5734b99 100644 --- a/vote.go +++ b/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 }