From d6df84af4c56ddcb1543eb91151942db5d5f1d28 Mon Sep 17 00:00:00 2001 From: Harshit Date: Wed, 14 Dec 2022 15:29:59 +0530 Subject: [PATCH] fix(build): Support for Custom-Header (#102) & wrap() with key version * Support for Custom-Header Signed-off-by: Harshit Gupta Signed-off-by: Harshit Gupta --- README.md | 31 +++++++++++++++++++++++++++++++ kp.go | 21 ++++++++++++++------- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3249864..e8bf8f3 100644 --- a/README.md +++ b/README.md @@ -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{} +``` \ No newline at end of file diff --git a/kp.go b/kp.go index bcadb06..976324f 100644 --- a/kp.go +++ b/kp.go @@ -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 ... @@ -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)