-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
48 lines (43 loc) · 1.15 KB
/
config.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
37
38
39
40
41
42
43
44
45
46
47
48
package openai
import (
"io"
"net/http"
"net/url"
)
type ClientConfig struct {
APIKey string
Org string
Proxy string
HttpRequestFactory RequestFactory
HttpFielFormFactory func(body io.Writer) FormFactory
}
func NewClientWithConfig(config *ClientConfig) *Client {
// Create a new instance of the Client struct with the given parameters
client := &Client{
apiKey: config.APIKey,
org: config.Org,
proxyUrl: config.Proxy,
requestFactory: config.HttpRequestFactory,
formFactory: config.HttpFielFormFactory,
}
// If a proxy URL was provided, set up an HTTP client with the proxy details
if len(client.proxyUrl) != 0 {
// Parse the proxy URL into a url.URL struct
proxyUrl, err := url.Parse(client.proxyUrl)
if err != nil {
// If there was an error parsing the URL, panic
panic(err)
}
// Set up the HTTP client with the proxy details
client.HttpClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyUrl),
},
}
} else {
// Otherwise, set up a regular HTTP client
client.HttpClient = &http.Client{}
}
// Return the newly created client instance
return client
}