Skip to content

Commit

Permalink
#50 - golint, errchk, go-metalinter applied
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Dec 29, 2016
1 parent 24dc7ba commit f990acd
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 85 deletions.
42 changes: 23 additions & 19 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ import (
)

const (
// GET HTTP method
GET = "GET"
// MethodGet HTTP method
MethodGet = "GET"

// POST HTTP method
POST = "POST"
// MethodPost HTTP method
MethodPost = "POST"

// PUT HTTP method
PUT = "PUT"
// MethodPut HTTP method
MethodPut = "PUT"

// DELETE HTTP method
DELETE = "DELETE"
// MethodDelete HTTP method
MethodDelete = "DELETE"

// PATCH HTTP method
PATCH = "PATCH"
// MethodPatch HTTP method
MethodPatch = "PATCH"

// HEAD HTTP method
HEAD = "HEAD"
// MethodHead HTTP method
MethodHead = "HEAD"

// OPTIONS HTTP method
OPTIONS = "OPTIONS"
// MethodOptions HTTP method
MethodOptions = "OPTIONS"
)

var (
Expand Down Expand Up @@ -505,8 +505,8 @@ func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.Log.Printf("ERROR [%v]", err)
return nil, err
}
conn.SetDeadline(time.Now().Add(timeout))
return conn, nil
err = conn.SetDeadline(time.Now().Add(timeout))
return conn, err
}

return c
Expand Down Expand Up @@ -657,7 +657,9 @@ func (c *Client) execute(req *Request) (*Response, error) {
}

if !req.isSaveResponse {
defer resp.Body.Close()
defer func() {
_ = resp.Body.Close()
}()
response.body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return response, err
Expand Down Expand Up @@ -870,7 +872,9 @@ func addFile(w *multipart.Writer, fieldName, path string) error {
if err != nil {
return err
}
defer file.Close()
defer func() {
_ = file.Close()
}()

part, err := w.CreateFormFile(fieldName, filepath.Base(path))
if err != nil {
Expand Down Expand Up @@ -900,7 +904,7 @@ func getPointer(v interface{}) interface{} {
}

func isPayloadSupported(m string) bool {
return (m == POST || m == PUT || m == DELETE || m == PATCH)
return (m == MethodPost || m == MethodPut || m == MethodDelete || m == MethodPatch)
}

func typeOf(i interface{}) reflect.Type {
Expand Down
6 changes: 4 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,13 @@ func Example_customRootCertificate() {
func ExampleNew() {
// Creating client1
client1 := resty.New()
client1.R().Get("http://httpbin.org/get")
resp1, err1 := client1.R().Get("http://httpbin.org/get")
fmt.Println(resp1, err1)

// Creating client2
client2 := resty.New()
client2.R().Get("http://httpbin.org/get")
resp2, err2 := client2.R().Get("http://httpbin.org/get")
fmt.Println(resp2, err2)
}

//
Expand Down
20 changes: 14 additions & 6 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func parseRequestBody(c *Client, r *Request) (err error) {
if isPayloadSupported(r.Method) {

// Handling Multipart
if r.isMultiPart && !(r.Method == PATCH) {
if r.isMultiPart && !(r.Method == MethodPatch) {
if err = handleMultipart(c, r); err != nil {
return
}
Expand Down Expand Up @@ -271,7 +271,9 @@ func handleMultipart(c *Client, r *Request) (err error) {

for k, v := range c.FormData {
for _, iv := range v {
w.WriteField(k, iv)
if err = w.WriteField(k, iv); err != nil {
return err
}
}
}

Expand All @@ -283,7 +285,9 @@ func handleMultipart(c *Client, r *Request) (err error) {
return
}
} else { // form value
w.WriteField(k, iv)
if err = w.WriteField(k, iv); err != nil {
return err
}
}
}
}
Expand Down Expand Up @@ -343,7 +347,7 @@ func handleRequestBody(c *Client, r *Request) (err error) {

if reader, ok := r.Body.(io.Reader); ok {
r.bodyBuf = &bytes.Buffer{}
r.bodyBuf.ReadFrom(reader)
_, err = r.bodyBuf.ReadFrom(reader)
} else if b, ok := r.Body.([]byte); ok {
bodyBytes = b
} else if s, ok := r.Body.(string); ok {
Expand Down Expand Up @@ -390,10 +394,14 @@ func saveResponseIntoFile(c *Client, res *Response) error {
if err != nil {
return err
}
defer outFile.Close()
defer func() {
_ = outFile.Close()
}()

// io.Copy reads maximum 32kb size, it is perfect for large file download too
defer res.RawResponse.Body.Close()
defer func() {
_ = res.RawResponse.Body.Close()
}()
written, err := io.Copy(outFile, res.RawResponse.Body)
if err != nil {
return err
Expand Down
16 changes: 8 additions & 8 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,45 +383,45 @@ func (r *Request) SetProxy(proxyURL string) *Request {

// Get method does GET HTTP request. It's defined in section 4.3.1 of RFC7231.
func (r *Request) Get(url string) (*Response, error) {
return r.Execute(GET, url)
return r.Execute(MethodGet, url)
}

// Head method does HEAD HTTP request. It's defined in section 4.3.2 of RFC7231.
func (r *Request) Head(url string) (*Response, error) {
return r.Execute(HEAD, url)
return r.Execute(MethodHead, url)
}

// Post method does POST HTTP request. It's defined in section 4.3.3 of RFC7231.
func (r *Request) Post(url string) (*Response, error) {
return r.Execute(POST, url)
return r.Execute(MethodPost, url)
}

// Put method does PUT HTTP request. It's defined in section 4.3.4 of RFC7231.
func (r *Request) Put(url string) (*Response, error) {
return r.Execute(PUT, url)
return r.Execute(MethodPut, url)
}

// Delete method does DELETE HTTP request. It's defined in section 4.3.5 of RFC7231.
func (r *Request) Delete(url string) (*Response, error) {
return r.Execute(DELETE, url)
return r.Execute(MethodDelete, url)
}

// Options method does OPTIONS HTTP request. It's defined in section 4.3.7 of RFC7231.
func (r *Request) Options(url string) (*Response, error) {
return r.Execute(OPTIONS, url)
return r.Execute(MethodOptions, url)
}

// Patch method does PATCH HTTP request. It's defined in section 2 of RFC5789.
func (r *Request) Patch(url string) (*Response, error) {
return r.Execute(PATCH, url)
return r.Execute(MethodPatch, url)
}

// Execute method performs the HTTP request with given HTTP method and URL
// for current `Request`.
// resp, err := resty.R().Execute(resty.GET, "http://httpbin.org/get")
//
func (r *Request) Execute(method, url string) (*Response, error) {
if r.isMultiPart && !(method == POST || method == PUT) {
if r.isMultiPart && !(method == MethodPost || method == MethodPut) {
return nil, fmt.Errorf("Multipart content is not allowed in HTTP verb [%v]", method)
}

Expand Down
2 changes: 1 addition & 1 deletion resty.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
package resty

// Version # of go-resty
var Version = "0.9"
var Version = "0.10"
Loading

0 comments on commit f990acd

Please sign in to comment.