-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfront.go
61 lines (49 loc) · 1.54 KB
/
front.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
package front
import (
"context"
"net/http"
"github.com/hashicorp/go-retryablehttp"
"golang.org/x/time/rate"
)
var LimitStarter = rate.NewLimiter(rate.Limit(float64(50)/60), 1)
var LimitGrowth = rate.NewLimiter(rate.Limit(float64(100)/60), 1)
var LimitScale = rate.NewLimiter(rate.Limit(float64(200)/60), 1)
type Limiter interface {
Wait(context.Context) error
}
type transport struct {
bearer string
limiter Limiter
transport http.RoundTripper
}
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
if err := t.limiter.Wait(req.Context()); err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+t.bearer)
return t.transport.RoundTrip(req)
}
// WithFrontClient creates a Front compatible client.
//
// It handles injection of the Front bearer token, API backoff requests and rate limits.
// See https://dev.frontapp.com/docs/rate-limiting
func WithFrontClient(token string, limiter Limiter) ClientOption {
// The retryablehttp client is used to handle Retry-After headers.
client := retryablehttp.NewClient()
client.RetryMax = 1
// Injects the authorization bearer token header into every request.
client.HTTPClient.Transport = &transport{
bearer: token,
limiter: limiter,
transport: client.HTTPClient.Transport,
}
return WithHTTPClient(client.StandardClient())
}
// StringParam creates a string pointer for optional params.
func StringParam(v string) *string {
return &v
}
// BooleanParam creates a boolean pointer for optional boolean params.
func BooleanParam(v bool) *bool {
return &v
}