More smooth package for http operations as a client:
- http request builder
- http response processor
- http client extension
import "github.com/x-mod/httpclient"
requestBuilder := httpclient.NewRequestBuilder(
httpclient.URL("https://url"),
httpclient.Method("GET"),
httpclient.Query("key", "value"),
httpclient.Header("key", "value"),
httpclient.BasicAuth("user", "pass"),
httpclient.Credential(*tlsconfig),
httpclient.Body(
httpclient.JSON(map[string]interface{}{
"a": "hello",
"b": true,
"c": 1,
}),
),
)
req, err := requestBuilder.Get()
//ResponseProcessor interface
type ResponseProcessor interface {
Process(context.Context, *http.Response) error
}
Implement your own ResponseProcessor
extend the http.Client with more useful interfaces:
import "github.com/x-mod/httpclient"
client := httpclient.New(
httpclient.MaxConnsPerHost(16),
httpclient.Retry(3),
httpclient.Response(
httpclient.NewDumpResponse(),
),
)
//get standard http.Client
c := client.GetClient()
//get standard http.Transport
tr := client.GetTransport()
//extension fn
err := client.Execute(context.TODO())
err := client.ExecuteRequest(context.TODO(), request)