tripperware

package
v3.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthenticationForwarder

func AuthenticationForwarder(options ...AuthOption) httpware.Tripperware

func CorrelationId

func CorrelationId(options ...correlation_id.Option) httpware.Tripperware

CorrelationId tripperware gets request id header if provided or generates a request id It will add the request ID to request context

Example
package main

import (
	"fmt"
	"log"
	"net"
	"net/http"

	"github.com/gol4ng/httpware/v3/correlation_id"
	"github.com/gol4ng/httpware/v3/tripperware"
)

func main() {
	// Example Need a random ephemeral port (to have a free port)
	ln, err := net.Listen("tcp", ":0")
	if err != nil {
		log.Fatal(err)
	}

	// create http client using the tripperwareStack as RoundTripper
	client := http.Client{
		Transport: tripperware.CorrelationId(
			correlation_id.WithHeaderName("my-personal-header-name"),
			correlation_id.WithIdGenerator(func(request *http.Request) string {
				return "my-fixed-request-id"
			}),
		),
	}

	// create a server in order to show it work
	srv := &http.Server{
		Handler: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
			fmt.Println("server receive request with request id:", request.Header.Get("my-personal-header-name"))
		}),
	}
	go func() {
		if err := srv.Serve(ln); err != nil {
			panic(err)
		}
	}()

	_, _ = client.Get("http://" + ln.Addr().String())

}
Output:

server receive request with request id: my-fixed-request-id

func DefaultCredentialForwarder

func DefaultCredentialForwarder(req *http.Request)

func Enable

func Enable(enable bool, tripperware httpware.Tripperware) httpware.Tripperware

Enable tripperware is used to conditionnaly add a tripperware to a TripperwareStack See Skip tripperware to active a tripperware in function of request

Example
// Example Need a random ephemeral port (to have a free port)
ln, err := net.Listen("tcp", ":0")
if err != nil {
	log.Fatal(err)
}

enableDummyTripperware := true //false
dummyTripperware := func(next http.RoundTripper) http.RoundTripper {
	return httpware.RoundTripFunc(func(request *http.Request) (*http.Response, error) {
		request.Header.Set("FakeHeader", "this header is set when not /home url")
		return next.RoundTrip(request)
	})
}

// create http client using the tripperwareStack as RoundTripper
client := http.Client{
	Transport: tripperware.Enable(enableDummyTripperware, dummyTripperware),
}

// create a server in order to show it work
srv := &http.Server{
	Handler: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
		fmt.Println("server receive request with request:", request.Header.Get("FakeHeader"))
	}),
}
go func() {
	if err := srv.Serve(ln); err != nil {
		panic(err)
	}
}()

_, _ = client.Get("http://" + ln.Addr().String())
Output:

server receive request with request: this header is set when not /home url

func Interceptor

func Interceptor(options ...InterceptorOption) httpware.Tripperware

Interceptor tripperware allow multiple req.Body read and allow to set callback before and after roundtrip

Example
// we recommend to use TripperwareStack to simplify managing all wanted tripperware
// caution tripperware order matter
stack := httpware.TripperwareStack(
	tripperware.Interceptor(
		tripperware.WithBefore(func(request *http.Request) {
			reqData, err := ioutil.ReadAll(request.Body)
			fmt.Println("before callback", string(reqData), err)
		}),
		tripperware.WithAfter(func(response *http.Response, request *http.Request) {
			reqData, err := ioutil.ReadAll(request.Body)
			fmt.Println("after callback", string(reqData), err)
		}),
	),
)

// create http client using the tripperwareStack as RoundTripper
client := http.Client{
	Transport: stack,
}

_, _ = client.Post("fake-address.foo", "plain/text", bytes.NewBufferString("my_fake_body"))
Output:

before callback my_fake_body <nil>
after callback my_fake_body <nil>

func Metrics

func Metrics(recorder metrics.Recorder, options ...metrics.Option) httpware.Tripperware
Example
recorder := prometheus.NewRecorder(prometheus.Config{}).RegisterOn(nil)

// we recommend to use MiddlewareStack to simplify managing all wanted middleware
// caution middleware order matter
stack := httpware.TripperwareStack(
	tripperware.Metrics(recorder, metrics.WithIdentifierProvider(func(req *http.Request) string {
		return req.URL.Host + " -> " + req.URL.Path
	})),
)

// create http client using the tripperwareStack as RoundTripper
client := http.Client{
	Transport: stack,
}

_, _ = client.Get("fake-address.foo")
Output:

func RateLimit

