Skip to content

Commit

Permalink
optimize request && comments
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCai1111 committed Feb 13, 2017
1 parent 91469aa commit b0b1877
Showing 1 changed file with 48 additions and 23 deletions.
71 changes: 48 additions & 23 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func New() *Client {
cookies: make([]*http.Cookie, 0),
mwBuf: bytes.NewBuffer(nil),
}

c.mw = multipart.NewWriter(c.mwBuf)

return c
Expand All @@ -95,6 +94,26 @@ func (c *Client) To(method string, URL string) *Client {
return c
}

// Get equals To("GET", URL) .
func (c *Client) Get(URL string) *Client {
return c.To(http.MethodGet, URL)
}

// Post equals To("POST", URL) .
func (c *Client) Post(URL string) *Client {
return c.To(http.MethodPost, URL)
}

// Put equals To("PUT", URL) .
func (c *Client) Put(URL string) *Client {
return c.To(http.MethodPut, URL)
}

// Delete equals To("DELETE", URL) .
func (c *Client) Delete(URL string) *Client {
return c.To(http.MethodDelete, URL)
}

// Get equals New().Get(URL) to let you start a GET request conveniently.
func Get(URL string) *Client {
return New().Get(URL)
Expand All @@ -116,26 +135,6 @@ func Delete(URL string) *Client {
return New().Delete(URL)
}

// Get equals To("GET", URL) .
func (c *Client) Get(URL string) *Client {
return c.To(http.MethodGet, URL)
}

// Post equals To("POST", URL) .
func (c *Client) Post(URL string) *Client {
return c.To(http.MethodPost, URL)
}

// Put equals To("PUT", URL) .
func (c *Client) Put(URL string) *Client {
return c.To(http.MethodPut, URL)
}

// Delete equals To("DELETE", URL) .
func (c *Client) Delete(URL string) *Client {
return c.To(http.MethodDelete, URL)
}

// Set sets the request header entries associated with key to the single
// element value. It replaces any existing values associated with key.
func (c *Client) Set(key, value string) *Client {
Expand Down Expand Up @@ -174,17 +173,43 @@ var typesMap = map[string]string{
}

// Type sets the "Content-Type" request header to the given value.
// Some shorthands are supported:
//
// "html": "text/html"
// "json": "application/json"
// "xml": "application/xml"
// "text": "text/plain"
// "urlencoded": "application/x-www-form-urlencoded"
// "form": "application/x-www-form-urlencoded"
// "form-data": "application/x-www-form-urlencoded"
// "multipart": "multipart/form-data"
//
// So you can just call .Type("html") to set the "Content-Type"
// header to "text/html".
func (c *Client) Type(t string) *Client {
if typ, ok := typesMap[strings.ToLower(t)]; ok {
if typ, ok := typesMap[strings.TrimSpace(strings.ToLower(t))]; ok {
return c.Set(headers.ContentType, typ)
}

return c.Set(headers.ContentType, t)
}

// Accept sets the "Accept" request header to the given value.
// Some shorthands are supported:
//
// "html": "text/html"
// "json": "application/json"
// "xml": "application/xml"
// "text": "text/plain"
// "urlencoded": "application/x-www-form-urlencoded"
// "form": "application/x-www-form-urlencoded"
// "form-data": "application/x-www-form-urlencoded"
// "multipart": "multipart/form-data"
//
// So you can just call .Accept("json") to set the "Accept"
// header to "application/json".
func (c *Client) Accept(t string) *Client {
if typ, ok := typesMap[strings.ToLower(t)]; ok {
if typ, ok := typesMap[strings.TrimSpace(strings.ToLower(t))]; ok {
return c.Set(headers.Accept, typ)
}

Expand Down

0 comments on commit b0b1877

Please sign in to comment.