/
web_with.go
81 lines (71 loc) · 1.66 KB
/
web_with.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
// Code generated by "genwith --client --ratelimit --package web"; DO NOT EDIT.
package web
import (
"errors"
"net/http"
"github.com/bzimmer/httpwares"
"golang.org/x/time/rate"
)
type service struct {
client *Client //nolint:golint,structcheck
}
// Option provides a configuration mechanism for a Client
type Option func(*Client) error
// NewClient creates a new client and applies all provided Options
func NewClient(opts ...Option) (*Client, error) {
c := &Client{
client: &http.Client{},
}
opts = append(opts, withServices())
for _, opt := range opts {
if err := opt(c); err != nil {
return nil, err
}
}
return c, nil
}
// WithRateLimiter rate limits the client's api calls
func WithRateLimiter(r *rate.Limiter) Option {
return func(c *Client) error {
if r == nil {
return errors.New("nil limiter")
}
c.client.Transport = &httpwares.RateLimitTransport{
Limiter: r,
Transport: c.client.Transport,
}
return nil
}
}
// WithHTTPTracing enables tracing http calls.
func WithHTTPTracing(debug bool) Option {
return func(c *Client) error {
if !debug {
return nil
}
c.client.Transport = &httpwares.VerboseTransport{
Transport: c.client.Transport,
}
return nil
}
}
// WithTransport sets the underlying http client transport.
func WithTransport(t http.RoundTripper) Option {
return func(c *Client) error {
if t == nil {
return errors.New("nil transport")
}
c.client.Transport = t
return nil
}
}
// WithHTTPClient sets the underlying http client.
func WithHTTPClient(client *http.Client) Option {
return func(c *Client) error {
if client == nil {
return errors.New("nil client")
}
c.client = client
return nil
}
}