http

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2022 License: MIT Imports: 21 Imported by: 51

Documentation

Index

Constants

View Source
const LogHandlerKey string = "Log"
View Source
const RequestIDHandlerKey string = "RequestID"

Variables

View Source
var DefaultTransport = &http.Transport{
	DialContext: (&net.Dialer{
		Timeout: 5 * time.Second,
	}).DialContext,
	TLSHandshakeTimeout: 5 * time.Second,
	MaxIdleConns:        10,
	IdleConnTimeout:     30 * time.Second,
}

DefaultTransport is the default implementation of Transport and is used by DefaultClient.

Functions

func DrainBody added in v1.0.7

func DrainBody(r *http.Request)

DrainBody drains the body of the given of the given HTTP request.

Types

type AwsSignerRoundTripper added in v1.3.0

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

func NewAWSSignerRoundTripper added in v1.3.0

func NewAWSSignerRoundTripper(awsFilename, awsProfile, awsRegion, awsService string, customTransport http.RoundTripper) (*AwsSignerRoundTripper, error)

func (*AwsSignerRoundTripper) RoundTrip added in v1.3.0

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

type Client

type Client struct {
	MaxRetries         int
	RetryTime          time.Duration
	PathsWithNoRetries map[string]bool
	HTTPClient         *http.Client
}

Client is an extension of the net/http client with ability to add timeouts, exponential backoff and context-based cancellation.

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *http.Request) (*http.Response, error)

Do calls ctxhttp.Do with the addition of retries with exponential backoff

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string) (*http.Response, error)

Get calls Do with a GET.

func (*Client) GetMaxRetries

func (c *Client) GetMaxRetries() int

GetMaxRetries gets the HTTP request maximum number of retries.

func (*Client) GetPathsWithNoRetries

func (c *Client) GetPathsWithNoRetries() (paths []string)

GetPathsWithNoRetries gets a list of paths that will HTTP request will not retry on error.

func (*Client) Head

func (c *Client) Head(ctx context.Context, url string) (*http.Response, error)

Head calls Do with a HEAD.

func (*Client) Post

func (c *Client) Post(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error)

Post calls Do with a POST and the appropriate content-type and body.

func (*Client) PostForm

func (c *Client) PostForm(ctx context.Context, uri string, data url.Values) (*http.Response, error)

PostForm calls Post with the appropriate form content-type.

func (*Client) Put

func (c *Client) Put(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error)

Put calls Do with a PUT and the appropriate content-type and body.

func (*Client) RoundTrip added in v1.3.0

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

Clienter roundtripper calls the httpclient roundtripper.

func (*Client) SetMaxRetries

func (c *Client) SetMaxRetries(maxRetries int)

SetMaxRetries sets HTTP request maximum number of retries.

func (*Client) SetPathsWithNoRetries

func (c *Client) SetPathsWithNoRetries(paths []string)

SetPathsWithNoRetries sets a list of paths that will HTTP request will not retry on error.

func (*Client) SetTimeout

func (c *Client) SetTimeout(timeout time.Duration)

SetTimeout sets HTTP request timeout.

type Clienter

type Clienter interface {
	SetTimeout(timeout time.Duration)
	SetMaxRetries(int)
	GetMaxRetries() int
	SetPathsWithNoRetries([]string)
	GetPathsWithNoRetries() []string

	Get(ctx context.Context, url string) (*http.Response, error)
	Head(ctx context.Context, url string) (*http.Response, error)
	Post(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error)
	Put(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error)
	PostForm(ctx context.Context, uri string, data url.Values) (*http.Response, error)

	Do(ctx context.Context, req *http.Request) (*http.Response, error)
	RoundTrip(req *http.Request) (*http.Response, error)
}

Clienter provides an interface for methods on an HTTP Client.

func ClientWithListOfNonRetriablePaths

func ClientWithListOfNonRetriablePaths(c Clienter, paths []string) Clienter

ClientWithListOfNonRetriablePaths facilitates creating a client and setting a list of paths that should not be retried on failure.

func ClientWithTimeout

func ClientWithTimeout(c Clienter, timeout time.Duration) Clienter

ClientWithTimeout facilitates creating a client and setting request timeout.

func NewClient

func NewClient() Clienter

NewClient returns a copy of DefaultClient.

func NewClientWithAwsSigner added in v1.3.0

func NewClientWithAwsSigner(awsFilename, awsProfile, awsRegion, awsService string) (Clienter, error)

NewClientWithAwsSigner return a new client with aws signer profile.

type ClienterMock

