forked from appleboy/CodeGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
36 lines (32 loc) · 857 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package openai
import (
"net/http"
"strings"
)
// DefaultHeaderTransport is an http.RoundTripper that adds the given headers to
type DefaultHeaderTransport struct {
Origin http.RoundTripper
Header http.Header
}
// RoundTrip implements the http.RoundTripper interface.
func (t *DefaultHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
for key, values := range t.Header {
for _, value := range values {
req.Header.Add(key, value)
}
}
return t.Origin.RoundTrip(req)
}
// NewHeaders creates a new http.Header from the given slice of headers.
func NewHeaders(headers []string) http.Header {
h := make(http.Header)
for _, header := range headers {
// split header into key and value with = as delimiter
vals := strings.Split(header, "=")
if len(vals) != 2 {
continue
}
h.Add(vals[0], vals[1])
}
return h
}