xonecas.io
6312
Ga

go-opencode-ai-zen-sdk / files

go-opencode-ai-zen-sdk / client.go

1.1 KB
1package zen
2
3import "net/http"
4
5type Client struct {
6	cfg        Config
7	httpClient *http.Client
8}
9
10func NewClient(cfg Config) (*Client, error) {
11	if err := cfg.applyDefaults(); err != nil {
12		return nil, err
13	}
14
15	httpClient := cfg.HTTPClient
16	if httpClient == nil {
17		// Clone the default transport so we can set ResponseHeaderTimeout
18		// without mutating the global.  Use a comma-ok assertion so we don't
19		// panic if http.DefaultTransport has been replaced (e.g. by test
20		// helpers or instrumentation libraries such as go-vcr / otelhttp).
21		var transport *http.Transport
22		if t, ok := http.DefaultTransport.(*http.Transport); ok {
23			transport = t.Clone()
24		} else {
25			transport = &http.Transport{}
26		}
27		transport.ResponseHeaderTimeout = cfg.ResponseHeaderTimeout
28		httpClient = &http.Client{
29			Transport: transport,
30			// cfg.Timeout is 0 by default (unlimited).  If the caller sets it
31			// explicitly they accept that it will terminate the body read —
32			// document this clearly in Config.Timeout's godoc.
33			Timeout: cfg.Timeout,
34		}
35	}
36
37	return &Client{
38		cfg:        cfg,
39		httpClient: httpClient,
40	}, nil
41}