Skip to content

Repository files navigation

Mix XHTTP

A highly efficient HTTP library.

Installation

go get github.com/mix-go/xhttp

Functions

FunctionDescription
xhttp.Fetch(ctx context.Context, method string, u string, opts ...RequestOption) (*Response, error)Send a http request.
xhttp.NewRequest(method string, u string, opts ...RequestOption) (*Request, error)Create a request object.
xhttp.Do(ctx context.Context, req *Request, opts ...RequestOption) (*Response, error)Send a http request.
xhttp.DoRequest(ctx context.Context, req *http.Request, opts ...RequestOption) (*Response, error)Send a standard http request.
xhttp.WithBody(body Body) RequestOptionConfigure an option.
xhttp.WithHeader(header http.Header) RequestOptionConfigure an option.
xhttp.WithContentType(contentType string) RequestOptionConfigure an option.
xhttp.WithTimeout(timeout time.Duration) RequestOptionConfigure an option.
xhttp.WithDebugFunc(f DebugFunc) RequestOptionConfigure an option.
xhttp.WithRetry(f RetryIfFunc, opts ...retry.Option) RequestOptionConfigure an option.
xhttp.WithMiddleware(middlewares ...Middleware) RequestOptionConfigure an option.
xhttp.BuildJSON(v interface{}) BodyGenerate json string.
xhttp.BuildQuery(m map[string]string) BodyGenerate urlencoded query string.
xhttp.Shutdown(ctx context.Context)Do shutdown.

Send a request

Send a simple request.

url:="https://github.com/"resp, err:=xhttp.Fetch(context.Background(), http.MethodGet, url)

Configure some options.

url:="https://github.com/"header:=make(http.Header)
header.Set("Authorization", "Bearer ***")
resp, err:=xhttp.Fetch(context.Background(), http.MethodGet, url,
xhttp.WithContentType("application/json"),
xhttp.WithHeader(header))

Create a request object and send the request.

url:="https://github.com/"req, err:=xhttp.NewRequest(http.MethodGet, url)
iferr!=nil {
log.Fatal(err)
}
resp, err:=xhttp.Do(context.Background(), req)

Create a standard request object and send the request.

url:="https://github.com/"req, err:=http.NewRequest(http.MethodGet, url, nil)
iferr!=nil {
log.Fatal(err)
}
resp, err:=xhttp.DoRequest(context.Background(), req)

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.Fetch(context.Background(), http.MethodPost, url, xhttp.WithDebugFunc(f))

The log object contains the following fields

typeLogstruct {
Context context.Context`json:"context"`Duration time.Duration`json:"duration"`Request*Request`json:"request"`// The Request.RetryAttempts field records the number of retry attemptsResponse*Response`json:"response"`// If request error this field is equal to nilErrorerror`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, errerror) error {
iferr!=nil {
returnerr
}
ifresp.StatusCode!=200 {
returnfmt.Errorf("invalid status_code: %d", resp.StatusCode)
}
returnnil
}
resp, err:=xhttp.Fetch(context.Background(), http.MethodGet, url, xhttp.WithRetry(retryIf, retry.Attempts(2)))

Network error, no retry.

url:="https://aaaaa.com/"retryIf:=func(resp*xhttp.Response, errerror) error {
iferr!=nil {
returnerrors.Join(err, xhttp.ErrAbortRetry)
}
ifresp.StatusCode!=200 {
returnfmt.Errorf("invalid status_code: %d", resp.StatusCode)
}
returnnil
}
resp, err:=xhttp.Fetch(context.Background(), http.MethodGet, url, xhttp.WithRetry(retryIf, retry.Attempts(2)))

Middleware

Middleware configuration before or after.

logicMiddleware:=func(next xhttp.HandlerFunc) xhttp.HandlerFunc {
returnfunc(req*xhttp.Request) (*xhttp.Response, error) {
// Before-logicfmt.Printf("Before: %s %s\n", req.Method, req.URL)
// Call the next handlerresp, err:=next(req)
// After-logicfmt.Printf("After: %s %s\n", req.Method, req.URL)
returnresp, err
}
}
resp, err:=xhttp.Fetch(context.Background(), http.MethodGet, "https://github.com/", xhttp.WithMiddleware(logicMiddleware))

Shutdown

Before shutdown, all requests will be completed and work with middleware to save the response results to the database.

ch:=make(chan os.Signal)
signal.Notify(ch, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
gofunc() {
<-chxhttp.Shutdown(context.Background())
}()

License

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

About

A highly efficient HTTP library.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages