mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-09-09 11:10:40 +00:00
feat: add custom http headers to openai related api backends (#1174)
* feat: add custom http headers to openai related api backends Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com> * ci: add custom headers test Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com> * add error handling Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com> * chore(deps): update docker/setup-buildx-action digest to 4fd8129 (#1173) Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com> * fix(deps): update module buf.build/gen/go/k8sgpt-ai/k8sgpt/grpc-ecosystem/gateway/v2 to v2.20.0-20240406062209-1cc152efbf5c.1 (#1147) Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com> * chore(deps): update anchore/sbom-action action to v0.16.0 (#1146) Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Alex Jones <alexsimonjones@gmail.com> Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com> * Update README.md Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com> --------- Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com> Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Alex Jones <alexsimonjones@gmail.com>
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -261,3 +262,36 @@ func FetchLatestEvent(ctx context.Context, kubernetesClient *kubernetes.Client,
|
||||
}
|
||||
return latestEvent, nil
|
||||
}
|
||||
|
||||
// NewHeaders parses a slice of strings in the format "key:value" into []http.Header
|
||||
// It handles headers with the same key by appending values
|
||||
func NewHeaders(customHeaders []string) []http.Header {
|
||||
headers := make(map[string][]string)
|
||||
|
||||
for _, header := range customHeaders {
|
||||
vals := strings.SplitN(header, ":", 2)
|
||||
if len(vals) != 2 {
|
||||
//TODO: Handle error instead of ignoring it
|
||||
continue
|
||||
}
|
||||
key := strings.TrimSpace(vals[0])
|
||||
value := strings.TrimSpace(vals[1])
|
||||
|
||||
if _, ok := headers[key]; !ok {
|
||||
headers[key] = []string{}
|
||||
}
|
||||
headers[key] = append(headers[key], value)
|
||||
}
|
||||
|
||||
// Convert map to []http.Header format
|
||||
var result []http.Header
|
||||
for key, values := range headers {
|
||||
header := make(http.Header)
|
||||
for _, value := range values {
|
||||
header.Add(key, value)
|
||||
}
|
||||
result = append(result, header)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
Reference in New Issue
Block a user