client

package
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2024 License: Apache-2.0 Imports: 3 Imported by: 1

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

func (c Chain) Extend(more Chain) Chain

Extend is like Append, except that the additional Constructors come from another chain

func (Chain) Then

func (c Chain) Then(next httpaux.Client) httpaux.Client

Then applies the given sequence of middleware to the next httpaux.Client.

func (Chain) ThenFunc

func (c Chain) ThenFunc(next Func) httpaux.Client

ThenFunc makes it easier to use a client transactor Func as the HTTP client

type CheckRedirect added in v0.4.2

type CheckRedirect func(*http.Request, []*http.Request) error

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

type Constructor func(httpaux.Client) httpaux.Client

Constructor applies clientside middleware to an HTTP client, as implemented by httpaux.Client.

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.

type Func

type Func func(*http.Request) (*http.Response, error)

Func is an HTTP client function type

func (Func) Do

func (f Func) Do(request *http.Request) (*http.Response, error)

Do fulfills the httpaux.Client interface and permits this function to be used like an HTTP client.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL