httputil

package
v0.0.0-...-9858bdd Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2023 License: MIT Imports: 7 Imported by: 11

Documentation

Index

Constants

View Source
const (
	OperationGet    = "GET"
	OperationPost   = "POST"
	OperationDelete = "DELETE"
	OperationPut    = "PUT"
)

Variables

This section is empty.

Functions

func IsSuccessStatusCode

func IsSuccessStatusCode(statusCode int) bool

Types

type Client

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

func (*Client) Delete

func (client *Client) Delete(url string, headers map[string]string, payload []byte) (responseCode int, body []byte, err error)

Delete issues a delete request

func (*Client) Get

func (client *Client) Get(url string, headers map[string]string) (responseCode int, body []byte, err error)

Get issues a get request

func (*Client) Post

func (client *Client) Post(url string, headers map[string]string, payload []byte) (responseCode int, body []byte, err error)

Post issues a post request

func (*Client) Put

func (client *Client) Put(url string, headers map[string]string, payload []byte) (responseCode int, body []byte, err error)

Put issues a put request

type HttpClient

type HttpClient interface {
	Get(url string, headers map[string]string) (responseCode int, body []byte, err error)
	Post(url string, headers map[string]string, payload []byte) (responseCode int, body []byte, err error)
	Put(url string, headers map[string]string, payload []byte) (responseCode int, body []byte, err error)
	Delete(url string, headers map[string]string, payload []byte) (responseCode int, body []byte, err error)
}

func NewInsecureHttpClientWithCertificates

func NewInsecureHttpClientWithCertificates(certificate string, key string, retryBehavior RetryBehavior) HttpClient

func NewSecureHttpClient

func NewSecureHttpClient(retryBehavior RetryBehavior) HttpClient

func NewSecureHttpClientWithCertificates

func NewSecureHttpClientWithCertificates(certificate string, key string, retryBehavior RetryBehavior) HttpClient

type MockHttpClient

type MockHttpClient struct {
	// overwrite these methods to get the desired output
	Getfunc    func(url string, headers map[string]string) (responseCode int, body []byte, err error)
	Postfunc   func(url string, headers map[string]string, payload []byte) (responseCode int, body []byte, err error)
	Putfunc    func(url string, headers map[string]string, payload []byte) (responseCode int, body []byte, err error)
	Deletefunc func(url string, headers map[string]string, payload []byte) (responseCode int, body []byte, err error)
}

func (*MockHttpClient) Delete

func (client *MockHttpClient) Delete(url string, headers map[string]string, payload []byte) (responseCode int, body []byte, err error)

func (*MockHttpClient) Get

func (client *MockHttpClient) Get(url string, headers map[string]string) (responseCode int, body []byte, err error)

func (*MockHttpClient) Post

func (client *MockHttpClient) Post(url string, headers map[string]string, payload []byte) (responseCode int, body []byte, err error)

func (*MockHttpClient) Put

func (client *MockHttpClient) Put(url string, headers map[string]string, payload []byte) (responseCode int, body []byte, err error)

type RetryBehavior

type RetryBehavior = func(statusCode int, i int) bool
var DefaultRetryBehavior RetryBehavior = func(statusCode int, i int) bool {
	if !isTransientHttpStatusCode(statusCode) {
		return false
	}
	delay := time.Second * time.Duration(2^(i))
	const maxDelay time.Duration = 60 * time.Second

	if delay > maxDelay {
		delay = maxDelay
	}
	time.Sleep(delay)
	if i < 5 {
		return true
	}
	return false
}

The default retry behavior is 5 retries with exponential back-off with a maximum wait time of 60 seconds

var LinearRetryThrice RetryBehavior = func(statusCode int, i int) bool {
	if !isTransientHttpStatusCode(statusCode) {
		return false
	}
	time.Sleep(time.Second * 3)
	if i < 3 {
		return true
	}
	return false
}
var NoRetry RetryBehavior = func(statusCode int, i int) bool {
	return false
}

Jump to

Keyboard shortcuts

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