@@ -25,6 +25,7 @@ import (
2525
2626type Client struct {
2727 httpClient *http.Client
28+ streamingClient *http.Client
2829 baseURL string
2930 token string
3031 project string
@@ -44,17 +45,21 @@ func WithTimeout(timeout time.Duration) ClientOption {
4445func WithInsecureSkipVerify() ClientOption {
4546 return func(c *Client) {
4647 c.insecureSkipVerify = true
47- t, ok := c.httpClient.Transport. (*http.Transport )
48- if !ok || t == nil {
49- t = http.DefaultTransport. (*http.Transport ).Clone ()
50- } else {
51- t = t.Clone ()
48+ applyInsecure := func(hc *http.Client ) {
49+ t, ok := hc.Transport. (*http.Transport )
50+ if !ok || t == nil {
51+ t = http.DefaultTransport. (*http.Transport ).Clone ()
52+ } else {
53+ t = t.Clone ()
54+ }
55+ if t.TLSClientConfig == nil {
56+ t.TLSClientConfig = &tls.Config {MinVersion: tls.VersionTLS12 }
57+ }
58+ t.TLSClientConfig.InsecureSkipVerify = true //nolint:gosec
59+ hc.Transport = t
5260 }
53- if t.TLSClientConfig == nil {
54- t.TLSClientConfig = &tls.Config {MinVersion: tls.VersionTLS12 }
55- }
56- t.TLSClientConfig.InsecureSkipVerify = true //nolint:gosec
57- c.httpClient.Transport = t
61+ applyInsecure(c.httpClient )
62+ applyInsecure(c.streamingClient )
5863 }
5964}
6065
@@ -95,15 +100,19 @@ func NewClient(baseURL, token, project string, opts ...ClientOption) (*Client, e
95100 return nil , fmt.Errorf (" invalid base URL: %w " , err)
96101 }
97102
103+ streamingTransport := http.DefaultTransport. (*http.Transport ).Clone ()
104+ streamingTransport.DisableCompression = true
105+
98106 c := &Client{
99107 httpClient: &http.Client {
100108 Timeout: 30 * time.Second ,
101109 },
102- baseURL: strings.TrimSuffix (baseURL, " /" ),
103- token: token,
104- project: project,
105- logger: slog.Default (),
106- userAgent: " ambient-go-sdk/1.0.0" ,
110+ streamingClient: &http.Client {Transport: streamingTransport},
111+ baseURL: strings.TrimSuffix (baseURL, " /" ),
112+ token: token,
113+ project: project,
114+ logger: slog.Default (),
115+ userAgent: " ambient-go-sdk/1.0.0" ,
107116 }
108117
109118 for _, opt := range opts {
@@ -115,6 +124,10 @@ func NewClient(baseURL, token, project string, opts ...ClientOption) (*Client, e
115124 return c, nil
116125}
117126
127+ func (c *Client) Project() string {
128+ return c.project
129+ }
130+
118131func NewClientFromEnv(opts ...ClientOption ) (*Client, error) {
119132 baseURL := os.Getenv (" AMBIENT_API_URL" )
120133 if baseURL == " " {
@@ -135,6 +148,10 @@ func NewClientFromEnv(opts ...ClientOption) (*Client, error) {
135148}
136149
137150func (c *Client) do(ctx context.Context , method, path string, body []byte, expectedStatus int, result interface{}) error {
151+ return c.doMultiStatus (ctx, method, path, body, result, expectedStatus)
152+ }
153+
154+ func (c *Client) doMultiStatus(ctx context.Context , method, path string, body []byte, result interface{}, expectedStatuses ...int ) error {
138155 url := c.baseURL + " {{.Spec.BasePath}}" + path
139156
140157 req, err := http.NewRequestWithContext (ctx, method, url, nil )
@@ -175,7 +192,14 @@ func (c *Client) do(ctx context.Context, method, path string, body []byte, expec
175192 slog.Int (" body_len" , len (respBody)),
176193 )
177194
178- if resp.StatusCode != expectedStatus {
195+ statusOK := false
196+ for _, s := range expectedStatuses {
197+ if resp.StatusCode == s {
198+ statusOK = true
199+ break
200+ }
201+ }
202+ if !statusOK {
179203 var apiErr types.APIError
180204 if json.Unmarshal (respBody, &apiErr) == nil && apiErr.Code != " " {
181205 apiErr.StatusCode = resp.StatusCode
0 commit comments