httpclient

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2020 License: MIT Imports: 10 Imported by: 3

README

httpclient Coverage Status

Table of contents

Installation
go get github.com/mediabuyerbot/httpclient
Commands
Build dependencies
make deps
Run test
make test
Run test with coverage profile
make covertest
Run sync coveralls
COVERALLS_HTTPCLIENT_TOKEN=${COVERALLS_REPO_TOKEN}
make sync-coveralls
Build mocks
make mocks
Usage
Making a GET request
cli, err := New()
if err != nil {
    panic(err)
}
resp, err := cli.Get(context.TODO(), "https://google.com", nil)
if err != nil {
    panic(err)
}
...
Making a GET request with headers
cli, err := New()
if err != nil {
    panic(err)
}
headers := make(http.Header)
headers.Add("X-Header", "value")
resp, err := cli.Get(context.TODO(), "https://google.com", headers)
if err != nil {
    panic(err)
}
...
Making a POST request
cli, err := New()
if err != nil {
    panic(err)
}
payload := ioutil.NopCloser(bytes.NewReader([]byte(`payload`)))
resp, err := cli.Post(context.TODO(), payload, "https://google.com", nil)
if err != nil {
    panic(err)
}
...
Making a POST request with headers
cli, err := New()
if err != nil {
    panic(err)
}
headers := make(http.Header)
headers.Add("X-Header", "value")
payload := ioutil.NopCloser(bytes.NewReader([]byte(`payload`)))
resp, err := cli.POST(context.TODO(), payload, "https://google.com", headers)
if err != nil {
    panic(err)
}
...
Making a PUT request
cli, err := New()
if err != nil {
    panic(err)
}
payload := ioutil.NopCloser(bytes.NewReader([]byte(`payload`)))
resp, err := cli.PUT(context.TODO(), payload, "https://google.com", nil)
if err != nil {
    panic(err)
}
...
Making a PUT request with headers
cli, err := New()
if err != nil {
    panic(err)
}
headers := make(http.Header)
headers.Add("X-Header", "value")
payload := ioutil.NopCloser(bytes.NewReader([]byte(`payload`)))
resp, err := cli.PUT(context.TODO(), payload, "https://google.com", headers)
if err != nil {
    panic(err)
}
...
Making a DELETE request
cli, err := New()
if err != nil {
    panic(err)
}
resp, err := cli.Delete(context.TODO(), "https://google.com", nil)
if err != nil {
    panic(err)
}
...
Making a DELETE request with headers
cli, err := New()
if err != nil {
    panic(err)
}
headers := make(http.Header)
headers.Add("X-Header", "value")
resp, err := cli.Delete(context.TODO(), "https://google.com", headers)
if err != nil {
    panic(err)
}
...
Making a CUSTOM request
cli, err := New()
if err != nil {
    panic(err)
}
req, err := http.NewRequest(http.MethodHead, "https://google.com", nil)
if err != nil {
    panic(err) 
}
resp, err := cli.Do(req)
if err != nil {
    panic(err)
}
...
Options
_, err := New(
   WithTimeout(time.Second),
   WithRetryCount(2),
   WithRequestHook(func(request *http.Request, i int) {})   
   WithResponseHook(func(request *http.Request, response *http.Response) {}),
   WithCheckRetry(func(req *http.Request, resp *http.Response, err error) (bool, error) {}),
   WithBackOff(func(attemptNum int, resp *http.Response) time.Duration {}),
   WithErrorHook(func(req *http.Request, err error, retry int) {}),
   WithErrorHandler(func(resp *http.Response, err error, numTries int) (*http.Response, error) {}),
   WithBaseURL("http://127.0.0.1"),  
)

Documentation

Overview

Package httpclient is a generated GoMock package.

Index

Constants

