Package list golang-github-go-kit-kit / 4c0df59
transport/http: Client constructor Peter Bourgon 6 years ago
5 changed file(s) with 69 addition(s) and 63 deletion(s). Raw diff Collapse all Expand all
506506 }
507507
508508 func makeUppercaseEndpoint(ctx context.Context, proxyURL string) endpoint.Endpoint {
509 return (httptransport.Client{
510 Client: http.DefaultClient,
511 Method: "GET",
512 URL: mustParseURL(proxyURL),
513 Context: ctx,
514 EncodeFunc: encodeUppercaseRequest,
515 DecodeFunc: decodeUppercaseResponse,
516 }).Endpoint()
509 return httptransport.NewClient(
510 "GET",
511 mustParseURL(proxyURL),
512 encodeUppercaseRequest,
513 decodeUppercaseResponse,
514 ).Endpoint()
517515 }
518516 ```
519517
2828 return client{
2929 Context: ctx,
3030 Logger: logger,
31 sum: (httptransport.Client{
32 Client: c,
33 Method: "GET",
34 URL: sumURL,
35 EncodeRequestFunc: server.EncodeSumRequest,
36 DecodeResponseFunc: server.DecodeSumResponse,
37 }).Endpoint(),
38 concat: (httptransport.Client{
39 Client: c,
40 Method: "GET",
41 URL: concatURL,
42 EncodeRequestFunc: server.EncodeConcatRequest,
43 DecodeResponseFunc: server.DecodeConcatResponse,
44 }).Endpoint(),
31 sum: httptransport.NewClient(
32 "GET",
33 sumURL,
34 server.EncodeSumRequest,
35 server.DecodeSumResponse,
36 ).Endpoint(),
37 concat: httptransport.NewClient(
38 "GET",
39 concatURL,
40 server.EncodeConcatRequest,
41 server.DecodeConcatResponse,
42 ).Endpoint(),
4543 }
4644 }
4745
22 import (
33 "errors"
44 "fmt"
5 "net/http"
65 "net/url"
76 "strings"
87 "time"
8483 if u.Path == "" {
8584 u.Path = "/uppercase"
8685 }
87 return (httptransport.Client{
88 Client: http.DefaultClient,
89 Method: "GET",
90 URL: u,
91 DecodeResponseFunc: decodeUppercaseResponse,
92 EncodeRequestFunc: encodeRequest,
93 }).Endpoint()
86 return httptransport.NewClient(
87 "GET",
88 u,
89 encodeRequest,
90 decodeUppercaseResponse,
91 ).Endpoint()
9492 }
9593
9694 func split(s string) []string {
1111
1212 // Client wraps a URL and provides a method that implements endpoint.Endpoint.
1313 type Client struct {
14 // If client is nil, http.DefaultClient will be used.
15 *http.Client
14 client *http.Client
15 method string
16 tgt *url.URL
17 enc EncodeRequestFunc
18 dec DecodeResponseFunc
19 before []RequestFunc
20 }
1621
17 // Method must be provided.
18 Method string
22 // NewClient returns a
23 func NewClient(method string, tgt *url.URL, enc EncodeRequestFunc, dec DecodeResponseFunc, options ...ClientOption) *Client {
24 c := &Client{
25 client: http.DefaultClient,
26 method: method,
27 tgt: tgt,
28 enc: enc,
29 dec: dec,
30 before: []RequestFunc{},
31 }
32 for _, option := range options {
33 option(c)
34 }
35 return c
36 }
1937
20 // URL must be provided.
21 URL *url.URL
38 // ClientOption sets an optional parameter for clients.
39 type ClientOption func(*Client)
2240
23 // EncodeRequestFunc must be provided. The HTTP request passed to the
24 // EncodeRequestFunc will have a nil body.
25 EncodeRequestFunc
41 // SetClient sets the underlying HTTP client used for requests.
42 // By default, http.DefaultClient is used.
43 func SetClient(client *http.Client) ClientOption {
44 return func(c *Client) { c.client = client }
45 }
2646
27 // DecodeResponseFunc must be provided.
28 DecodeResponseFunc
29
30 // Before functions are executed on the outgoing request after it is
31 // created, but before it's sent to the HTTP client. Clients have no After
32 // ResponseFuncs, as they don't work with ResponseWriters.
33 Before []RequestFunc
47 // SetClientBefore sets the RequestFuncs that are applied to the outgoing HTTP
48 // request before it's invoked.
49 func SetClientBefore(before ...RequestFunc) ClientOption {
50 return func(c *Client) { c.before = before }
3451 }
3552
3653 // Endpoint returns a usable endpoint that will invoke the RPC specified by
4057 ctx, cancel := context.WithCancel(ctx)
4158 defer cancel()
4259
43 req, err := http.NewRequest(c.Method, c.URL.String(), nil)
60 req, err := http.NewRequest(c.method, c.tgt.String(), nil)
4461 if err != nil {
4562 return nil, fmt.Errorf("NewRequest: %v", err)
4663 }
4764
48 if err = c.EncodeRequestFunc(req, request); err != nil {
65 if err = c.enc(req, request); err != nil {
4966 return nil, fmt.Errorf("Encode: %v", err)
5067 }
5168
52 for _, f := range c.Before {
69 for _, f := range c.before {
5370 ctx = f(ctx, req)
5471 }
5572
56 var resp *http.Response
57 if c.Client != nil {
58 resp, err = c.Client.Do(req)
59 } else {
60 resp, err = http.DefaultClient.Do(req)
61 }
73 resp, err := c.client.Do(req)
6274 if err != nil {
6375 return nil, fmt.Errorf("Do: %v", err)
6476 }
6577 defer func() { _ = resp.Body.Close() }()
6678
67 response, err := c.DecodeResponseFunc(resp)
79 response, err := c.dec(resp)
6880 if err != nil {
6981 return nil, fmt.Errorf("Decode: %v", err)
7082 }
2525 w.WriteHeader(http.StatusOK)
2626 }))
2727
28 client := httptransport.Client{
29 Method: "GET",
30 URL: mustParse(server.URL),
31 EncodeRequestFunc: encode,
32 DecodeResponseFunc: decode,
33 Before: []httptransport.RequestFunc{httptransport.SetRequestHeader(headerKey, headerVal)},
34 }
28 client := httptransport.NewClient(
29 "GET",
30 mustParse(server.URL),
31 encode,
32 decode,
33 httptransport.SetClientBefore(httptransport.SetRequestHeader(headerKey, headerVal)),
34 )
3535
3636 _, err := client.Endpoint()(context.Background(), struct{}{})
3737 if err != nil {