mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 07:26:47 +00:00
request: adds WithVerbose function to package to add verbosity to request context (#950)
* request: adds WithVerbose function to package to add verbosity to request context * request: add t.Parr.... * thrasher: nits
This commit is contained in:
@@ -18,6 +18,8 @@ import (
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
)
|
||||
|
||||
const contextVerboseFlag verbosity = "verbose"
|
||||
|
||||
var (
|
||||
// ErrRequestSystemIsNil defines and error if the request system has not
|
||||
// been set up yet.
|
||||
@@ -146,7 +148,9 @@ func (r *Requester) doRequest(ctx context.Context, endpoint EndpointLimit, newRe
|
||||
return err
|
||||
}
|
||||
|
||||
if p.Verbose {
|
||||
verbose := isVerbose(ctx, p.Verbose)
|
||||
|
||||
if verbose {
|
||||
log.Debugf(log.RequestSys, "%s attempt %d request path: %s", r.name, attempt, p.Path)
|
||||
for k, d := range req.Header {
|
||||
log.Debugf(log.RequestSys, "%s request header [%s]: %s", r.name, k, d)
|
||||
@@ -194,7 +198,7 @@ func (r *Requester) doRequest(ctx context.Context, endpoint EndpointLimit, newRe
|
||||
return fmt.Errorf("deadline would be exceeded by retry, status: %s", resp.Status)
|
||||
}
|
||||
|
||||
if p.Verbose {
|
||||
if verbose {
|
||||
log.Errorf(log.RequestSys,
|
||||
"%s request has failed. Retrying request in %s, attempt %d",
|
||||
r.name,
|
||||
@@ -255,7 +259,7 @@ func (r *Requester) doRequest(ctx context.Context, endpoint EndpointLimit, newRe
|
||||
r.name,
|
||||
err)
|
||||
}
|
||||
if p.Verbose {
|
||||
if verbose {
|
||||
log.Debugf(log.RequestSys,
|
||||
"HTTP status: %s, Code: %v",
|
||||
resp.Status,
|
||||
@@ -367,3 +371,28 @@ func (r *Requester) Shutdown() error {
|
||||
}
|
||||
return r._HTTPClient.release()
|
||||
}
|
||||
|
||||
// WithVerbose adds verbosity to a request context so that specific requests
|
||||
// can have distinct verbosity without impacting all requests.
|
||||
func WithVerbose(ctx context.Context) context.Context {
|
||||
return context.WithValue(ctx, contextVerboseFlag, true)
|
||||
}
|
||||
|
||||
// isVerbose checks main verbosity first then checks context verbose values
|
||||
// for specific request verbosity.
|
||||
func isVerbose(ctx context.Context, verbose bool) bool {
|
||||
if verbose {
|
||||
return true
|
||||
}
|
||||
|
||||
val := ctx.Value(contextVerboseFlag)
|
||||
if val == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
isCtxVerbose, ok := val.(bool)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return isCtxVerbose
|
||||
}
|
||||
|
||||
@@ -752,3 +752,32 @@ func TestGetHTTPClientUserAgent(t *testing.T) {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextVerbosity(t *testing.T) {
|
||||
t.Parallel()
|
||||
if isVerbose(context.Background(), false) {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
|
||||
if !isVerbose(context.Background(), true) {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = WithVerbose(ctx)
|
||||
if !isVerbose(ctx, false) {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
|
||||
ctx = context.Background()
|
||||
ctx = context.WithValue(ctx, contextVerboseFlag, false)
|
||||
if isVerbose(ctx, false) {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
|
||||
ctx = context.Background()
|
||||
ctx = context.WithValue(ctx, contextVerboseFlag, "bruh")
|
||||
if isVerbose(ctx, false) {
|
||||
t.Fatal("unexpected value")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,3 +74,5 @@ type RequesterOption func(*Requester)
|
||||
// being outside of receive window if application rate limiting reduces outbound
|
||||
// requests.
|
||||
type Generate func() (*Item, error)
|
||||
|
||||
type verbosity string
|
||||
|
||||
Reference in New Issue
Block a user