middl

package module
v0.0.0-...-2162286 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2018 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Build Status Go Report Card

go-httpclient-middl

Add middleware to your HTTP clients in Go

Why ?

If you need an HTTP client that automatically logs requests, tracks metrics, joins a token with each request, validates HTTP status codes, or does any other custom action for every request/reponse, this library might be for you.

How ?

Download the library using go get -u github.com/nmrshll/go-httpclient-middl

Then use it this way:

func main() {
	httpClient := http.Client{Timeout: 30 * time.Second}
	client, err := middl.NewClient(&httpClient)
	if err != nil {
		log.Fatal(err)
	}

	// add middleware to you client (classic examples provided in this library or custom)
	client.UseMiddleware(logger.New())
	client.UseMiddleware(statusvalidator.New())

	// then do your requests as usual
	resp, err := client.Get("https://google.com")
	if err != nil {
		log.Fatal(err)
	}
	if resp == nil {
		log.Fatalf("no response from server")
	} // else
	defer resp.Body.Close()

	// do something with the response here
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*http.Client
}

Client is a wrapper around http.Client that you can add middleware to

func NewClient

func NewClient(clients ...*http.Client) (*Client, error)

NewClient creates a new HttpClient

func (*Client) UseMiddleware

func (c *Client) UseMiddleware(middleware ...middleware.MiddlewareFunc)

type MiddlewareFunc

type MiddlewareFunc func(parent http.RoundTripper) http.RoundTripper

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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