Skip to content

Commit

Permalink
fix(transport): allow to override existing header with extra headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagriar committed Jul 5, 2023
1 parent 40b333d commit 62653fc
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions algolia/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const version = "3.30.0"
type Transport struct {
requester Requester
retryStrategy *RetryStrategy
headers map[string]string
headers http.Header
compression compression.Compression
}

Expand Down Expand Up @@ -54,16 +54,19 @@ func New(
userAgents = append([]string{extraUserAgent}, userAgents...)
}

headers := map[string]string{
headers := http.Header{}
for k, v := range map[string]string{
"Connection": "Keep-Alive",
"Content-Type": "application/json; charset=utf-8",
"User-Agent": strings.Join(userAgents, ";"),
"X-Algolia-Application-Id": appID,
"X-Algolia-API-Key": apiKey,
} {
headers.Set(k, v)
}

for k, v := range defaultHeaders {
headers[k] = v
headers.Set(k, v)
}

return &Transport{
Expand Down Expand Up @@ -191,14 +194,11 @@ func (t *Transport) request(req *http.Request) (io.ReadCloser, int, error) {
return res.Body, res.StatusCode, nil
}

func mergeHeaders(defaultHeaders, extraHeaders map[string]string) map[string]string {
headers := make(map[string]string)
func mergeHeaders(defaultHeaders http.Header, extraHeaders map[string]string) http.Header {
headers := defaultHeaders.Clone()

for key, value := range defaultHeaders {
headers[key] = value
}
for key, value := range extraHeaders {
headers[key] = value
headers.Set(key, value)
}

return headers
Expand Down Expand Up @@ -263,7 +263,7 @@ func buildRequest(
host string,
path string,
body interface{},
headers map[string]string,
headers http.Header,
urlParams map[string]string,
) (req *http.Request, err error) {
urlStr := "https://" + host + path
Expand All @@ -280,9 +280,7 @@ func buildRequest(
}

// Add headers
for k, v := range headers {
req.Header.Add(k, v)
}
req.Header = headers

// Add Content-Encoding header, if needed
if isCompressionEnabled {
Expand Down

0 comments on commit 62653fc

Please sign in to comment.