-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
94 lines (83 loc) · 2.86 KB
/
api.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
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
package anvil
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
)
type FillPDFPayload struct {
Data map[string]interface{} `json:"data"`
// optional
Title *string `json:"title,omitempty"`
FontFamily *string `json:"fontFamily,omitempty"`
FontSize *int `json:"fontSize,omitempty"`
TextColor *string `json:"textColor,omitempty"`
UseInteractiveFields *bool `json:"useInteractiveFields,omitempty"`
}
type GeneratePDFPayload struct {
Data string `json:"data"`
// optional
Title *string `json:"title,omitempty"`
Type *string `json:"type,omitempty"`
FontFamily *string `json:"fontFamily,omitempty"`
FontSize *int `json:"fontSize,omitempty"`
TextColor *string `json:"textColor,omitempty"`
IncludeTimestamp *bool `json:"includeTimestamp,omitempty"`
Logo *Logo `json:"logo,omitempty"`
Page *Page `json:"page,omitempty"`
}
type Logo struct {
Src *string `json:"src,omitempty"`
MaxWidth *int `json:"maxWidth,omitempty"`
MaxHeight *int `json:"maxHeight,omitempty"`
}
type Page struct {
Margin *string `json:"margin,omitempty"`
MarginTop *string `json:"marginTop,omitempty"`
MarginBottom *string `json:"marginBottom,omitempty"`
MarginLeft *string `json:"marginLeft,omitempty"`
MarginRight *string `json:"marginRight,omitempty"`
PageCount *string `json:"pageCount,omitempty"`
Width *int `json:"width,omitempty"`
Height *int `json:"height,omitempty"`
}
func (s *Anvil) restRequest(method, path string, body []byte, queryParams url.Values, numRetries int) (response *http.Response, err error) {
url := fmt.Sprintf("%s/api/%s/%s", s.BaseURL, s.RESTAPIVersion, strings.TrimLeft(path, "/"))
if len(queryParams) > 0 {
url += "?" + queryParams.Encode()
}
request, err := http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
return nil, errors.Wrap(err, "issue building request")
}
request.Header.Add("Content-Type", "application/json")
for i := 0; i < numRetries+1; i++ {
response, err := s.restClient.Do(request)
if err != nil {
return nil, errors.Wrap(err, "issue sending request")
}
if response.StatusCode >= 300 {
if response.StatusCode == http.StatusTooManyRequests {
retryAfterSec, err := strconv.Atoi(response.Header.Get("Retry-After"))
if err != nil {
retryAfterSec = 1
}
time.Sleep(time.Duration(retryAfterSec) * time.Second)
continue
}
defer response.Body.Close()
errorResponseBody, err := io.ReadAll(response.Body)
if err == nil {
return nil, errors.Errorf("status code: %d, body:\n%s", response.StatusCode, string(errorResponseBody))
}
return nil, errors.Errorf("status code: %d", response.StatusCode)
}
return response, nil
}
return nil, errors.New("rate limit exceeded")
}