type ClienterMock struct {
	// DoFunc mocks the Do method.
	DoFunc func(ctx context.Context, req *http.Request) (*http.Response, error)

	// GetFunc mocks the Get method.
	GetFunc func(ctx context.Context, url string) (*http.Response, error)

	// GetMaxRetriesFunc mocks the GetMaxRetries method.
	GetMaxRetriesFunc func() int

	// GetPathsWithNoRetriesFunc mocks the GetPathsWithNoRetries method.
	GetPathsWithNoRetriesFunc func() []string

	// HeadFunc mocks the Head method.
	HeadFunc func(ctx context.Context, url string) (*http.Response, error)

	// PostFunc mocks the Post method.
	PostFunc func(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error)

	// PostFormFunc mocks the PostForm method.
	PostFormFunc func(ctx context.Context, uri string, data url.Values) (*http.Response, error)

	// PutFunc mocks the Put method.
	PutFunc func(ctx context.Context, urlMoqParam string, contentType string, body io.Reader) (*http.Response, error)

	// RoundTripFunc mocks the RoundTrip method.
	RoundTripFunc func(req *http.Request) (*http.Response, error)

	// SetMaxRetriesFunc mocks the SetMaxRetries method.
	SetMaxRetriesFunc func(n int)

	// SetPathsWithNoRetriesFunc mocks the SetPathsWithNoRetries method.
	SetPathsWithNoRetriesFunc func(strings []string)

	// SetTimeoutFunc mocks the SetTimeout method.
	SetTimeoutFunc func(timeout time.Duration)
	// contains filtered or unexported fields
}

ClienterMock is a mock implementation of Clienter.

func TestSomethingThatUsesClienter(t *testing.T) {

	// make and configure a mocked Clienter
	mockedClienter := &ClienterMock{
		DoFunc: func(ctx context.Context, req *http.Request) (*http.Response, error) {
			panic("mock out the Do method")
		},
		GetFunc: func(ctx context.Context, url string) (*http.Response, error) {
			panic("mock out the Get method")
		},
		GetMaxRetriesFunc: func() int {
			panic("mock out the GetMaxRetries method")
		},
		GetPathsWithNoRetriesFunc: func() []string {
			panic("mock out the GetPathsWithNoRetries method")
		},
		HeadFunc: func(ctx context.Context, url string) (*http.Response, error) {
			panic("mock out the Head method")
		},
		PostFunc: func(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error) {
			panic("mock out the Post method")
		},
		PostFormFunc: func(ctx context.Context, uri string, data url.Values) (*http.Response, error) {
			panic("mock out the PostForm method")
		},
		PutFunc: func(ctx context.Context, urlMoqParam string, contentType string, body io.Reader) (*http.Response, error) {
			panic("mock out the Put method")
		},
		RoundTripFunc: func(req *http.Request) (*http.Response, error) {
			panic("mock out the RoundTrip method")
		},
		SetMaxRetriesFunc: func(n int)  {
			panic("mock out the SetMaxRetries method")
		},
		SetPathsWithNoRetriesFunc: func(strings []string)  {
			panic("mock out the SetPathsWithNoRetries method")
		},
		SetTimeoutFunc: func(timeout time.Duration)  {
			panic("mock out the SetTimeout method")
		},
	}

	// use mockedClienter in code that requires Clienter
	// and then make assertions.

}

func (*ClienterMock) Do

func (mock *ClienterMock) Do(ctx context.Context, req *http.Request) (*http.Response, error)

Do calls DoFunc.

func (*ClienterMock) DoCalls

func (mock *ClienterMock) DoCalls() []struct {
	Ctx context.Context
	Req *http.Request
}

DoCalls gets all the calls that were made to Do. Check the length with:

len(mockedClienter.DoCalls())

func (*ClienterMock) Get

func (mock *ClienterMock) Get(ctx context.Context, url string) (*http.Response, error)

Get calls GetFunc.

func (*ClienterMock) GetCalls

func (mock *ClienterMock) GetCalls() []struct {
	Ctx context.Context
	URL string
}

GetCalls gets all the calls that were made to Get. Check the length with:

len(mockedClienter.GetCalls())

func (*ClienterMock) GetMaxRetries

func (mock *ClienterMock) GetMaxRetries() int

GetMaxRetries calls GetMaxRetriesFunc.

func (*ClienterMock) GetMaxRetriesCalls

func (mock *ClienterMock) GetMaxRetriesCalls() []struct {
}

GetMaxRetriesCalls gets all the calls that were made to GetMaxRetries. Check the length with:

len(mockedClienter.GetMaxRetriesCalls())

func (*ClienterMock) GetPathsWithNoRetries

func (mock *ClienterMock) GetPathsWithNoRetries() []string

GetPathsWithNoRetries calls GetPathsWithNoRetriesFunc.

func (*ClienterMock) GetPathsWithNoRetriesCalls

func (mock *ClienterMock) GetPathsWithNoRetriesCalls() []struct {
}

GetPathsWithNoRetriesCalls gets all the calls that were made to GetPathsWithNoRetries. Check the length with:

len(mockedClienter.GetPathsWithNoRetriesCalls())

func (*ClienterMock) Head

