Skip to content

Commit

Permalink
Move ClientOptions to go-kit ClientOptions
Browse files Browse the repository at this point in the history
By removing our old truss-specific ClientOptions and instead using
standard go-kit ClientOptions, this allows a developer to leverage more
out-of-the-box functionality without needing to adapt truss to support
those options.

This change was made to allow a custom http.Client to be provided to
New(), allowing for a different client that http.DefaultClient.
  • Loading branch information
lelandbatey committed Sep 25, 2019
1 parent c3bd2e4 commit 7c15636
Showing 1 changed file with 6 additions and 34 deletions.
40 changes: 6 additions & 34 deletions gengokit/httptransport/templates/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,7 @@ var (
// New returns a service backed by an HTTP server living at the remote
// instance. We expect instance to come from a service discovery system, so
// likely of the form "host:port".
func New(instance string, options ...ClientOption) (pb.{{.Service.Name}}Server, error) {
var cc clientConfig
for _, f := range options {
err := f(&cc)
if err != nil {
return nil, errors.Wrap(err, "cannot apply option") }
}
{{ if .HTTPHelper.Methods }}
clientOptions := []httptransport.ClientOption{
httptransport.ClientBefore(
contextValuesToHttpHeaders(cc.headers)),
}
{{ end }}
func New(instance string, options ...httptransport.ClientOption) (pb.{{.Service.Name}}Server, error) {
if !strings.HasPrefix(instance, "http") {
instance = "http://" + instance
Expand All @@ -155,7 +141,7 @@ func New(instance string, options ...ClientOption) (pb.{{.Service.Name}}Server,
copyURL(u, "{{$binding.BasePath}}"),
EncodeHTTP{{$binding.Label}}Request,
DecodeHTTP{{$method.Name}}Response,
clientOptions...,
options...,
).Endpoint()
}
{{- end}}
Expand All @@ -179,36 +165,22 @@ func copyURL(base *url.URL, path string) *url.URL {
return &next
}
type clientConfig struct {
headers []string
}
// ClientOption is a function that modifies the client config
type ClientOption func(*clientConfig) error
// CtxValuesToSend configures the http client to pull the specified keys out of
// the context and add them to the http request as headers. Note that keys
// will have net/http.CanonicalHeaderKey called on them before being send over
// the wire and that is the form they will be available in the server context.
func CtxValuesToSend(keys ...string) ClientOption {
return func(o *clientConfig) error {
o.headers = keys
return nil
}
}
func contextValuesToHttpHeaders(keys []string) httptransport.RequestFunc {
return func(ctx context.Context, r *http.Request) context.Context {
func CtxValuesToSend(keys ...string) httptransport.ClientOption {
return httptransport.ClientBefore(func(ctx context.Context, r *http.Request) context.Context {
for _, k := range keys {
if v, ok := ctx.Value(k).(string); ok {
r.Header.Set(k, v)
}
}
return ctx
}
})
}
// HTTP Client Decode
{{range $method := .HTTPHelper.Methods}}
// DecodeHTTP{{$method.Name}}Response is a transport/http.DecodeResponseFunc that decodes
Expand Down

0 comments on commit 7c15636

Please sign in to comment.