tripware

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: Apache-2.0 Imports: 1 Imported by: 0

README

tripware

Tripware provides a simple way to compose multiple http.RoundTrips against a single http.Client, similar to middleware in a server context.

Installation

go get ivan.dev/httpcache/tripware

Usage

tripware.Tripware will apply all roundtrippers in the order specified when the client makes a request.

roundtrip:= tripware.New(http.DefaultTransport)

// Middleware 1
roundtrip.Use(func(rt http.RoundTripper) http.RoundTripper {
return &loghttp.Transport{Transport: rt}
})

// Middleware 2 adds the authorization header to each request.
roundtrip.Use(tripware.WithHeader(http.Header{"Authorization": []string{"Bearer 1234567890"}}))

// Apply the roundtrip transport to your custom http.Client
client := &http.Client{Transport: roundtrip}

req:= http.NewRequest(...)
client.Do(req)

See headers.go for an example of how to write a Triperware that adds headers to each request.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRoundTripper

func NewRoundTripper(original http.RoundTripper) http.RoundTripper

Types

type HeaderRoundTripper

type HeaderRoundTripper struct {
	Header http.Header
	// contains filtered or unexported fields
}

func (*HeaderRoundTripper) RoundTrip

func (rt *HeaderRoundTripper) RoundTrip(req *http.Request) (resp *http.Response, err error)

type RoundTrip

type RoundTrip func(req *http.Request) (*http.Response, error)

RoundTrip wraps a func to make it into a http.RoundTripper. Similar to http.HandleFunc.

func (RoundTrip) RoundTrip

func (f RoundTrip) RoundTrip(r *http.Request) (*http.Response, error)

type Tripperware

type Tripperware func(rt http.RoundTripper) http.RoundTripper

Middleware func(rt http.RoundTripper) http.RoundTripper

func WithHeader

func WithHeader(Header http.Header) Tripperware

WithHeader adds the given headers to each request. Example: roundtrip.Use(WithHeader(http.Header{"Authorization": []string{"Bearer 1234567890"}}))

type Tripware

type Tripware struct {
	// contains filtered or unexported fields
}

func New

func New(base http.RoundTripper) *Tripware

func (*Tripware) RoundTrip

func (t *Tripware) RoundTrip(req *http.Request) (*http.Response, error)

func (*Tripware) Use

func (t *Tripware) Use(middleware Tripperware)

Jump to

Keyboard shortcuts

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