-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
119 lines (105 loc) · 3.18 KB
/
client.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
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
// package client provides functionality for building HTTP clients.
//
// Recommended Usage:
//
// type MyClient struct {
// Client *client.Client
// }
//
// type ThingInput struct {
// }
//
// type ThingOutput struct {
// }
//
// func(c MyClient) Thing(input *ThingInput) (*ThingOutput, err) {
// var output ThingOutput
// req := c.Client.NewRequest("GET", "/thing", input, output)
// err := req.Send()
// return output, err
// }
package client
import (
"context"
"net"
"net/http"
"net/url"
"time"
"github.com/remind101/pkg/client/metadata"
"github.com/remind101/pkg/client/request"
)
// Client is a request builder.
type Client struct {
Info metadata.ClientInfo // Metadata about this client.
HTTPClient *http.Client // Underlying http.Client used to make requests.
Handlers request.Handlers // Handlers to use with requests generated by this client.
}
// ClientOpt represents a function that will alter the configuration of a Client.
type ClientOpt func(*Client)
// Timeout specifies a time limit for requests made by this Client.
func Timeout(t time.Duration) ClientOpt {
return func(c *Client) {
c.HTTPClient.Timeout = t
}
}
// RoundTripper sets a custom transport on the underlying http Client.
func RoundTripper(r http.RoundTripper) ClientOpt {
return func(c *Client) {
c.HTTPClient.Transport = r
}
}
// BasicAuth adds basic auth to every request made by this client.
func BasicAuth(username, password string) ClientOpt {
return func(c *Client) {
c.Handlers.Build.Append(request.BasicAuther(username, password))
}
}
// RequestSigning adds a handler to sign requests.
func RequestSigning(id, key string) ClientOpt {
return func(c *Client) {
c.Handlers.Sign.Append(request.RequestSigner(id, key))
}
}
// DebugLogging adds logging of the enitre request and response.
func DebugLogging(c *Client) {
c.Handlers.Send.Prepend(request.RequestLogger)
c.Handlers.Send.Append(request.ResponseLogger)
}
// New returns a new client with default Handlers and http client.
func New(info metadata.ClientInfo, options ...ClientOpt) *Client {
c := &Client{
Info: info,
Handlers: request.DefaultHandlers(),
HTTPClient: &http.Client{
Timeout: 60 * time.Second,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 1 * time.Second,
KeepAlive: 90 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 3 * time.Second,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 8,
IdleConnTimeout: 90 * time.Second,
},
},
}
// Apply options
for _, option := range options {
option(c)
}
return c
}
// NewRequest builds a request from the client configuration.
// An http.Request will be initialized with the given context, method and path.
// A request.Request will be initialized with the http.Request, Handlers,
// params and data.
func (c *Client) NewRequest(ctx context.Context, method, path string, params interface{}, data interface{}) *request.Request {
httpReq, _ := http.NewRequest(method, path, nil)
httpReq = httpReq.WithContext(ctx)
httpReq.URL, _ = url.Parse(c.Info.Endpoint + path)
r := request.New(httpReq, c.Info, c.Handlers.Copy(), params, data)
r.HTTPClient = c.HTTPClient
return r
}