Skip to content

Commit

Permalink
fix(build): Support for Custom-Header (#102) & wrap() with key version
Browse files Browse the repository at this point in the history
* Support for Custom-Header

Signed-off-by: Harshit Gupta <harshitgupta@Harshits-MacBook-Pro.local>
Signed-off-by: Harshit Gupta <harshitgupta@harshits-mbp.in.ibm.com>
  • Loading branch information
harshit777 committed Dec 14, 2022
1 parent e98c3c6 commit d6df84a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
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 {
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

0 comments on commit d6df84a

Please sign in to comment.