forked from basecamp/fizzy-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
728 lines (609 loc) · 19.1 KB
/
Copy pathclient.go
File metadata and controls
728 lines (609 loc) · 19.1 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
// Package client provides an HTTP client for the Fizzy API.
package client
import (
"bytes"
"context"
"crypto/md5"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/basecamp/cli/output"
"github.com/basecamp/fizzy-cli/internal/errors"
)
// Client is an HTTP client for the Fizzy API.
type Client struct {
BaseURL string
Token string
Account string
HTTPClient *http.Client
Verbose bool
// Sleeper is called for retry delays. Defaults to time.Sleep.
// Override in tests with a no-op or recording function.
Sleeper func(time.Duration)
}
// APIResponse represents a response from the API.
type APIResponse struct {
StatusCode int
Body []byte
Location string
LinkNext string
Data any
}
// New creates a new API client.
func New(baseURL, token, account string) *Client {
return &Client{
BaseURL: strings.TrimSuffix(baseURL, "/"),
Token: token,
Account: account,
HTTPClient: &http.Client{
Timeout: 30 * time.Second,
},
}
}
// buildURL constructs the full API URL.
func (c *Client) buildURL(path string) string {
// If path already starts with http, use as-is
if strings.HasPrefix(path, "http") {
return path
}
// Ensure path starts with /
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
// Insert account into path if not present
if c.Account != "" {
accountPrefix := "/" + c.Account + "/"
if !strings.HasPrefix(path, accountPrefix) && path != "/"+c.Account {
path = "/" + c.Account + path
}
}
return c.BaseURL + path
}
// Get performs a GET request.
func (c *Client) Get(path string) (*APIResponse, error) {
return c.request("GET", path, nil)
}
// Post performs a POST request with JSON body.
func (c *Client) Post(path string, body any) (*APIResponse, error) {
return c.request("POST", path, body)
}
// Patch performs a PATCH request with JSON body.
func (c *Client) Patch(path string, body any) (*APIResponse, error) {
return c.request("PATCH", path, body)
}
// PatchMultipart performs a PATCH request with multipart form data.
func (c *Client) PatchMultipart(path, fileField, filePath string, fields map[string]string) (*APIResponse, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, errors.NewError(fmt.Sprintf("Failed to open file: %v", err))
}
defer func() { _ = file.Close() }()
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
part, err := writer.CreateFormFile(fileField, filepath.Base(filePath))
if err != nil {
return nil, errors.NewError(fmt.Sprintf("Failed to create form file: %v", err))
}
if _, err = io.Copy(part, file); err != nil {
return nil, errors.NewError(fmt.Sprintf("Failed to copy file: %v", err))
}
for key, value := range fields {
if err = writer.WriteField(key, value); err != nil {
return nil, errors.NewError(fmt.Sprintf("Failed to write form field: %v", err))
}
}
if err = writer.Close(); err != nil {
return nil, errors.NewError(fmt.Sprintf("Failed to finalize multipart body: %v", err))
}
reqURL := c.buildURL(path)
req, err := http.NewRequestWithContext(context.Background(), "PATCH", reqURL, &buf)
if err != nil {
return nil, errors.NewNetworkError(fmt.Sprintf("Failed to create request: %v", err))
}
c.setHeaders(req)
req.Header.Set("Content-Type", writer.FormDataContentType())
resp, err := c.doWithRetry(req)
if err != nil {
return nil, errors.NewNetworkError(fmt.Sprintf("Request failed: %v", err))
}
defer func() { _ = resp.Body.Close() }()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.NewNetworkError(fmt.Sprintf("Failed to read response: %v", err))
}
apiResp := &APIResponse{
StatusCode: resp.StatusCode,
Body: respBody,
Location: resp.Header.Get("Location"),
}
if len(respBody) > 0 {
if err := json.Unmarshal(respBody, &apiResp.Data); err != nil {
return apiResp, nil
}
}
if resp.StatusCode >= 400 {
return apiResp, c.errorFromResponse(resp.StatusCode, respBody, resp.Header)
}
return apiResp, nil
}
// Put performs a PUT request with JSON body.
func (c *Client) Put(path string, body any) (*APIResponse, error) {
return c.request("PUT", path, body)
}
// Delete performs a DELETE request.
func (c *Client) Delete(path string) (*APIResponse, error) {
return c.request("DELETE", path, nil)
}
// GetHTML performs a GET request expecting an HTML response.
// Unlike Get, it sets Accept: text/html and does not attempt JSON parsing.
func (c *Client) GetHTML(path string) (*APIResponse, error) {
requestURL := c.buildURL(path)
req, err := http.NewRequestWithContext(context.Background(), "GET", requestURL, nil)
if err != nil {
return nil, errors.NewNetworkError(fmt.Sprintf("Failed to create request: %v", err))
}
c.setHeaders(req)
req.Header.Set("Accept", "text/html")
if c.Verbose {
fmt.Fprintf(os.Stderr, "> GET %s (HTML)\n", requestURL)
}
resp, err := c.doWithRetry(req)
if err != nil {
return nil, errors.NewNetworkError(fmt.Sprintf("Request failed: %v", err))
}
defer func() { _ = resp.Body.Close() }()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.NewNetworkError(fmt.Sprintf("Failed to read response: %v", err))
}
if c.Verbose {
fmt.Fprintf(os.Stderr, "< %d %s\n", resp.StatusCode, http.StatusText(resp.StatusCode))
}
apiResp := &APIResponse{
StatusCode: resp.StatusCode,
Body: respBody,
}
if resp.StatusCode >= 400 {
return apiResp, c.errorFromResponse(resp.StatusCode, respBody, resp.Header)
}
return apiResp, nil
}
func (c *Client) request(method, path string, body any) (*APIResponse, error) {
requestURL := c.buildURL(path)
var reqBody io.Reader
if body != nil {
jsonBody, err := json.Marshal(body)
if err != nil {
return nil, errors.NewError(fmt.Sprintf("Failed to marshal request body: %v", err))
}
reqBody = bytes.NewReader(jsonBody)
}
req, err := http.NewRequestWithContext(context.Background(), method, requestURL, reqBody)
if err != nil {
return nil, errors.NewNetworkError(fmt.Sprintf("Failed to create request: %v", err))
}
c.setHeaders(req)
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
if c.Verbose {
fmt.Fprintf(os.Stderr, "> %s %s\n", method, requestURL)
}
resp, err := c.doWithRetry(req)
if err != nil {
return nil, errors.NewNetworkError(fmt.Sprintf("Request failed: %v", err))
}
defer func() { _ = resp.Body.Close() }()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.NewNetworkError(fmt.Sprintf("Failed to read response: %v", err))
}
if c.Verbose {
fmt.Fprintf(os.Stderr, "< %d %s\n", resp.StatusCode, http.StatusText(resp.StatusCode))
}
apiResp := &APIResponse{
StatusCode: resp.StatusCode,
Body: respBody,
Location: resp.Header.Get("Location"),
LinkNext: parseLinkNext(resp.Header.Get("Link")),
}
// Check for error status codes before parsing JSON,
// since error responses may not be JSON (e.g. HTML 401 pages)
if resp.StatusCode >= 400 {
return apiResp, c.errorFromResponse(resp.StatusCode, respBody, resp.Header)
}
// Parse JSON body if present
if len(respBody) > 0 {
if err := json.Unmarshal(respBody, &apiResp.Data); err != nil {
return apiResp, errors.NewError(fmt.Sprintf("Failed to parse JSON response: %v", err))
}
}
return apiResp, nil
}
func (c *Client) setHeaders(req *http.Request) {
req.Header.Set("Authorization", "Bearer "+c.Token)
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "fizzy-cli/1.0")
}
func (c *Client) sleep(d time.Duration) {
if c.Sleeper != nil {
c.Sleeper(d)
} else {
time.Sleep(d)
}
}
const maxRetries = 3
var linkNextRe = regexp.MustCompile(`<([^>]+)>;\s*rel="next"`)
// doWithRetry wraps HTTPClient.Do with retry logic for 429 and 5xx responses.
// Only retries GET/DELETE/PUT on 5xx; POST/PATCH only retry on 429.
func (c *Client) doWithRetry(req *http.Request) (*http.Response, error) {
idempotent := req.Method == "GET" || req.Method == "DELETE" || req.Method == "PUT"
for attempt := range maxRetries + 1 {
resp, err := c.HTTPClient.Do(req)
// Network error — retry idempotent methods with backoff
if err != nil {
if attempt < maxRetries && idempotent {
c.sleep(time.Duration(1<<uint(attempt)) * time.Second)
resetBody(req)
continue
}
return nil, err
}
// 429: always retry (server explicitly says "try again")
if resp.StatusCode == 429 {
if attempt < maxRetries {
delay := parseRetryAfter(resp.Header.Get("Retry-After"))
_ = resp.Body.Close()
c.sleep(delay)
resetBody(req)
continue
}
}
// 5xx: retry idempotent methods with exponential backoff
if resp.StatusCode >= 500 && idempotent && attempt < maxRetries {
_ = resp.Body.Close()
c.sleep(time.Duration(1<<uint(attempt)) * time.Second)
resetBody(req)
continue
}
return resp, nil
}
// unreachable, but the compiler needs it
return c.HTTPClient.Do(req)
}
// resetBody rewinds the request body for retry. Uses GetBody (set automatically
// by http.NewRequestWithContext for *bytes.Reader, *bytes.Buffer, *strings.Reader).
func resetBody(req *http.Request) {
if req.GetBody != nil {
req.Body, _ = req.GetBody()
}
}
// parseRetryAfter parses the Retry-After header value as seconds.
// Returns a default of 1 second if the header is missing or unparseable.
func parseRetryAfter(value string) time.Duration {
const maxRetryDelay = 300 // 5 minutes; anything larger is almost certainly bogus
if value == "" {
return time.Second
}
if seconds, err := strconv.Atoi(value); err == nil && seconds > 0 {
if seconds > maxRetryDelay {
seconds = maxRetryDelay
}
return time.Duration(seconds) * time.Second
}
// Try HTTP-date format
if t, err := http.ParseTime(value); err == nil {
delay := time.Until(t)
if delay > 0 {
if delay > maxRetryDelay*time.Second {
return maxRetryDelay * time.Second
}
return delay
}
}
return time.Second
}
func (c *Client) errorFromResponse(status int, body []byte, header http.Header) error {
// Try to parse error message from response
var errResp struct {
Error string `json:"error"`
}
message := http.StatusText(status)
if json.Unmarshal(body, &errResp) == nil && errResp.Error != "" {
message = errResp.Error
}
if status == 429 {
retryAfter := 0
if ra := header.Get("Retry-After"); ra != "" {
if seconds, err := strconv.Atoi(ra); err == nil {
retryAfter = seconds
}
}
e := errors.FromHTTPStatus(429, message)
if retryAfter > 0 {
e = output.ErrRateLimit(retryAfter)
if message != "" && message != http.StatusText(http.StatusTooManyRequests) {
e.Message = message
}
}
return e
}
return errors.FromHTTPStatus(status, message)
}
// parseLinkNext extracts the "next" URL from a Link header.
func parseLinkNext(linkHeader string) string {
if linkHeader == "" {
return ""
}
matches := linkNextRe.FindStringSubmatch(linkHeader)
if len(matches) > 1 {
return matches[1]
}
return ""
}
// GetWithPagination fetches all pages of a paginated endpoint.
func (c *Client) GetWithPagination(path string, fetchAll bool) (*APIResponse, error) {
resp, err := c.Get(path)
if err != nil {
return resp, err
}
if !fetchAll || resp.LinkNext == "" {
return resp, nil
}
// Collect all data
var allData []any
if arr, ok := resp.Data.([]any); ok {
allData = append(allData, arr...)
}
// Fetch remaining pages
nextURL := resp.LinkNext
for nextURL != "" {
pageResp, err := c.Get(nextURL)
if err != nil {
return nil, err
}
if arr, ok := pageResp.Data.([]any); ok {
allData = append(allData, arr...)
}
nextURL = pageResp.LinkNext
}
resp.Data = allData
resp.LinkNext = ""
return resp, nil
}
// FollowLocation fetches the resource at the Location header.
func (c *Client) FollowLocation(location string) (*APIResponse, error) {
if location == "" {
return nil, nil
}
return c.Get(location)
}
// UploadFile uploads a file using the direct upload flow.
func (c *Client) UploadFile(filePath string) (*APIResponse, error) {
// Open the file
file, err := os.Open(filePath)
if err != nil {
return nil, errors.NewError(fmt.Sprintf("Failed to open file: %v", err))
}
defer func() { _ = file.Close() }()
// Get file info
fileInfo, err := file.Stat()
if err != nil {
return nil, errors.NewError(fmt.Sprintf("Failed to stat file: %v", err))
}
// Read file content for checksum
fileContent, err := io.ReadAll(file)
if err != nil {
return nil, errors.NewError(fmt.Sprintf("Failed to read file: %v", err))
}
filename := filepath.Base(filePath)
contentType := detectContentType(filePath)
checksum := computeChecksum(fileContent)
// Step 1: Create blob
blobReq := map[string]any{
"blob": map[string]any{
"filename": filename,
"byte_size": fileInfo.Size(),
"content_type": contentType,
"checksum": checksum,
},
}
createResp, err := c.Post("/rails/active_storage/direct_uploads", blobReq)
if err != nil {
return nil, err
}
// Parse the response to get upload URL and signed_id
blobData, ok := createResp.Data.(map[string]any)
if !ok {
return nil, errors.NewError("Invalid blob creation response")
}
directUploadData, ok := blobData["direct_upload"].(map[string]any)
if !ok {
return nil, errors.NewError("Missing direct_upload in response")
}
uploadURL, ok := directUploadData["url"].(string)
if !ok {
return nil, errors.NewError("Missing upload URL in response")
}
headers, _ := directUploadData["headers"].(map[string]any)
signedID, ok := blobData["signed_id"].(string)
if !ok {
return nil, errors.NewError("Missing signed_id in response")
}
// Get attachable_sgid for use in action-text-attachment
attachableSGID, _ := blobData["attachable_sgid"].(string)
// Step 2: Upload file to the direct upload URL
uploadReq, err := http.NewRequestWithContext(context.Background(), "PUT", uploadURL, bytes.NewReader(fileContent))
if err != nil {
return nil, errors.NewNetworkError(fmt.Sprintf("Failed to create upload request: %v", err))
}
// Set headers from the direct_upload response
for key, value := range headers {
if strVal, ok := value.(string); ok {
uploadReq.Header.Set(key, strVal)
}
}
uploadResp, err := c.doWithRetry(uploadReq)
if err != nil {
return nil, errors.NewNetworkError(fmt.Sprintf("Upload failed: %v", err))
}
defer func() { _ = uploadResp.Body.Close() }()
if uploadResp.StatusCode >= 400 {
body, _ := io.ReadAll(uploadResp.Body)
return nil, errors.NewError(fmt.Sprintf("Upload failed: %d %s", uploadResp.StatusCode, string(body)))
}
// Return the signed_id and attachable_sgid
responseData := map[string]any{
"signed_id": signedID,
}
if attachableSGID != "" {
responseData["attachable_sgid"] = attachableSGID
}
return &APIResponse{
StatusCode: 200,
Data: responseData,
}, nil
}
// UploadFileMultipart uploads a file using multipart form data.
func (c *Client) UploadFileMultipart(path, fieldName, filePath string, extraFields map[string]string) (*APIResponse, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, errors.NewError(fmt.Sprintf("Failed to open file: %v", err))
}
defer func() { _ = file.Close() }()
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
// Add the file
part, err := writer.CreateFormFile(fieldName, filepath.Base(filePath))
if err != nil {
return nil, errors.NewError(fmt.Sprintf("Failed to create form file: %v", err))
}
if _, err = io.Copy(part, file); err != nil {
return nil, errors.NewError(fmt.Sprintf("Failed to copy file: %v", err))
}
// Add extra fields
for key, value := range extraFields {
if err = writer.WriteField(key, value); err != nil {
return nil, errors.NewError(fmt.Sprintf("Failed to write form field: %v", err))
}
}
if err = writer.Close(); err != nil {
return nil, errors.NewError(fmt.Sprintf("Failed to finalize multipart body: %v", err))
}
reqURL := c.buildURL(path)
req, err := http.NewRequestWithContext(context.Background(), "POST", reqURL, &buf)
if err != nil {
return nil, errors.NewNetworkError(fmt.Sprintf("Failed to create request: %v", err))
}
c.setHeaders(req)
req.Header.Set("Content-Type", writer.FormDataContentType())
resp, err := c.doWithRetry(req)
if err != nil {
return nil, errors.NewNetworkError(fmt.Sprintf("Request failed: %v", err))
}
defer func() { _ = resp.Body.Close() }()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.NewNetworkError(fmt.Sprintf("Failed to read response: %v", err))
}
apiResp := &APIResponse{
StatusCode: resp.StatusCode,
Body: respBody,
Location: resp.Header.Get("Location"),
}
if len(respBody) > 0 {
if err := json.Unmarshal(respBody, &apiResp.Data); err != nil {
return apiResp, errors.NewError(fmt.Sprintf("Failed to parse JSON response: %v", err))
}
}
if resp.StatusCode >= 400 {
return apiResp, c.errorFromResponse(resp.StatusCode, respBody, resp.Header)
}
return apiResp, nil
}
// computeChecksum computes the base64-encoded MD5 checksum of content.
func computeChecksum(content []byte) string {
hash := md5.Sum(content)
return base64.StdEncoding.EncodeToString(hash[:])
}
func detectContentType(filePath string) string {
ext := strings.ToLower(filepath.Ext(filePath))
contentTypes := map[string]string{
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".gif": "image/gif",
".webp": "image/webp",
".svg": "image/svg+xml",
".pdf": "application/pdf",
".txt": "text/plain",
".html": "text/html",
".json": "application/json",
".xml": "application/xml",
".zip": "application/zip",
}
if ct, ok := contentTypes[ext]; ok {
return ct
}
return "application/octet-stream"
}
// ParsePage extracts page number from a URL query string.
func ParsePage(nextURL string) string {
if nextURL == "" {
return ""
}
u, err := url.Parse(nextURL)
if err != nil {
return ""
}
return u.Query().Get("page")
}
// DownloadFile downloads a file from a URL (following redirects) and saves it to the specified path.
// The URL should be a relative path like /6085671/rails/active_storage/blobs/redirect/...
func (c *Client) DownloadFile(urlPath string, destPath string) error {
requestURL := c.buildURL(urlPath)
req, err := http.NewRequestWithContext(context.Background(), "GET", requestURL, nil)
if err != nil {
return errors.NewNetworkError(fmt.Sprintf("Failed to create request: %v", err))
}
req.Header.Set("Authorization", "Bearer "+c.Token)
req.Header.Set("User-Agent", "fizzy-cli/1.0")
if c.Verbose {
fmt.Fprintf(os.Stderr, "> GET %s\n", requestURL)
}
resp, err := c.doWithRetry(req)
if err != nil {
return errors.NewNetworkError(fmt.Sprintf("Request failed: %v", err))
}
defer func() { _ = resp.Body.Close() }()
if c.Verbose {
fmt.Fprintf(os.Stderr, "< %d %s\n", resp.StatusCode, http.StatusText(resp.StatusCode))
}
if resp.StatusCode >= 400 {
body, _ := io.ReadAll(resp.Body)
return errors.NewError(fmt.Sprintf("Download failed: %d %s", resp.StatusCode, string(body)))
}
// Create the destination file
out, err := os.Create(destPath)
if err != nil {
return errors.NewError(fmt.Sprintf("Failed to create file: %v", err))
}
// Copy the response body to the file
_, err = io.Copy(out, resp.Body)
if err != nil {
_ = out.Close()
_ = os.Remove(destPath)
return errors.NewError(fmt.Sprintf("Failed to write file: %v", err))
}
return out.Close()
}