utils

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

wraps retry logic in overrides to http.Client

Index

Constants

View Source
const (
	Authorization = "Authorization"
	ContentType   = "Content-Type"
	UserAgent     = "User-Agent"

	CliUserAgent = "tcli"
)
View Source
const (
	RetryAfter               = "Retry-After"
	DefaultRetryAfterSeconds = time.Second * 3
	DefaultRetryCount        = 3
)

Variables

This section is empty.

Functions

func AddAuthorizationHeader

func AddAuthorizationHeader(req *http.Request, bearerToken string)

add authorization header to http request

func AddContentTypeJson

func AddContentTypeJson(req *http.Request)

add content type application/json

func AddContentTypeUrlEncoded

func AddContentTypeUrlEncoded(req *http.Request)

add content type url encoded

func AddRequestHeader

func AddRequestHeader(req *http.Request, key, value string)

generic add header

func AddUserAgentHeader

func AddUserAgentHeader(req *http.Request)

add user-agent to http request

func CloseBodyReader

func CloseBodyReader(rc io.ReadCloser)

close w error check

func DoFormat

func DoFormat(bytes []byte, v common.Values, f string)

func RetryWait

func RetryWait(count int64, fn func() bool) bool

RetryWait retries a function up to 'count' times, waiting an increasing amount of time between each attempt. The wait time increases linearly (1s, 2s, 3s, ...). If the function returns true, it stops retrying and returns true. If all attempts fail, it returns false.

Types

type Client

type Client struct {
	HttpClient *http.Client
	Do         Do
	MaxRetry   int64
	StatusCode int
}

func NewClient

func NewClient() *Client

no friction wrapper

func RetriableClient

func RetriableClient(retries int64) *Client

retry enabled client

func RetriableClientWithStatus

func RetriableClientWithStatus(retries int64, status int) *Client

retry enabled client with specific status code to wait for e.g. wait for 200 OK

func (*Client) Get

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

get with retry

func (*Client) Post

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

post with retry

func (*Client) RetriableDo

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

retry logic

func (*Client) RetryWithBackoff

func (c *Client) RetryWithBackoff(logger *config.Log,
	i int64, resp *http.Response, err error) bool

determine if we should retry, and backoff if so returns true if we should retry sleeps for backoff duration if retrying

type Do

type Do func(req *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

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