Skip to content

utility for before & after hooks on an http.RoundTripper

License

Notifications You must be signed in to change notification settings

dillonstreator/roundtriphook

Repository files navigation

roundtriphook

codecov

utility package which provides a simple before & after hook interface for an http.RoundTripper

Install

go get github.com/dillonstreator/roundtriphook

Usage

var wrappedTransport = roundtriphook.NewTransport(
	// This call to roundtriphook.WithBaseRoundTripper is unnecessary
	// since the default behavior is to set the base round tripper to http.DefaultTransport if none is provided
	roundtriphook.WithBaseRoundTripper(http.DefaultTransport),
	roundtriphook.WithBefore(func(req *http.Request) *http.Request {
		fmt.Println("before request")
		// mutate request or add context here
		return req
	}),
	roundtriphook.WithAfter(func(req *http.Request, res *http.Response, err error) {
		fmt.Println("after request")
	}),
)

var httpClient = &http.Client{
	Transport: wrappedTransport,
}

logging transport

var loggingTransport = roundtriphook.NewTransport(
	roundtriphook.WithBefore(func(req *http.Request) *http.Request {
		startTime := time.Now()
		id := startTime.UnixNano()

		fmt.Printf("[%d] -> %s %s\n", id, req.Method, req.URL)

		ctx := req.Context()
		ctx = context.WithValue(ctx, timeStartKey, startTime)
		ctx = context.WithValue(ctx, idKey, id)

		return req.WithContext(ctx)
	}),
	roundtriphook.WithAfter(func(req *http.Request, res *http.Response, err error) {
		startTime := req.Context().Value(timeStartKey).(time.Time)
		id := req.Context().Value(idKey).(int64)

		sb := strings.Builder{}
		sb.WriteString(fmt.Sprintf("[%d] <- %s %s %s", id, req.Method, req.URL, time.Since(startTime)))

		if res != nil {
			sb.WriteString(" " + res.Status)
		}

		if err != nil {
			sb.WriteString(fmt.Sprintf(" %s", err.Error()))
		}

		fmt.Printf("%s\n", sb.String())
	}),
)

full logging transport example

About

utility for before & after hooks on an http.RoundTripper

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages