forked from goadesign/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
71 lines (62 loc) · 1.76 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
// Code generated by goa v2.0.8, DO NOT EDIT.
//
// fetcher client HTTP transport
//
// Command:
// $ goa gen goa.design/plugins/goakit/examples/fetcher/fetcher/design -o
// $(GOPATH)/src/goa.design/plugins/goakit/examples/fetcher/fetcher
package client
import (
"context"
"net/http"
"github.com/go-kit/kit/endpoint"
goahttp "goa.design/goa/http"
)
// Client lists the fetcher service endpoint HTTP clients.
type Client struct {
// Fetch Doer is the HTTP client used to make requests to the fetch endpoint.
FetchDoer goahttp.Doer
// RestoreResponseBody controls whether the response bodies are reset after
// decoding so they can be read again.
RestoreResponseBody bool
scheme string
host string
encoder func(*http.Request) goahttp.Encoder
decoder func(*http.Response) goahttp.Decoder
}
// NewClient instantiates HTTP clients for all the fetcher service servers.
func NewClient(
scheme string,
host string,
doer goahttp.Doer,
enc func(*http.Request) goahttp.Encoder,
dec func(*http.Response) goahttp.Decoder,
restoreBody bool,
) *Client {
return &Client{
FetchDoer: doer,
RestoreResponseBody: restoreBody,
scheme: scheme,
host: host,
decoder: dec,
encoder: enc,
}
}
// Fetch returns an endpoint that makes HTTP requests to the fetcher service
// fetch server.
func (c *Client) Fetch() endpoint.Endpoint {
var (
decodeResponse = DecodeFetchResponse(c.decoder, c.RestoreResponseBody)
)
return func(ctx context.Context, v interface{}) (interface{}, error) {
req, err := c.BuildFetchRequest(ctx, v)
if err != nil {
return nil, err
}
resp, err := c.FetchDoer.Do(req)
if err != nil {
return nil, goahttp.ErrRequestError("fetcher", "fetch", err)
}
return decodeResponse(resp)
}
}