func RateLimit(rateLimiter rate_limit.RateLimiter, options ...RateLimitOption) httpware.Tripperware
Example
package main

import (
	"errors"
	"fmt"
	"net"
	"net/http"
	"time"

	"github.com/gol4ng/httpware/v3/rate_limit"
	"github.com/gol4ng/httpware/v3/tripperware"
)

func main() {
	// Example Need a random ephemeral port (to have a free port)
	ln, err := net.Listen("tcp", ":0")
	if err != nil {
		panic(err)
	}

	rl := rate_limit.NewTokenBucket(1*time.Second, 1)
	defer rl.Stop()

	client := http.Client{Transport: tripperware.RateLimit(rl)}

	srv := &http.Server{
		Handler: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
			fmt.Println("server receive request")
		}),
	}
	go func() {
		if err := srv.Serve(ln); err != nil {
			panic(err)
		}
	}()

	_, err = client.Get("http://" + ln.Addr().String())
	fmt.Println(err)

	_, err = client.Get("http://" + ln.Addr().String())
	fmt.Println(errors.Unwrap(err))

	time.Sleep(2 * time.Second)
	_, err = client.Get("http://" + ln.Addr().String())
	fmt.Println(err)
}
Output:

server receive request
<nil>
request limit reached
server receive request
<nil>

func Skip

func Skip(condition skip.Condition, tripperware httpware.Tripperware) httpware.Tripperware

Skip tripperware is used to conditionnaly activate a tripperware in function of request See Enable tripperware to conditionnaly add tripperware to a stack

Example
// Example Need a random ephemeral port (to have a free port)
ln, err := net.Listen("tcp", ":0")
if err != nil {
	panic(err)
}

dummyTripperware := func(next http.RoundTripper) http.RoundTripper {
	return httpware.RoundTripFunc(func(request *http.Request) (*http.Response, error) {
		request.Header.Set("FakeHeader", "this header is set when not /home url")
		return next.RoundTrip(request)
	})
}

// create http client using the tripperwareStack as RoundTripper
client := http.Client{
	Transport: tripperware.Skip(func(request *http.Request) bool {
		return request.URL.Path == "/home"
	}, dummyTripperware),
}

srv := &http.Server{
	Handler: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
		fmt.Println("server receive request with request:", request.Header.Get("FakeHeader"))
	}),
}
go func() {
	if err := srv.Serve(ln); err != nil {
		panic(err)
	}
}()

_, _ = client.Get("http://" + ln.Addr().String())
_, _ = client.Get("http://" + ln.Addr().String() + "/home")
Output:

server receive request with request: this header is set when not /home url
server receive request with request:

Types

type AuthConfig

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

func NewAuthConfig

func NewAuthConfig(options ...AuthOption) *AuthConfig

type AuthOption

type AuthOption func(*AuthConfig)

AuthOption defines a interceptor tripperware configuration option

func WithCredentialForwarder

func WithCredentialForwarder(authFunc credentialForwarder) AuthOption

WithCredentialForwarder will configure credentialForwarder option

type InterceptorConfig

type InterceptorConfig struct {
	CallbackBefore func(*http.Request)
	CallbackAfter  func(*http.Response, *http.Request)
}

func NewInterceptorConfig

func NewInterceptorConfig(options ...InterceptorOption) *InterceptorConfig

NewInterceptorConfig returns a new interceptor configuration with all options applied

type InterceptorOption

type InterceptorOption func(*InterceptorConfig)

InterceptorOption defines a interceptor tripperware configuration option

func WithAfter

func WithAfter(callbackAfter func(*http.Response, *http.Request)) InterceptorOption

WithAfter will configure CallbackAfter interceptor option

func WithBefore

func WithBefore(callbackBefore func(*http.Request)) InterceptorOption

WithAfter will configure CallbackAfter interceptor option

type RateLimitConfig

type RateLimitConfig struct {
	ErrorCallback RateLimitErrorCallback
}

func NewRateLimitConfig

func NewRateLimitConfig(options ...RateLimitOption) *RateLimitConfig

type RateLimitErrorCallback

type RateLimitErrorCallback func(request *http.Request, limitErr error) (response *http.Response, err error)

func DefaultRateLimitErrorCallback

func DefaultRateLimitErrorCallback() RateLimitErrorCallback

type RateLimitOption

type RateLimitOption func(*RateLimitConfig)

func WithRateLimitErrorCallback

func WithRateLimitErrorCallback(callback RateLimitErrorCallback) RateLimitOption

Jump to

Keyboard shortcuts

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