github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/http/http.go (about)

     1  package http
     2  
     3  import (
     4  	"crypto/tls"
     5  	"fmt"
     6  	"net"
     7  	"net/http"
     8  	"runtime"
     9  	"time"
    10  
    11  	"github.com/IBM-Cloud/bluemix-go"
    12  )
    13  
    14  //NewHTTPClient ...
    15  func NewHTTPClient(config *bluemix.Config) *http.Client {
    16  	return &http.Client{
    17  		Transport: makeTransport(config),
    18  		Timeout:   config.HTTPTimeout,
    19  	}
    20  }
    21  
    22  func makeTransport(config *bluemix.Config) http.RoundTripper {
    23  	proxyFunc := http.ProxyFromEnvironment
    24  	if config.HTTPClient != nil && config.HTTPClient.Transport != nil {
    25  		if t, ok := config.HTTPClient.Transport.(*http.Transport); ok {
    26  			proxyFunc = t.Proxy
    27  		}
    28  	}
    29  	return NewTraceLoggingTransport(&http.Transport{
    30  		Proxy: proxyFunc,
    31  		Dial: (&net.Dialer{
    32  			Timeout:   50 * time.Second,
    33  			KeepAlive: 30 * time.Second,
    34  		}).Dial,
    35  		TLSHandshakeTimeout: 20 * time.Second,
    36  		DisableCompression:  true,
    37  		TLSClientConfig: &tls.Config{
    38  			InsecureSkipVerify: config.SSLDisable,
    39  		},
    40  	})
    41  }
    42  
    43  //UserAgent ...
    44  func UserAgent() string {
    45  	return fmt.Sprintf("Bluemix-go SDK %s / %s ", bluemix.Version, runtime.GOOS)
    46  }