func (mock *ClienterMock) Head(ctx context.Context, url string) (*http.Response, error)

Head calls HeadFunc.

func (*ClienterMock) HeadCalls

func (mock *ClienterMock) HeadCalls() []struct {
	Ctx context.Context
	URL string
}

HeadCalls gets all the calls that were made to Head. Check the length with:

len(mockedClienter.HeadCalls())

func (*ClienterMock) Post

func (mock *ClienterMock) Post(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error)

Post calls PostFunc.

func (*ClienterMock) PostCalls

func (mock *ClienterMock) PostCalls() []struct {
	Ctx         context.Context
	URL         string
	ContentType string
	Body        io.Reader
}

PostCalls gets all the calls that were made to Post. Check the length with:

len(mockedClienter.PostCalls())

func (*ClienterMock) PostForm

func (mock *ClienterMock) PostForm(ctx context.Context, uri string, data url.Values) (*http.Response, error)

PostForm calls PostFormFunc.

func (*ClienterMock) PostFormCalls

func (mock *ClienterMock) PostFormCalls() []struct {
	Ctx  context.Context
	URI  string
	Data url.Values
}

PostFormCalls gets all the calls that were made to PostForm. Check the length with:

len(mockedClienter.PostFormCalls())

func (*ClienterMock) Put

func (mock *ClienterMock) Put(ctx context.Context, urlMoqParam string, contentType string, body io.Reader) (*http.Response, error)

Put calls PutFunc.

func (*ClienterMock) PutCalls

func (mock *ClienterMock) PutCalls() []struct {
	Ctx         context.Context
	UrlMoqParam string
	ContentType string
	Body        io.Reader
}

PutCalls gets all the calls that were made to Put. Check the length with:

len(mockedClienter.PutCalls())

func (*ClienterMock) RoundTrip added in v1.5.0

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

RoundTrip calls RoundTripFunc.

func (*ClienterMock) RoundTripCalls added in v1.5.0

func (mock *ClienterMock) RoundTripCalls() []struct {
	Req *http.Request
}

RoundTripCalls gets all the calls that were made to RoundTrip. Check the length with:

len(mockedClienter.RoundTripCalls())

func (*ClienterMock) SetMaxRetries

func (mock *ClienterMock) SetMaxRetries(n int)

SetMaxRetries calls SetMaxRetriesFunc.

func (*ClienterMock) SetMaxRetriesCalls

func (mock *ClienterMock) SetMaxRetriesCalls() []struct {
	N int
}

SetMaxRetriesCalls gets all the calls that were made to SetMaxRetries. Check the length with:

len(mockedClienter.SetMaxRetriesCalls())

func (*ClienterMock) SetPathsWithNoRetries

func (mock *ClienterMock) SetPathsWithNoRetries(strings []string)

SetPathsWithNoRetries calls SetPathsWithNoRetriesFunc.

func (*ClienterMock) SetPathsWithNoRetriesCalls

func (mock *ClienterMock) SetPathsWithNoRetriesCalls() []struct {
	Strings []string
}

SetPathsWithNoRetriesCalls gets all the calls that were made to SetPathsWithNoRetries. Check the length with:

len(mockedClienter.SetPathsWithNoRetriesCalls())

func (*ClienterMock) SetTimeout

func (mock *ClienterMock) SetTimeout(timeout time.Duration)

SetTimeout calls SetTimeoutFunc.

func (*ClienterMock) SetTimeoutCalls

func (mock *ClienterMock) SetTimeoutCalls() []struct {
	Timeout time.Duration
}

SetTimeoutCalls gets all the calls that were made to SetTimeout. Check the length with:

len(mockedClienter.SetTimeoutCalls())

type Doer

type Doer = func(context.Context, *http.Client, *http.Request) (*http.Response, error)

type Server

type Server struct {
	http.Server

	Alice                  *alice.Chain
	CertFile               string
	KeyFile                string
	DefaultShutdownTimeout time.Duration
	HandleOSSignals        bool
	// contains filtered or unexported fields
}

Server is a http.Server with sensible defaults, which supports configurable middleware and timeouts, and shuts down cleanly on SIGINT/SIGTERM

func NewServer

func NewServer(bindAddr string, router http.Handler) *Server

NewServer creates a new server

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe sets up SIGINT/SIGTERM signals, builds the middleware chain, and creates/starts a http.Server instance

If CertFile/KeyFile are both set, the http.Server instance is started using ListenAndServeTLS. Otherwise ListenAndServe is used.

Specifying one of CertFile/KeyFile without the other will panic.

func (*Server) ListenAndServeTLS

func (s *Server) ListenAndServeTLS(certFile, keyFile string) error

ListenAndServeTLS sets KeyFile and CertFile, then calls ListenAndServe

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown will gracefully shutdown the server, using a default shutdown timeout if a context is not provided.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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