Back to godoc.org
github.com/f2prateek/train

Package train

v0.0.0 (523ebca)
Latest Go to latest
Published: Apr 9, 2017 | License: MIT | Module: github.com/f2prateek/train

Overview

Package train provides a http.RoundTripper with chainable middleware.

Index

Examples

Package Files

func Transport

func Transport(interceptors ...Interceptor) http.RoundTripper

Return a new http.RoundTripper with the given interceptors and http.DefaultTransport. Interceptors will be called in the order they are provided.

Example

Code:

client := &http.Client{

	Transport: train.Transport(log.New(os.Stdout, log.None)),
}

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
	response.OK(w, "Hello World!")
}))
defer ts.Close()

resp, _ := client.Get(ts.URL)
bytes, _ := ioutil.ReadAll(resp.Body)

fmt.Println(string(bytes))
Hello World!

func TransportWith

func TransportWith(transport http.RoundTripper, interceptors ...Interceptor) http.RoundTripper

Return a new http.RoundTripper with the given interceptors and a custom http.RoundTripper to perform the actual HTTP request. Interceptors will be called in the order they are provided.

type Chain

type Chain interface {
	// Request returns the http.Request for this chain.
	Request() *http.Request
	// Proceed the chain with a given request and returns the result.
	Proceed(*http.Request) (*http.Response, error)
}

type Interceptor

type Interceptor interface {
	// Intercept the chain and return a result.
	Intercept(Chain) (*http.Response, error)
}

Observes, modifies, and potentially short-circuits requests going out and the corresponding requests coming back in. Typically interceptors will be used to add, remove, or transform headers on the request or response. Interceptors must return either a response or an error.

func RoundTripper

func RoundTripper(rt http.RoundTripper) Interceptor

RoundTripper adapts an `http.RoundTripper` to an `Interceptor`.

func UserAgent

func UserAgent(userAgent string) Interceptor

UserAgent returns an `Interceptor` that sets the user-agent on outgoing requests.

type InterceptorFunc

type InterceptorFunc func(Chain) (*http.Response, error)

The InterceptorFunc type is an adapter to allow the use of ordinary functions as interceptors. If f is a function with the appropriate signature, InterceptorFunc(f) is a Interceptor that calls f.

func (InterceptorFunc) Intercept

func (f InterceptorFunc) Intercept(c Chain) (*http.Response, error)

Intercept calls f(c).

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier