github.com/vseinstrumentiru/lego@v1.0.2/pkg/lehttp/client.go (about)

     1  package lehttp
     2  
     3  import (
     4  	"crypto/tls"
     5  	"emperror.dev/emperror"
     6  	"emperror.dev/errors"
     7  	"github.com/go-kit/kit/transport/http/jsonrpc"
     8  	"github.com/go-resty/resty/v2"
     9  	"github.com/shurcooL/graphql"
    10  	"github.com/vseinstrumentiru/lego/internal/lego/monitor/propagation"
    11  	"go.opencensus.io/plugin/ochttp"
    12  	"go.opencensus.io/trace"
    13  	"net/http"
    14  	"net/url"
    15  )
    16  
    17  type ClientOption func(*http.Client)
    18  
    19  func SetSpanNameFormatter(formatter func(req *http.Request) string) ClientOption {
    20  	return func(client *http.Client) {
    21  		if octr, ok := client.Transport.(*ochttp.Transport); ok {
    22  			octr.FormatSpanName = formatter
    23  		}
    24  	}
    25  }
    26  
    27  func SetTraceSampler(sampler trace.Sampler) ClientOption {
    28  	return func(client *http.Client) {
    29  		if octr, ok := client.Transport.(*ochttp.Transport); ok {
    30  			octr.StartOptions.Sampler = sampler
    31  		}
    32  	}
    33  }
    34  
    35  func InsecureClient() ClientOption {
    36  	return func(client *http.Client) {
    37  		var httpTr *http.Transport
    38  		if octr, ok := client.Transport.(*ochttp.Transport); ok {
    39  			if octr.Base == nil {
    40  				octr.Base = http.DefaultTransport
    41  			}
    42  
    43  			httpTr = octr.Base.(*http.Transport)
    44  		} else if httpTr, ok = client.Transport.(*http.Transport); !ok {
    45  			return
    46  		}
    47  
    48  		if httpTr == nil {
    49  			return
    50  		}
    51  
    52  		if httpTr.TLSClientConfig == nil {
    53  			httpTr.TLSClientConfig = &tls.Config{}
    54  		}
    55  
    56  		httpTr.TLSClientConfig.InsecureSkipVerify = true
    57  	}
    58  }
    59  
    60  func NewClient(name string, opts ...ClientOption) *http.Client {
    61  	c := &http.Client{
    62  		Transport: &ochttp.Transport{
    63  			Base:        http.DefaultTransport,
    64  			Propagation: propagation.DefaultHTTPFormat,
    65  			StartOptions: trace.StartOptions{
    66  				Sampler: trace.AlwaysSample(),
    67  			},
    68  			FormatSpanName: func(req *http.Request) string {
    69  				return name + ":" + req.URL.Path
    70  			},
    71  		},
    72  	}
    73  
    74  	for _, opt := range opts {
    75  		opt(c)
    76  	}
    77  
    78  	return c
    79  }
    80  
    81  func NewRestyClient(name string, host string, opts ...ClientOption) *resty.Client {
    82  	return resty.NewWithClient(NewClient(name, opts...)).SetHostURL(host)
    83  }
    84  
    85  func NewGraphQLClient(name string, endpoint string, opts ...ClientOption) *graphql.Client {
    86  	return graphql.NewClient(endpoint, NewClient(name))
    87  }
    88  
    89  func NewJsonRPCClient(addr, method string, client *http.Client, option ...jsonrpc.ClientOption) *jsonrpc.Client {
    90  	u, err := url.Parse(addr)
    91  	emperror.Panic(errors.Wrap(err, "can't create JsonRPC client"))
    92  
    93  	option = append([]jsonrpc.ClientOption{jsonrpc.SetClient(client)}, option...)
    94  
    95  	return jsonrpc.NewClient(u, method, option...)
    96  }