Skip to content

mediabuyerbot/httpclient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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"),  
)