package
1.0.20
Repository: https://github.com/hero1s/golib.git
Documentation: pkg.go.dev

# README

Produced by OpenMix: https://openmix.org

Mix XHttp

A highly efficient HTTP library.

Installation

go get github.com/mix-go/xutil

Functions

FunctionDescription
xhttp.Request(method string, u string, opts ...RequestOption) (*Response, error)Execute an http request.
xhttp.Do(req *http.Request, opts ...RequestOption) (*Response, error)Execute an http request.
xhttp.WithBody(body Body) RequestOptionSet configuration item
xhttp.WithHeader(header http.Header) RequestOptionSet configuration item
xhttp.WithContentType(contentType string) RequestOptionSet configuration item
xhttp.WithTimeout(timeout time.Duration) RequestOptionSet configuration item
xhttp.WithDebugFunc(f DebugFunc) RequestOptionSet configuration item
xhttp.WithRetry(f RetryIfFunc, opts ...retry.Option) RequestOptionSet configuration item
xhttp.BuildJSON(v interface{}) BodyGenerate json string
xhttp.BuildQuery(m map[string]string) BodyGenerate urlencoded query string

Debug Log

By configuring DebugFunc, you can use any logging library to print log information here.

  • Global configuration
xhttp.DefaultOptions.DebugFunc = func(l *Log) {
    log.Println(l)
}
  • Single request configuration
f := func(l *Log) {
    log.Println(l)
}
xhttp.Request("POST", url, xhttp.WithDebugFunc(f))

The log object contains the following fields

type Log struct {
	Duration time.Duration `json:"duration"`
	Request  *XRequest     `json:"request"`  // The XRequest.RetryAttempts field records the number of retry attempts
	Response *XResponse    `json:"response"` // If request error this field is equal to nil
	Error    error         `json:"error"`
}

Retry

Set the conditions for determining retries, and specify various options such as the number of attempts.

url := "https://aaaaa.com/"
retryIf := func(resp *xhttp.Response, err error) error {
    if err != nil {
        return err
    }
    if resp.StatusCode != 200 {
        return fmt.Errorf("invalid status_code: %d", resp.StatusCode)
    }
    return nil
}
resp, err := xhttp.Request("GET", url, xhttp.WithRetry(retryIf, retry.Attempts(2)))

License

Apache License Version 2.0, http://www.apache.org/licenses/