Documentation
¶
Overview ¶
Package client provides infrastructure for build HTTP clients, including observability and logging.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain is an immutable sequence of constructors. This type is essentially a bundle of middleware for HTTP clients.
func NewChain ¶
func NewChain(ctors ...Constructor) (c Chain)
NewChain creates a chain from a sequence of constructors. The constructors are always applied in the order presented here.
func (Chain) Append ¶
func (c Chain) Append(more ...Constructor) (nc Chain)
Append adds additional Constructors to this chain, and returns the new chain. This chain is not modified. If more has zero length, this chain is returned.
func (Chain) Extend ¶
Extend is like Append, except that the additional Constructors come from another chain
type CheckRedirect ¶ added in v0.4.2
CheckRedirect is the type expected by http.Client.CheckRedirect.
Closures of this type can also be chained together via the NewCheckRedirects function.
func CopyHeadersOnRedirect ¶ added in v0.4.2
func CopyHeadersOnRedirect(names ...string) CheckRedirect
CopyHeadersOnRedirect copies the headers from the most recent request into the next request. If no names are supplied, this function returns nil so that the default behavior will take over.
Example ¶
request := httptest.NewRequest("GET", "/", nil)
previous := httptest.NewRequest("GET", "/", nil)
previous.Header.Set("Copy-Me", "copied value")
client := http.Client{
CheckRedirect: CopyHeadersOnRedirect("copy-me"), // canonicalization will happen
}
if err := client.CheckRedirect(request, []*http.Request{previous}); err != nil {
// shouldn't be output
fmt.Println(err)
}
fmt.Println(request.Header.Get("Copy-Me"))
Output: copied value
func MaxRedirects ¶ added in v0.4.2
func MaxRedirects(max int) CheckRedirect
MaxRedirects returns a CheckRedirect that returns an error if a maximum number of redirects has been reached. If the max value is 0 or negative, then no redirects are allowed.
Example ¶
request := httptest.NewRequest("GET", "/", nil)
client := http.Client{
CheckRedirect: MaxRedirects(5),
}
if client.CheckRedirect(request, make([]*http.Request, 4)) == nil {
fmt.Println("fewer than 5 redirects")
}
if client.CheckRedirect(request, make([]*http.Request, 6)) != nil {
fmt.Println("max redirects exceeded")
}
Output: fewer than 5 redirects max redirects exceeded
func NewCheckRedirects ¶ added in v0.4.2
func NewCheckRedirects(checks ...CheckRedirect) CheckRedirect
NewCheckRedirects produces a CheckRedirect that is the logical AND of the given strategies. All the checks must pass, or the returned function halts early and returns the error from the failing check.
Since a nil http.Request.CheckRedirect indicates that the internal default will be used, this function returns nil if no checks are supplied. Additionally, any nil checks are skipped. If all checks are nil, this function also returns nil.
Example ¶
request := httptest.NewRequest("GET", "/", nil)
previous := httptest.NewRequest("GET", "/", nil)
previous.Header.Set("Copy-Me", "copied value")
client := http.Client{
CheckRedirect: NewCheckRedirects(
MaxRedirects(10),
CopyHeadersOnRedirect("Copy-Me"),
func(*http.Request, []*http.Request) error {
fmt.Println("custom check redirect")
return nil
},
),
}
if err := client.CheckRedirect(request, []*http.Request{previous}); err != nil {
// shouldn't be output
fmt.Println(err)
}
fmt.Println(request.Header.Get("Copy-Me"))
Output: custom check redirect copied value
type Constructor ¶
Constructor applies clientside middleware to an HTTP client, as implemented by httpaux.Client.
func Header ¶ added in v0.1.6
func Header(hf func(http.Header)) Constructor
Header adds a middleware Constructor that uses a closure to modify each request header. Both httpaux.Header.SetTo and httpaux.Header.AddTo can be used as this closure.