Skip to content

Commit

Permalink
feat(enhancement): add missing SetHeaderAuthorizationKey from the mut…
Browse files Browse the repository at this point in the history
…ex implementation and the flow of it (#947)
  • Loading branch information
jeevatkm authored Jan 10, 2025
1 parent 06301fa commit 433c6ec
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
11 changes: 11 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,16 @@ func (c *Client) HeaderAuthorizationKey() string {
return c.headerAuthorizationKey
}

// SetHeaderAuthorizationKey method sets the given HTTP header name for Authorization in the client instance.
//
// client.SetHeaderAuthorizationKey("X-Custom-Authorization")
func (c *Client) SetHeaderAuthorizationKey(k string) *Client {
c.lock.Lock()
defer c.lock.Unlock()
c.headerAuthorizationKey = k
return c
}

// SetAuthToken method sets the auth token of the `Authorization` header for all HTTP requests.
// The default auth scheme is `Bearer`; it can be customized with the method [Client.SetAuthScheme]. For Example:
//
Expand Down Expand Up @@ -635,6 +645,7 @@ func (c *Client) R() *Request {
AllowMethodGetPayload: c.allowMethodGetPayload,
AllowMethodDeletePayload: c.allowMethodDeletePayload,
AllowNonIdempotentRetry: c.allowNonIdempotentRetry,
HeaderAuthorizationKey: c.headerAuthorizationKey,

client: c,
baseURL: c.baseURL,
Expand Down
4 changes: 4 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,10 @@ func TestClientSettingsCoverage(t *testing.T) {
c.SetAuthToken(authToken)
assertEqual(t, authToken, c.AuthToken())

customAuthHeader := "X-Custom-Authorization"
c.SetHeaderAuthorizationKey(customAuthHeader)
assertEqual(t, customAuthHeader, c.HeaderAuthorizationKey())

c.SetCloseConnection(true)

c.DisableDebug()
Expand Down
2 changes: 1 addition & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func addCredentials(c *Client, r *Request) error {
// Build the token Auth header
if !isStringEmpty(r.AuthToken) {
credentialsAdded = true
r.RawRequest.Header.Set(c.HeaderAuthorizationKey(), r.AuthScheme+" "+r.AuthToken)
r.RawRequest.Header.Set(r.HeaderAuthorizationKey, r.AuthScheme+" "+r.AuthToken)
}

if !c.IsDisableWarn() && credentialsAdded {
Expand Down
1 change: 1 addition & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Request struct {
IsDone bool
IsSaveResponse bool
Timeout time.Duration
HeaderAuthorizationKey string
RetryCount int
RetryWaitTime time.Duration
RetryMaxWaitTime time.Duration
Expand Down

0 comments on commit 433c6ec

Please sign in to comment.