Skip to content

Commit

Permalink
fix: use req-id for specific methods only
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed Oct 7, 2023
1 parent c0eb9a6 commit e71439f
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ func (c *client) newRequest(ctx context.Context, method, url string, body interf
}

var b []byte
var requestID string

if req.Method == http.MethodPost || req.Method == http.MethodPatch {
switch req.Method {
case http.MethodPut, http.MethodPost, http.MethodPatch, http.MethodDelete:
b, err = json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("failed to marshal payload: %w", err)
Expand All @@ -107,21 +109,22 @@ func (c *client) newRequest(ctx context.Context, method, url string, body interf

req.Body = io.NopCloser(bytes.NewReader(b))
req.Header.Set("DigitalSignature", string(signature))
}

requestID := RequestIdFromContext(ctx)

req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Request-Id", requestID)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
requestID = RequestIdFromContext(ctx)
req.Header.Set("X-Request-Id", requestID)
}

if c.logger != nil {
c.logger.WithContext(ctx).WithFields(logrus.Fields{
"http.request.method": req.Method,
"http.request.url": req.URL.String(),
"http.request.body.content": string(b),
"http.request.headers.request_id": requestID,
}).Debug("clearbank.client -> request")
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
return NewRequest(req), nil
}

Expand Down

0 comments on commit e71439f

Please sign in to comment.