Skip to content

Commit 433c6ec

Browse files
authored
feat(enhancement): add missing SetHeaderAuthorizationKey from the mutex implementation and the flow of it (#947)
1 parent 06301fa commit 433c6ec

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

client.go

+11
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,16 @@ func (c *Client) HeaderAuthorizationKey() string {
527527
return c.headerAuthorizationKey
528528
}
529529

530+
// SetHeaderAuthorizationKey method sets the given HTTP header name for Authorization in the client instance.
531+
//
532+
// client.SetHeaderAuthorizationKey("X-Custom-Authorization")
533+
func (c *Client) SetHeaderAuthorizationKey(k string) *Client {
534+
c.lock.Lock()
535+
defer c.lock.Unlock()
536+
c.headerAuthorizationKey = k
537+
return c
538+
}
539+
530540
// SetAuthToken method sets the auth token of the `Authorization` header for all HTTP requests.
531541
// The default auth scheme is `Bearer`; it can be customized with the method [Client.SetAuthScheme]. For Example:
532542
//
@@ -635,6 +645,7 @@ func (c *Client) R() *Request {
635645
AllowMethodGetPayload: c.allowMethodGetPayload,
636646
AllowMethodDeletePayload: c.allowMethodDeletePayload,
637647
AllowNonIdempotentRetry: c.allowNonIdempotentRetry,
648+
HeaderAuthorizationKey: c.headerAuthorizationKey,
638649

639650
client: c,
640651
baseURL: c.baseURL,

client_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,10 @@ func TestClientSettingsCoverage(t *testing.T) {
524524
c.SetAuthToken(authToken)
525525
assertEqual(t, authToken, c.AuthToken())
526526

527+
customAuthHeader := "X-Custom-Authorization"
528+
c.SetHeaderAuthorizationKey(customAuthHeader)
529+
assertEqual(t, customAuthHeader, c.HeaderAuthorizationKey())
530+
527531
c.SetCloseConnection(true)
528532

529533
c.DisableDebug()

middleware.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func addCredentials(c *Client, r *Request) error {
284284
// Build the token Auth header
285285
if !isStringEmpty(r.AuthToken) {
286286
credentialsAdded = true
287-
r.RawRequest.Header.Set(c.HeaderAuthorizationKey(), r.AuthScheme+" "+r.AuthToken)
287+
r.RawRequest.Header.Set(r.HeaderAuthorizationKey, r.AuthScheme+" "+r.AuthToken)
288288
}
289289

290290
if !c.IsDisableWarn() && credentialsAdded {

request.go

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type Request struct {
6262
IsDone bool
6363
IsSaveResponse bool
6464
Timeout time.Duration
65+
HeaderAuthorizationKey string
6566
RetryCount int
6667
RetryWaitTime time.Duration
6768
RetryMaxWaitTime time.Duration

0 commit comments

Comments
 (0)