Add method to create request with form data

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-07-11 14:11:41 -04:00
parent 4094044593
commit 8f95f3880a
10 changed files with 40 additions and 41 deletions

View file

@ -397,7 +397,7 @@ func (s *AccountService) AddTrusted(ctx context.Context, username string) (*Resp
form.Set("name", username) form.Set("name", username)
// todo: you can also do this with the user id. form.Set("id", id). should we? or is this enough? // 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 { if err != nil {
return nil, err return nil, err
} }
@ -414,7 +414,7 @@ func (s *AccountService) RemoveTrusted(ctx context.Context, username string) (*R
form.Set("name", username) form.Set("name", username)
// todo: you can also do this with the user id. form.Set("id", id). should we? or is this enough? // 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 { if err != nil {
return nil, err return nil, err
} }

View file

@ -3,6 +3,7 @@ package reddit
import ( import (
"context" "context"
"fmt" "fmt"
"net/http"
"net/url" "net/url"
"strings" "strings"
) )
@ -25,7 +26,7 @@ func (s *CommentService) Submit(ctx context.Context, id string, text string) (*C
form.Set("parent", id) form.Set("parent", id)
form.Set("text", text) form.Set("text", text)
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, nil, err 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("thing_id", id)
form.Set("text", text) form.Set("text", text)
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -80,7 +81,7 @@ func (s *CommentService) Delete(ctx context.Context, id string) (*Response, erro
form := url.Values{} form := url.Values{}
form.Set("id", id) form.Set("id", id)
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -104,7 +105,7 @@ func (s *CommentService) Save(ctx context.Context, id string) (*Response, error)
form := url.Values{} form := url.Values{}
form.Set("id", id) form.Set("id", id)
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -128,7 +129,7 @@ func (s *CommentService) Unsave(ctx context.Context, id string) (*Response, erro
form := url.Values{} form := url.Values{}
form.Set("id", id) form.Set("id", id)
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -6,19 +6,19 @@ import (
"net/http" "net/http"
) )
// RedditError is an error coming from Reddit // APIError is an error coming from Reddit
type RedditError struct { type APIError struct {
Label string Label string
Reason string Reason string
Field 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) return fmt.Sprintf("field %q caused %s: %s", e.Field, e.Label, e.Reason)
} }
// UnmarshalJSON implements the json.Unmarshaler interface. // UnmarshalJSON implements the json.Unmarshaler interface.
func (e *RedditError) UnmarshalJSON(data []byte) error { func (e *APIError) UnmarshalJSON(data []byte) error {
var info []string var info []string
err := json.Unmarshal(data, &info) err := json.Unmarshal(data, &info)
@ -43,7 +43,7 @@ type JSONErrorResponse struct {
Response *http.Response `json:"-"` Response *http.Response `json:"-"`
JSON *struct { JSON *struct {
Errors []RedditError `json:"errors,omitempty"` Errors []APIError `json:"errors,omitempty"`
} `json:"json,omitempty"` } `json:"json,omitempty"`
} }

View file

@ -176,7 +176,7 @@ func NewClient(httpClient *http.Client, opts ...Opt) (c *Client, err error) {
// NewRequest creates an API request. // NewRequest creates an API request.
// The path is the relative URL which will be resolves to the BaseURL of the Client. // 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. // 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) u, err := c.BaseURL.Parse(path)
if err != nil { if err != nil {
return nil, err return nil, err
@ -202,16 +202,16 @@ func (c *Client) NewRequest(method, path string, body interface{}) (*http.Reques
return req, nil 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. // 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. // 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) u, err := c.BaseURL.Parse(path)
if err != nil { if err != nil {
return nil, err 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 { if err != nil {
return nil, err return nil, err
} }

View file

@ -198,7 +198,7 @@ func (s *MultiService) Copy(ctx context.Context, copyRequest *MultiCopyRequest)
path := "api/multi/copy" 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 { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -220,7 +220,7 @@ func (s *MultiService) Create(ctx context.Context, createRequest *MultiCreateOrU
path := "api/multi" path := "api/multi"
req, err := s.client.NewPostForm(path, createRequest.Form()) req, err := s.client.NewRequestWithForm(http.MethodPost, path, createRequest.Form())
if err != nil { if err != nil {
return nil, nil, err 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) 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 { if err != nil {
return nil, nil, err return nil, nil, err
} }
// todo: fix this
req.Method = http.MethodPut
root := new(multiRoot) root := new(multiRoot)
resp, err := s.client.Do(ctx, req, root) 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 := url.Values{}
form.Set("model", fmt.Sprintf(`{"body_md":"%s"}`, description)) 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 { if err != nil {
return "", nil, err return "", nil, err
} }
// todo: fix this
req.Method = http.MethodPut
root := new(rootMultiDescription) root := new(rootMultiDescription)
resp, err := s.client.Do(ctx, req, root) 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 := url.Values{}
form.Set("model", fmt.Sprintf(`{"name":"%s"}`, subreddit)) 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 { if err != nil {
return nil, err return nil, err
} }
// todo: fix this
req.Method = http.MethodPut
return s.client.Do(ctx, req, nil) return s.client.Do(ctx, req, nil)
} }

19
post.go
View file

@ -3,6 +3,7 @@ package reddit
import ( import (
"context" "context"
"errors" "errors"
"net/http"
"net/url" "net/url"
"strings" "strings"
@ -82,7 +83,7 @@ func (s *PostService) submit(ctx context.Context, v interface{}) (*Submitted, *R
} }
form.Set("api_type", "json") form.Set("api_type", "json")
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -104,7 +105,7 @@ func (s *PostService) EnableReplies(ctx context.Context, id string) (*Response,
form.Set("id", id) form.Set("id", id)
form.Set("state", "true") form.Set("state", "true")
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -120,7 +121,7 @@ func (s *PostService) DisableReplies(ctx context.Context, id string) (*Response,
form.Set("id", id) form.Set("id", id)
form.Set("state", "false") form.Set("state", "false")
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -135,7 +136,7 @@ func (s *PostService) MarkNSFW(ctx context.Context, id string) (*Response, error
form := url.Values{} form := url.Values{}
form.Set("id", id) form.Set("id", id)
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -150,7 +151,7 @@ func (s *PostService) UnmarkNSFW(ctx context.Context, id string) (*Response, err
form := url.Values{} form := url.Values{}
form.Set("id", id) form.Set("id", id)
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -165,7 +166,7 @@ func (s *PostService) Spoiler(ctx context.Context, id string) (*Response, error)
form := url.Values{} form := url.Values{}
form.Set("id", id) form.Set("id", id)
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -180,7 +181,7 @@ func (s *PostService) Unspoiler(ctx context.Context, id string) (*Response, erro
form := url.Values{} form := url.Values{}
form.Set("id", id) form.Set("id", id)
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -199,7 +200,7 @@ func (s *PostService) Hide(ctx context.Context, ids ...string) (*Response, error
form := url.Values{} form := url.Values{}
form.Set("id", strings.Join(ids, ",")) 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 { if err != nil {
return nil, err return nil, err
} }
@ -218,7 +219,7 @@ func (s *PostService) Unhide(ctx context.Context, ids ...string) (*Response, err
form := url.Values{} form := url.Values{}
form.Set("id", strings.Join(ids, ",")) 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 { if err != nil {
return nil, err return nil, err
} }

View file

@ -2,6 +2,7 @@ package reddit
import ( import (
"context" "context"
"net/http"
"net/url" "net/url"
) )
@ -25,7 +26,7 @@ func (s *PrivateMessageServiceOp) BlockUser(ctx context.Context, messageID strin
form := url.Values{} form := url.Values{}
form.Set("id", messageID) form.Set("id", messageID)
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, nil return nil, nil
} }

View file

@ -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) { func (s *SubredditService) handleSubscription(ctx context.Context, form url.Values) (*Response, error) {
path := "api/subscribe" path := "api/subscribe"
req, err := s.client.NewPostForm(path, form)
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -400,7 +400,7 @@ func (s *UserService) Block(ctx context.Context, username string) (*Blocked, *Re
form := url.Values{} form := url.Values{}
form.Set("name", username) form.Set("name", username)
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -449,7 +449,7 @@ func (s *UserService) Unblock(ctx context.Context, username string) (*Response,
form.Set("type", "enemy") form.Set("type", "enemy")
form.Set("container", selfID) form.Set("container", selfID)
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -3,6 +3,7 @@ package reddit
import ( import (
"context" "context"
"fmt" "fmt"
"net/http"
"net/url" "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("dir", fmt.Sprint(vote))
form.Set("rank", "10") form.Set("rank", "10")
req, err := s.client.NewPostForm(path, form) req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
if err != nil { if err != nil {
return nil, err return nil, err
} }