Skip to content

Commit

Permalink
SetCommonResultStateCheckFunc --> SetResultStateCheckFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Jan 24, 2023
1 parent 3af71df commit f0e2325
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 54 deletions.
31 changes: 20 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,28 @@ func (c *Client) SetCommonUnknownResultHandlerFunc(fn func(resp *Response) error
return c
}

// SetCommonResultStateCheckFunc overrides the default result state checker with customized one,
// ResultState represents the state of the result.
type ResultState int

const (
// SuccessState indicates the response is in success state,
// and result will be unmarshalled if Request.SetSuccessResult
// is called.
SuccessState ResultState = iota
// ErrorState indicates the response is in error state,
// and result will be unmarshalled if Request.SetErrorResult
// or Client.SetCommonErrorResult is called.
ErrorState
// UnknownState indicates the response is in unknown state,
// and handler will be invoked if Request.SetUnknownResultHandlerFunc
// or Client.SetCommonUnknownResultHandlerFunc is called.
UnknownState
)

// SetResultStateCheckFunc overrides the default result state checker with customized one,
// which returns SuccessState when HTTP status `code >= 200 and <= 299`, and returns
// ErrorState when HTTP status `code >= 400`, otherwise returns UnknownState.
func (c *Client) SetCommonResultStateCheckFunc(fn func(resp *Response) ResultState) *Client {
func (c *Client) SetResultStateCheckFunc(fn func(resp *Response) ResultState) *Client {
c.resultStateCheckFunc = fn
return c
}
Expand Down Expand Up @@ -1362,15 +1380,6 @@ func (c *Client) roundTrip(r *Request) (resp *Response, err error) {
httpResponse, err = c.httpClient.Do(r.RawRequest)
resp.Response = httpResponse

// setup resultStateCheckFunc
if r.resultStateCheckFunc == nil {
if c.resultStateCheckFunc != nil {
r.resultStateCheckFunc = c.resultStateCheckFunc
} else {
r.resultStateCheckFunc = defaultResultStateChecker
}
}

// auto-read response body if possible
if err == nil && !c.disableAutoReadResponse && !r.isSaveResponse && !r.disableAutoReadResponse {
_, err = resp.ToBytes()
Expand Down
6 changes: 3 additions & 3 deletions client_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ func SetCommonUnknownResultHandlerFunc(fn func(resp *Response) error) *Client {
return defaultClient.SetCommonUnknownResultHandlerFunc(fn)
}

// SetCommonResultStateCheckFunc is a global wrapper methods which delegated
// SetResultStateCheckFunc is a global wrapper methods which delegated
// to the default client's SetCommonResultStateCheckFunc.
func SetCommonResultStateCheckFunc(fn func(resp *Response) ResultState) *Client {
return defaultClient.SetCommonResultStateCheckFunc(fn)
func SetResultStateCheckFunc(fn func(resp *Response) ResultState) *Client {
return defaultClient.SetResultStateCheckFunc(fn)
}

// SetCommonFormDataFromValues is a global wrapper methods which delegated
Expand Down
2 changes: 1 addition & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func parseResponseBody(c *Client, r *Response) (err error) {
return
}
req := r.Request
switch req.resultStateCheckFunc(r) {
switch r.ResultState() {
case SuccessState:
if req.Result != nil && r.StatusCode != http.StatusNoContent {
err = unmarshalBody(c, r, r.Request.Result)
Expand Down
35 changes: 4 additions & 31 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ type Request struct {
dumpBuffer *bytes.Buffer
responseReturnTime time.Time
unknownResultHandlerFunc func(resp *Response) error
resultStateCheckFunc func(resp *Response) ResultState
}

type GetContentFunc func() (io.ReadCloser, error)
Expand Down Expand Up @@ -352,7 +351,7 @@ func (r *Request) SetDownloadCallbackWithInterval(callback DownloadCallback, min
// SetResult set the result that response Body will be unmarshalled to if
// no error occurs and Response.ResultState() returns SuccessState, by default
// it requires HTTP status `code >= 200 && code <= 299`, you can also use
// Request.SetResultStateCheckFunc or Client.SetCommonResultStateCheckFunc to customize
// Request.SetResultStateCheckFunc or Client.SetResultStateCheckFunc to customize
// the result state check logic.
//
// Deprecated: Use SetSuccessResult instead.
Expand All @@ -363,7 +362,7 @@ func (r *Request) SetResult(result interface{}) *Request {
// SetSuccessResult set the result that response Body will be unmarshalled to if
// no error occurs and Response.ResultState() returns SuccessState, by default
// it requires HTTP status `code >= 200 && code <= 299`, you can also use
// Request.SetResultStateCheckFunc or Client.SetCommonResultStateCheckFunc to customize
// Request.SetResultStateCheckFunc or Client.SetResultStateCheckFunc to customize
// the result state check logic.
func (r *Request) SetSuccessResult(result interface{}) *Request {
r.Result = util.GetPointer(result)
Expand All @@ -373,7 +372,7 @@ func (r *Request) SetSuccessResult(result interface{}) *Request {
// SetError set the result that response body will be unmarshalled to if
// no error occurs and Response.ResultState() returns ErrorState, by default
// it requires HTTP status `code >= 400`, you can also use Request.SetResultStateCheckFunc
// or Client.SetCommonResultStateCheckFunc to customize the result state check logic.
// or Client.SetResultStateCheckFunc to customize the result state check logic.
//
// Deprecated: Use SetErrorResult result.
func (r *Request) SetError(error interface{}) *Request {
Expand All @@ -383,7 +382,7 @@ func (r *Request) SetError(error interface{}) *Request {
// SetErrorResult set the result that response body will be unmarshalled to if
// no error occurs and Response.ResultState() returns ErrorState, by default
// it requires HTTP status `code >= 400`, you can also use Request.SetResultStateCheckFunc
// or Client.SetCommonResultStateCheckFunc to customize the result state check logic.
// or Client.SetResultStateCheckFunc to customize the result state check logic.
func (r *Request) SetErrorResult(error interface{}) *Request {
r.Error = util.GetPointer(error)
return r
Expand All @@ -396,32 +395,6 @@ func (r *Request) SetUnknownResultHandlerFunc(fn func(resp *Response) error) *Re
return r
}

// ResultState represents the state of the result.
type ResultState int

const (
// SuccessState indicates the response is in success state,
// and result will be unmarshalled if Request.SetSuccessResult
// is called.
SuccessState ResultState = iota
// ErrorState indicates the response is in error state,
// and result will be unmarshalled if Request.SetErrorResult
// or Client.SetCommonErrorResult is called.
ErrorState
// UnknownState indicates the response is in unknown state,
// and handler will be invoked if Request.SetUnknownResultHandlerFunc
// or Client.SetCommonUnknownResultHandlerFunc is called.
UnknownState
)

// SetResultStateCheckFunc overrides the default result state checker with customized one,
// which returns SuccessState when HTTP status `code >= 200 and <= 299`, and returns
// ErrorState when HTTP status `code >= 400`, otherwise returns UnknownState.
func (r *Request) SetResultStateCheckFunc(fn func(resp *Response) ResultState) *Request {
r.resultStateCheckFunc = fn
return r
}

// SetBearerAuthToken set bearer auth token for the request.
func (r *Request) SetBearerAuthToken(token string) *Request {
return r.SetHeader("Authorization", "Bearer "+token)
Expand Down
6 changes: 0 additions & 6 deletions request_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,6 @@ func SetErrorResult(error interface{}) *Request {
return defaultClient.R().SetErrorResult(error)
}

// SetResultStateCheckFunc is a global wrapper methods which delegated
// to the default client, create a request and SetResultStateCheckFunc for request.
func SetResultStateCheckFunc(fn func(resp *Response) ResultState) *Request {
return defaultClient.R().SetResultStateCheckFunc(fn)
}

// SetUnknownResultHandlerFunc is a global wrapper methods which delegated
// to the default client, create a request and SetUnknownResultHandlerFunc for request.
func SetUnknownResultHandlerFunc(fn func(resp *Response) error) *Request {
Expand Down
10 changes: 8 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,19 @@ func (r *Response) GetContentType() string {
// ResultState returns the result state.
// By default, it returns SuccessState if HTTP status `code >= 400`, and returns
// ErrorState if HTTP status `code >= 400`, otherwise returns UnknownState.
// You can also use Request.SetResultStateCheckFunc or Client.SetCommonResultStateCheckFunc
// You can also use Request.SetResultStateCheckFunc or Client.SetResultStateCheckFunc
// to customize the result state check logic.
func (r *Response) ResultState() ResultState {
if r.Response == nil {
return UnknownState
}
return r.Request.resultStateCheckFunc(r)
var resultStateCheckFunc func(resp *Response) ResultState
if r.Request.client.resultStateCheckFunc != nil {
resultStateCheckFunc = r.Request.client.resultStateCheckFunc
} else {
resultStateCheckFunc = defaultResultStateChecker
}
return resultStateCheckFunc(r)
}

// Result returns the automatically unmarshalled object if Request.SetSuccessResult
Expand Down

0 comments on commit f0e2325

Please sign in to comment.