Skip to content

Commit

Permalink
#57 - onbeforerequest taken care
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Feb 24, 2017
1 parent a44e0a1 commit 8ff5d9e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 100 deletions.
112 changes: 12 additions & 100 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package resty

import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
Expand Down Expand Up @@ -95,6 +94,7 @@ type Client struct {
mutex *sync.Mutex
closeConnection bool
beforeRequest []func(*Client, *Request) error
udBeforeRequest []func(*Client, *Request) error
afterResponse []func(*Client, *Response) error
}

Expand Down Expand Up @@ -310,8 +310,7 @@ func (c *Client) R() *Request {
// })
//
func (c *Client) OnBeforeRequest(m func(*Client, *Request) error) *Client {
c.beforeRequest[len(c.beforeRequest)-1] = m
c.beforeRequest = append(c.beforeRequest, requestLogger)
c.udBeforeRequest = append(c.udBeforeRequest, m)

return c
}
Expand Down Expand Up @@ -624,6 +623,16 @@ func (c *Client) SetCloseConnection(close bool) *Client {
func (c *Client) execute(req *Request) (*Response, error) {
// Apply Request middleware
var err error

// user defined on before request methods
// to modify the *resty.Request object
for _, f := range c.udBeforeRequest {
err = f(c, req)
if err != nil {
return nil, err
}
}

for _, f := range c.beforeRequest {
err = f(c, req)
if err != nil {
Expand Down Expand Up @@ -705,103 +714,6 @@ func (c *Client) IsProxySet() bool {
return c.proxyURL != nil
}

//
// Response
//

// Response is an object represents executed request and its values.
type Response struct {
Request *Request
RawResponse *http.Response

body []byte
size int64
receivedAt time.Time
}

// Body method returns HTTP response as []byte array for the executed request.
// Note: `Response.Body` might be nil, if `Request.SetOutput` is used.
func (r *Response) Body() []byte {
return r.body
}

// Status method returns the HTTP status string for the executed request.
// Example: 200 OK
func (r *Response) Status() string {
return r.RawResponse.Status
}

// StatusCode method returns the HTTP status code for the executed request.
// Example: 200
func (r *Response) StatusCode() int {
return r.RawResponse.StatusCode
}

// Result method returns the response value as an object if it has one
func (r *Response) Result() interface{} {
return r.Request.Result
}

// Error method returns the error object if it has one
func (r *Response) Error() interface{} {
return r.Request.Error
}

// Header method returns the response headers
func (r *Response) Header() http.Header {
return r.RawResponse.Header
}

// Cookies method to access all the response cookies
func (r *Response) Cookies() []*http.Cookie {
return r.RawResponse.Cookies()
}

// String method returns the body of the server response as String.
func (r *Response) String() string {
if r.body == nil {
return ""
}

return strings.TrimSpace(string(r.body))
}

// Time method returns the time of HTTP response time that from request we sent and received a request.
// See `response.ReceivedAt` to know when client recevied response and see `response.Request.Time` to know
// when client sent a request.
func (r *Response) Time() time.Duration {
return r.receivedAt.Sub(r.Request.Time)
}

// ReceivedAt method returns when response got recevied from server for the request.
func (r *Response) ReceivedAt() time.Time {
return r.receivedAt
}

// Size method returns the HTTP response size in bytes. Ya, you can relay on HTTP `Content-Length` header,
// however it won't be good for chucked transfer/compressed response. Since Resty calculates response size
// at the client end. You will get actual size of the http response.
func (r *Response) Size() int64 {
return r.size
}

func (r *Response) fmtBodyString() string {
bodyStr := "***** NO CONTENT *****"
if r.body != nil {
ct := r.Header().Get(hdrContentTypeKey)
if IsJSONType(ct) {
var out bytes.Buffer
if err := json.Indent(&out, r.body, "", " "); err == nil {
bodyStr = string(out.Bytes())
}
} else {
bodyStr = r.String()
}
}

return bodyStr
}

//
// File
//
Expand Down
3 changes: 3 additions & 0 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func New() *Client {
requestLogger,
}

// user defined request middlewares
c.udBeforeRequest = []func(*Client, *Request) error{}

// default after response middlewares
c.afterResponse = []func(*Client, *Response) error{
responseLogger,
Expand Down

0 comments on commit 8ff5d9e

Please sign in to comment.