-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.go
More file actions
51 lines (45 loc) · 1.41 KB
/
client.go
File metadata and controls
51 lines (45 loc) · 1.41 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
package kitty
import (
"context"
"net/http"
"net/url"
kithttp "github.com/go-kit/kit/transport/http"
)
// Client is a wrapper above the go-kit http client.
// It maps HTTP errors to Go errors.
// As the mapped error implements StatusCode, the returned status code will also be used
// as the status code returned by a go-kit HTTP endpoint.
// When using the backoff middleware, only 429 & 5XX errors trigger a retry.
type Client struct {
*kithttp.Client
}
// NewClient creates a kitty client.
func NewClient(
method string,
tgt *url.URL,
enc kithttp.EncodeRequestFunc,
dec kithttp.DecodeResponseFunc,
options ...kithttp.ClientOption,
) *Client {
return &Client{Client: kithttp.NewClient(method, tgt, enc, makeDecodeResponseFunc(dec), options...)}
}
// NewClientWithError creates a kitty client that doesn't deal with HTTP errors.
// and let you do it while you decode.
func NewClientWithError(
method string,
tgt *url.URL,
enc kithttp.EncodeRequestFunc,
dec kithttp.DecodeResponseFunc,
options ...kithttp.ClientOption,
) *Client {
return &Client{Client: kithttp.NewClient(method, tgt, enc, dec, options...)}
}
// makeDecodeResponseFunc maps HTTP errors to Go errors.
func makeDecodeResponseFunc(fn kithttp.DecodeResponseFunc) kithttp.DecodeResponseFunc {
return func(ctx context.Context, resp *http.Response) (interface{}, error) {
if err := HTTPError(resp); err != nil {
return nil, err
}
return fn(ctx, resp)
}
}