Repository navigation
Expand file tree
/
Copy pathclient.go
More file actions
376 lines (330 loc) · 10.4 KB
/
Copy pathclient.go
File metadata and controls
376 lines (330 loc) · 10.4 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package client
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"math"
"math/rand/v2"
"net/http"
"net/http/httptrace"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/alpacahq/cli/internal/config"
"github.com/alpacahq/cli/internal/useragent"
)
const (
ExitAPIError = 1
ExitAuthError = 2
)
const maxRetries = 3
type Client struct {
HTTP *http.Client
BaseURL string
DataURL string
APIKey string
Secret string
AccessToken string
UserAgent string
Verbose bool
Debug bool
Quiet bool
Trace bool
Timeout time.Duration
}
type APIError struct {
StatusCode int `json:"status"`
Code int `json:"code"`
Message string `json:"message"`
Method string `json:"method,omitempty"`
Path string `json:"path,omitempty"`
RequestID string `json:"request_id,omitempty"`
hint string
retryAfter time.Duration
}
func (e *APIError) Error() string {
if e.Message == "" {
return fmt.Sprintf("API error (HTTP %d)", e.StatusCode)
}
if e.Code > 0 {
return fmt.Sprintf("%s [%d] (HTTP %d)", e.Message, e.Code, e.StatusCode)
}
if e.StatusCode > 0 {
return fmt.Sprintf("%s (HTTP %d)", e.Message, e.StatusCode)
}
return e.Message
}
func (e *APIError) ExitCode() int {
if e.StatusCode == 401 {
return ExitAuthError
}
return ExitAPIError
}
func (e *APIError) Hint() string {
if e.hint != "" {
return e.hint
}
switch e.StatusCode {
case 422:
return "Validation error. Check parameter values — common issues: invalid qty, bad price, unknown symbol, or missing required fields."
case 429:
return "Rate limited. Reduce request frequency or add delays between calls."
case 401:
return "Invalid credentials. Run `alpaca profile login` to re-authenticate."
case 403:
return "Forbidden. Check your permissions, account status, minimum order size, or feature availability."
}
return ""
}
var Version = "dev"
func New(cfg *config.Resolved) *Client {
return &Client{
HTTP: &http.Client{Timeout: 30 * time.Second},
BaseURL: strings.TrimRight(cfg.BaseURL, "/"),
DataURL: strings.TrimRight(cfg.DataURL, "/"),
APIKey: cfg.APIKey,
Secret: cfg.SecretKey,
AccessToken: cfg.AccessToken,
UserAgent: useragent.Build(Version),
Timeout: 30 * time.Second,
}
}
func (c *Client) SetTimeout(d time.Duration) {
c.Timeout = d
c.HTTP.Timeout = d
}
// Do sends an HTTP request against baseURL + path with optional query params
// and body. All other Client methods are thin wrappers around Do.
func (c *Client) Do(method, baseURL, path string, params url.Values, body any) (json.RawMessage, error) {
return c.DoWithHeaders(method, baseURL, path, params, nil, body)
}
// DoWithHeaders sends an HTTP request with endpoint-specific headers.
func (c *Client) DoWithHeaders(method, baseURL, path string, params url.Values, headers http.Header, body any) (json.RawMessage, error) {
u := baseURL + path
if len(params) > 0 {
u += "?" + params.Encode()
}
return c.doWithRetry(method, u, headers, body)
}
func (c *Client) Get(path string, params url.Values) (json.RawMessage, error) {
return c.Do("GET", c.BaseURL, path, params, nil)
}
func (c *Client) Post(path string, params url.Values, body any) (json.RawMessage, error) {
return c.Do("POST", c.BaseURL, path, params, body)
}
func (c *Client) Put(path string, params url.Values, body any) (json.RawMessage, error) {
return c.Do("PUT", c.BaseURL, path, params, body)
}
func (c *Client) Patch(path string, params url.Values, body any) (json.RawMessage, error) {
return c.Do("PATCH", c.BaseURL, path, params, body)
}
func (c *Client) Delete(path string, params url.Values) (json.RawMessage, error) {
return c.Do("DELETE", c.BaseURL, path, params, nil)
}
func (c *Client) GetData(path string, params url.Values) (json.RawMessage, error) {
return c.Do("GET", c.DataURL, path, params, nil)
}
func (c *Client) RawRequest(method, fullURL string, body any) (json.RawMessage, error) {
return c.doWithRetry(method, fullURL, nil, body)
}
func (c *Client) doWithRetry(method, reqURL string, headers http.Header, body any) (json.RawMessage, error) {
var lastErr error
for attempt := range maxRetries {
result, err := c.do(method, reqURL, headers, body)
if err == nil {
return result, nil
}
lastErr = err
apiErr, ok := err.(*APIError)
if !ok || !isRetryable(apiErr.StatusCode) {
return nil, err
}
delay := retryDelay(apiErr, attempt)
if c.Verbose {
fmt.Fprintf(os.Stderr, " retrying in %s (attempt %d/%d)\n", delay, attempt+1, maxRetries)
} else if !c.Quiet && apiErr.StatusCode == 429 {
fmt.Fprintf(os.Stderr, "Rate limited, retrying in %s...\n", delay)
}
time.Sleep(delay)
}
return nil, lastErr
}
func isRetryable(status int) bool {
return status == 429 || status == 500 || status == 502 || status == 503 || status == 504
}
func retryDelay(apiErr *APIError, attempt int) time.Duration {
if apiErr.StatusCode == 429 && apiErr.retryAfter > 0 {
return apiErr.retryAfter
}
base := time.Duration(math.Pow(2, float64(attempt))) * 500 * time.Millisecond
jitter := time.Duration(rand.Int64N(int64(base/2) + 1))
return base + jitter
}
func (c *Client) do(method, reqURL string, headers http.Header, body any) (json.RawMessage, error) {
start := time.Now()
var reqData []byte
var bodyReader io.Reader
if body != nil {
var err error
reqData, err = json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("encoding request body: %w", err)
}
bodyReader = bytes.NewReader(reqData)
}
req, err := http.NewRequest(method, reqURL, bodyReader)
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}
for name, values := range headers {
for _, value := range values {
req.Header.Add(name, value)
}
}
if c.AccessToken != "" {
req.Header.Set("Authorization", "Bearer "+c.AccessToken)
} else {
req.Header.Set("APCA-API-KEY-ID", c.APIKey)
req.Header.Set("APCA-API-SECRET-KEY", c.Secret)
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("User-Agent", c.UserAgent)
if c.Debug {
fmt.Fprintf(os.Stderr, "→ %s %s\n", method, c.scrub(reqURL))
c.debugHeaders("→", req.Header)
if len(reqData) > 0 {
fmt.Fprintf(os.Stderr, "→ %s\n", string(reqData))
}
}
var tt *traceTimings
if c.Trace {
tt = newTrace(os.Stderr, method, c.scrub(reqURL))
req = req.WithContext(httptrace.WithClientTrace(req.Context(), tt.clientTrace()))
}
resp, err := c.HTTP.Do(req)
if err != nil {
return nil, &APIError{
Message: fmt.Sprintf("could not reach %s: %v", c.scrub(reqURL), c.scrubErr(err)),
hint: "check your internet connection and base URL. Run `alpaca doctor` to verify configuration",
}
}
defer func() { _ = resp.Body.Close() }()
elapsed := time.Since(start)
if tt != nil {
tt.finish(os.Stderr, resp.StatusCode, elapsed)
} else if c.Verbose {
fmt.Fprintf(os.Stderr, "%s %s → %d (%dms)\n", method, c.scrub(reqURL), resp.StatusCode, elapsed.Milliseconds())
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("reading response: %w", err)
}
if c.Debug {
c.debugHeaders("←", resp.Header)
if len(respBody) > 0 {
fmt.Fprintf(os.Stderr, "← %s\n", c.scrub(string(respBody)))
}
}
if resp.StatusCode >= 400 {
apiErr := &APIError{StatusCode: resp.StatusCode, Method: method, Path: reqURL, RequestID: resp.Header.Get("X-Request-Id")}
if json.Unmarshal(respBody, apiErr) != nil || apiErr.Message == "" {
apiErr.Message = strings.TrimSpace(string(respBody))
if apiErr.Message == "" {
apiErr.Message = http.StatusText(resp.StatusCode)
}
}
if resp.StatusCode == 401 {
var probe map[string]any
if json.Unmarshal(respBody, &probe) != nil {
apiErr.hint = "Received a non-API response (possible proxy or wrong URL). Verify your base URL with `alpaca doctor`."
}
}
if resp.StatusCode == 429 {
if ra := resp.Header.Get("Retry-After"); ra != "" {
if secs, err := strconv.Atoi(ra); err == nil {
apiErr.retryAfter = time.Duration(secs) * time.Second
}
}
}
return nil, apiErr
}
if len(respBody) == 0 {
return nil, nil
}
return json.RawMessage(respBody), nil
}
func (c *Client) debugHeaders(prefix string, h http.Header) {
for name, vals := range h {
for _, v := range vals {
fmt.Fprintf(os.Stderr, "%s %s: %s\n", prefix, name, c.scrub(v))
}
}
}
func (c *Client) scrub(s string) string {
if c.APIKey != "" {
s = strings.ReplaceAll(s, c.APIKey, "[REDACTED]")
}
if c.Secret != "" {
s = strings.ReplaceAll(s, c.Secret, "[REDACTED]")
}
if c.AccessToken != "" {
s = strings.ReplaceAll(s, c.AccessToken, "[REDACTED]")
}
return s
}
func (c *Client) scrubErr(err error) error {
if err == nil {
return nil
}
msg := c.scrub(err.Error())
if msg == err.Error() {
return err
}
return fmt.Errorf("%s", msg)
}
// traceTimings prints each HTTP phase to stderr as it completes.
type traceTimings struct {
w io.Writer
dnsStart, connectStart time.Time
tlsStart, wroteRequest time.Time
gotFirstByte time.Time
}
func newTrace(w io.Writer, method, url string) *traceTimings {
fmt.Fprintf(w, "trace: %s %s\n", method, url)
return &traceTimings{w: w}
}
func (t *traceTimings) clientTrace() *httptrace.ClientTrace {
return &httptrace.ClientTrace{
DNSStart: func(_ httptrace.DNSStartInfo) { t.dnsStart = time.Now() },
DNSDone: func(_ httptrace.DNSDoneInfo) {
fmt.Fprintf(t.w, " dns: %dms\n", time.Since(t.dnsStart).Milliseconds())
},
ConnectStart: func(_, _ string) { t.connectStart = time.Now() },
ConnectDone: func(_, addr string, _ error) {
fmt.Fprintf(t.w, " tcp: %dms (%s)\n", time.Since(t.connectStart).Milliseconds(), addr)
},
TLSHandshakeStart: func() { t.tlsStart = time.Now() },
TLSHandshakeDone: func(_ tls.ConnectionState, _ error) {
fmt.Fprintf(t.w, " tls: %dms\n", time.Since(t.tlsStart).Milliseconds())
},
WroteRequest: func(_ httptrace.WroteRequestInfo) { t.wroteRequest = time.Now() },
GotFirstResponseByte: func() { t.gotFirstByte = time.Now() },
GotConn: func(info httptrace.GotConnInfo) {
if info.Reused {
fmt.Fprintf(t.w, " conn: reused (%s)\n", info.Conn.RemoteAddr())
}
},
}
}
func (t *traceTimings) finish(w io.Writer, status int, elapsed time.Duration) {
if !t.wroteRequest.IsZero() && !t.gotFirstByte.IsZero() {
fmt.Fprintf(w, " ttfb: %dms\n", t.gotFirstByte.Sub(t.wroteRequest).Milliseconds())
}
fmt.Fprintf(w, " total: %dms → %d\n", elapsed.Milliseconds(), status)
}