View Source
const (
	DefaultHTTPTimeout = 60 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BackOff

type BackOff func(attemptNum int, resp *http.Response) time.Duration

BackOff specifies a policy for how long to wait between retries. It is called after a failing request to determine the amount of time that should pass before trying again.

type CheckRetry

type CheckRetry func(req *http.Request, resp *http.Response, err error) (bool, error)

CheckRetry specifies a policy for handling retries. It is called following each request with the response and error values returned by the http.Client. If CheckRetry returns false, the Client stops retrying and returns the response to the caller. If CheckRetry returns an error, that error value is returned in lieu of the error from the request. The Client will close any response body when retrying, but if the retry is aborted it is up to the CheckRetry callback to properly close any response body before returning.

type Client

type Client interface {
	Get(ctx context.Context, url string, headers http.Header) (*http.Response, error)
	Post(ctx context.Context, url string, body io.Reader, headers http.Header) (*http.Response, error)
	Put(ctx context.Context, url string, body io.Reader, headers http.Header) (*http.Response, error)
	Delete(ctx context.Context, url string, headers http.Header) (*http.Response, error)
	Do(req *http.Request) (*http.Response, error)
}

Client is a generic HTTP client interface.

func New

func New(opts ...Option) (Client, error)

New returns a new instance of Client.

type Doer

type Doer interface {
	Do(*http.Request) (*http.Response, error)
}

Doer interface has the method required to use a type as custom http client. The net/*http.Client type satisfies this interface.

type ErrorHandler

type ErrorHandler func(resp *http.Response, err error, numTries int) (*http.Response, error)

ErrorHandler is called if retries are expired, containing the last status from the http library. If not specified, default behavior for the library is to close the body and return an error indicating how many tries were attempted. If overriding this, be sure to close the body if needed.

type ErrorHook

type ErrorHook func(req *http.Request, err error, retry int)

ErrorHook is called when the request returned a connection error.

type HttpClient

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

HttpClient is the http client implementation

func (*HttpClient) Delete

func (c *HttpClient) Delete(ctx context.Context, url string, headers http.Header) (*http.Response, error)

Delete makes a HTTP DELETE request with provided URL.

func (*HttpClient) Do

func (c *HttpClient) Do(req *http.Request) (resp *http.Response, err error)

Do makes an HTTP request with the native `http.Do` interface.

func (*HttpClient) Get

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

Get makes a HTTP GET request to provided URL.

func (*HttpClient) Post

func (c *HttpClient) Post(ctx context.Context, url string, body io.Reader, headers http.Header) (*http.Response, error)

Post makes a HTTP POST request to provided URL and requestBody.

func (*HttpClient) Put

func (c *HttpClient) Put(ctx context.Context, url string, body io.Reader, headers http.Header) (*http.Response, error)

Put makes a HTTP PUT request to provided URL and requestBody.

type MockDoer

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

MockDoer is a mock of Doer interface

func NewMockDoer

func NewMockDoer(ctrl *gomock.Controller) *MockDoer

NewMockDoer creates a new mock instance

func (*MockDoer) Do

func (m *MockDoer) Do(arg0 *http.Request) (*http.Response, error)

Do mocks base method

func (*MockDoer) EXPECT

func (m *MockDoer) EXPECT() *MockDoerMockRecorder

EXPECT returns an object that allows the caller to indicate expected use

type MockDoerMockRecorder

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

MockDoerMockRecorder is the mock recorder for MockDoer

func (*MockDoerMockRecorder) Do

func (mr *MockDoerMockRecorder) Do(arg0 interface{}) *gomock.Call

Do indicates an expected call of Do

type Option

type Option func(client *HttpClient)

func WithBackOff

func WithBackOff(b BackOff) Option

func WithBaseURL

func WithBaseURL(u string) Option

func WithCheckRetry

func WithCheckRetry(cr CheckRetry) Option

func WithDoer

func WithDoer(client Doer) Option

func WithErrorHandler

func WithErrorHandler(eh ErrorHandler) Option

func WithErrorHook

func WithErrorHook(eh ErrorHook) Option

func WithRequestHook

func WithRequestHook(rh RequestHook) Option

func WithResponseHook

func WithResponseHook(rh ResponseHook) Option

func WithRetryCount

func WithRetryCount(retryCount int) Option

func WithTimeout

func WithTimeout(timeout time.Duration) Option

type RequestHook

type RequestHook func(*http.Request, int)

RequestHook allows a function to run before each retry. The HTTP request which will be made, and the retry number (0 for the initial request) are available to users.

type ResponseHook

type ResponseHook func(*http.Request, *http.Response)

ResponseHook is like RequestHook, but allows running a function on each HTTP response. This function will be invoked at the end of every HTTP request executed, regardless of whether a subsequent retry needs to be performed or not. If the response body is read or closed from this method, this will affect the response returned from Do().

Jump to

Keyboard shortcuts

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