Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Custom-Header #102

Merged
merged 4 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,34 @@ if err != nil {
}
fmt.Println(keys)
```

### Support for Adding Custom Header


1) From ServiceClient (For Every API Call)
```go
cc := kp.ClientConfig{
BaseURL: "BASE_URL",
APIKey: "API_KEY",
InstanceID: "INSTANCE_ID",
Headers: http.Header{
"Custom-Header": {"Custom-Value"},
},
}
```

2) From ServiceCall (Per API Call)

* Define Header just before the API Call and Empty out when done.

```go
client.Config.Headers = make(http.Header))
client.Config.Headers.Set("Custom-Header", "Custom-Header-Value")

key, err := client.CreateKey(params)
if err != nil {
panic(err)
}

client.Config.Headers = http.Header{}
```
21 changes: 14 additions & 7 deletions kp.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ type ctxKey string
// ClientConfig ...
type ClientConfig struct {
BaseURL string
Authorization string // The IBM Cloud (Bluemix) access token
APIKey string // Service ID API key, can be used instead of an access token
TokenURL string // The URL used to get an access token from the API key
InstanceID string // The IBM Cloud (Bluemix) instance ID that identifies your Key Protect service instance.
KeyRing string // The ID of the target Key Ring the key is associated with. It is optional but recommended for better performance.
Verbose int // See verbose values above
Timeout float64 // KP request timeout in seconds.
Authorization string // The IBM Cloud (Bluemix) access token
APIKey string // Service ID API key, can be used instead of an access token
TokenURL string // The URL used to get an access token from the API key
InstanceID string // The IBM Cloud (Bluemix) instance ID that identifies your Key Protect service instance.
KeyRing string // The ID of the target Key Ring the key is associated with. It is optional but recommended for better performance.
Verbose int // See verbose values above
Timeout float64 // KP request timeout in seconds.
Headers http.Header // Support for Custom Header
}

// DefaultTransport ...
Expand Down Expand Up @@ -255,6 +256,12 @@ func (c *Client) do(ctx context.Context, req *http.Request, res interface{}) (*h
if c.Config.KeyRing != "" {
req.Header.Set("x-kms-key-ring", c.Config.KeyRing)
}
// Adding check for Custom Header Input
if c.Config.Headers != nil {
harshit777 marked this conversation as resolved.
Show resolved Hide resolved
for key, value := range c.Config.Headers {
req.Header.Set(key, strings.Join(value, ","))
}
}

// set request up to be retryable on 500-level http codes and client errors
retryableClient := getRetryableClient(&c.HttpClient)
Expand Down