Source File
options_client.go
Belonging Package
github.com/goforj/httpx
package httpximport ()// BaseURL sets a base URL on the client.// @group Client Options//// Applies to client configuration only.// Example: client base URL//// c := httpx.New(httpx.BaseURL("https://api.example.com"))// _ = cfunc ( string) OptionBuilder {return OptionBuilder{}.BaseURL()}func ( OptionBuilder) ( string) OptionBuilder {return .add(clientOnly(func( *Client) {.req.SetBaseURL()}))}// Transport wraps the underlying transport with a custom RoundTripper.// @group Client Options//// Applies to client configuration only.// Example: wrap transport//// c := httpx.New(httpx.Transport(http.RoundTripper(http.DefaultTransport)))// _ = cfunc ( http.RoundTripper) OptionBuilder {return OptionBuilder{}.Transport()}func ( OptionBuilder) ( http.RoundTripper) OptionBuilder {return .add(clientOnly(func( *Client) {if == nil {return}.req.Transport.WrapRoundTrip(func(http.RoundTripper) http.RoundTripper {return})}))}// Middleware adds request middleware to the client.// @group Client Options//// Applies only to the client configuration.// Example: add request middleware//// c := httpx.New(httpx.Middleware(func(_ *req.Client, r *req.Request) error {// r.SetHeader("X-Trace", "1")// return nil// }))// _ = cfunc ( ...req.RequestMiddleware) OptionBuilder {return OptionBuilder{}.Middleware(...)}func ( OptionBuilder) ( ...req.RequestMiddleware) OptionBuilder {return .add(clientOnly(func( *Client) {for , := range {.req.OnBeforeRequest()}}))}// ErrorMapper sets a custom error mapper for non-2xx responses.// @group Client Options//// Applies only to the client configuration.// Example: map error responses//// c := httpx.New(httpx.ErrorMapper(func(resp *req.Response) error {// return fmt.Errorf("status %d", resp.StatusCode)// }))// _ = cfunc ( ErrorMapperFunc) OptionBuilder {return OptionBuilder{}.ErrorMapper()}func ( OptionBuilder) ( ErrorMapperFunc) OptionBuilder {return .add(clientOnly(func( *Client) {.errorMapper =}))}// Proxy sets a proxy URL for the client.// @group Client Options//// Applies to client configuration only.// Example: set proxy URL//// c := httpx.New(httpx.Proxy("http://localhost:8080"))// _ = cfunc ( string) OptionBuilder {return OptionBuilder{}.Proxy()}func ( OptionBuilder) ( string) OptionBuilder {return .add(clientOnly(func( *Client) {if == "" {return}.req.SetProxyURL()}))}// ProxyFunc sets a proxy function for the client.// @group Client Options//// Applies to client configuration only.// Example: set proxy function//// c := httpx.New(httpx.ProxyFunc(http.ProxyFromEnvironment))// _ = cfunc ( func(*http.Request) (*url.URL, error)) OptionBuilder {return OptionBuilder{}.ProxyFunc()}func ( OptionBuilder) ( func(*http.Request) (*url.URL, error)) OptionBuilder {if == nil {return}return .add(clientOnly(func( *Client) {.req.SetProxy()}))}// CookieJar sets the cookie jar for the client.// @group Client Options//// Applies to client configuration only.// Example: set cookie jar and seed cookies//// jar, _ := cookiejar.New(nil)// u, _ := url.Parse("https://example.com")// jar.SetCookies(u, []*http.Cookie{// {Name: "session", Value: "abc123"},// })// c := httpx.New(httpx.CookieJar(jar))// _ = cfunc ( http.CookieJar) OptionBuilder {return OptionBuilder{}.CookieJar()}func ( OptionBuilder) ( http.CookieJar) OptionBuilder {return .add(clientOnly(func( *Client) {.req.SetCookieJar()}))}// Redirect sets the redirect policy for the client.// @group Client Options//// Applies to client configuration only.// Example: disable redirects//// c := httpx.New(httpx.Redirect(req.NoRedirectPolicy()))// _ = cfunc ( ...req.RedirectPolicy) OptionBuilder {return OptionBuilder{}.Redirect(...)}func ( OptionBuilder) ( ...req.RedirectPolicy) OptionBuilder {return .add(clientOnly(func( *Client) {if len() == 0 {return}.req.SetRedirectPolicy(...)}))}
![]() |
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |