Documentation ¶
Index ¶
- func DefaultRandMillis() time.Duration
- func DefaultRetryRule(resp *http.Response, err error) bool
- func MultipartFile(data io.Reader, name string) io.Reader
- func MultipartItem(data io.Reader, contentType string) io.Reader
- func Retry(ctx context.Context, baseDelay time.Duration, maxDelay time.Duration, ...)
- type Client
- func (c *Client) AddMiddleware(middlewares ...Middleware)
- func (c Client) Delete(ctx context.Context, url string, data RequestData) (Response, error)
- func (c Client) Get(ctx context.Context, url string, data RequestData) (Response, error)
- func (c Client) Patch(ctx context.Context, url string, data RequestData) (Response, error)
- func (c Client) Post(ctx context.Context, url string, data RequestData) (Response, error)
- func (c Client) Put(ctx context.Context, url string, data RequestData) (Response, error)
- type Middleware
- type Mock
- func (m Mock) Delete(ctx context.Context, url string, data RequestData) (resp Response, err error)
- func (m Mock) Get(ctx context.Context, url string, data RequestData) (resp Response, err error)
- func (m Mock) Patch(ctx context.Context, url string, data RequestData) (resp Response, err error)
- func (m Mock) Post(ctx context.Context, url string, data RequestData) (resp Response, err error)
- func (m Mock) Put(ctx context.Context, url string, data RequestData) (resp Response, err error)
- type MultipartData
- type NextMiddleware
- type Provider
- type RequestData
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultRandMillis ¶
DefaultRandMillis calculates retry random factor based on the following guide:
https://cloud.google.com/iot/docs/how-tos/exponential-backoff
func DefaultRetryRule ¶
DefaultRetryRule is the default retry rule that will retry (i.e. return true) if the request ends with an error, if the status is > 500 or if the status is one of: StatusLocked, StatusTooEarly and StatusTooManyRequests.
func MultipartFile ¶
MultipartFile is a helper for encoding files as a part of the multipart data payload
func MultipartItem ¶
MultipartItem is a helper for encoding items with specific content-types as a part of the multipart data payload
func Retry ¶
func Retry(ctx context.Context, baseDelay time.Duration, maxDelay time.Duration, maxRetries int, fn func() bool)
Retry retries the fn callback using an exponential backoff strategy starting from `baseDelay` and performing at most `maxRetries`
A retry is attempted only when the callback returns true.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client contains methods for making rest requests these methods accept any struct that can be marshaled into JSON but the response is returned in Bytes, since not all APIs follow rest strictly.
func New ¶
func New(timeout time.Duration, middlewares ...Middleware) Client
New instantiates a new rest client
func (*Client) AddMiddleware ¶ added in v0.0.3
func (c *Client) AddMiddleware(middlewares ...Middleware)
AddMiddleware adds one or more new middlewares to this instance
type Middleware ¶ added in v0.0.3
type Middleware func( ctx context.Context, method string, url string, data RequestData, next NextMiddleware, ) (Response, error)
Middleware describes the expected format for this package middlewares
type Mock ¶
type Mock struct { GetFn func(ctx context.Context, url string, data RequestData) (resp Response, err error) PostFn func(ctx context.Context, url string, data RequestData) (resp Response, err error) PutFn func(ctx context.Context, url string, data RequestData) (resp Response, err error) PatchFn func(ctx context.Context, url string, data RequestData) (resp Response, err error) DeleteFn func(ctx context.Context, url string, data RequestData) (resp Response, err error) }
Mock mocks the krest.Provider interface with a configurable structure
type MultipartData ¶
MultipartData is a helper type for storing the multipart data payload in a practical structure.
type NextMiddleware ¶ added in v0.0.3
type NextMiddleware func( ctx context.Context, method string, url string, data RequestData, ) (Response, error)
NextMiddleware describes the signature of the `next` middleware function
type Provider ¶
type Provider interface { Get(ctx context.Context, url string, data RequestData) (resp Response, err error) Post(ctx context.Context, url string, data RequestData) (resp Response, err error) Put(ctx context.Context, url string, data RequestData) (resp Response, err error) Patch(ctx context.Context, url string, data RequestData) (resp Response, err error) Delete(ctx context.Context, url string, data RequestData) (resp Response, err error) }
Provider describes the functions necessary to do all types of REST requests.
It returns error if it was not possible to complete the request or if the status code of the request was not in the range 200-299.
type RequestData ¶
type RequestData struct { // The body accepts any struct that can // be marshaled into JSON Body interface{} Headers map[string]any // It's the max number of retries, if 0 it defaults 1 MaxRetries int // The start and max delay for the exponential backoff strategy // if unset they default to 300ms and 32s respectively BaseRetryDelay time.Duration MaxRetryDelay time.Duration // Set this attribute if you want to personalize the retry behavior // if nil it defaults to `rest.DefaultRetryRule()` RetryRule func(resp *http.Response, err error) bool // Use this for setting up mutual TLS TLSConfig *tls.Config // Set this option to true if you // expect to receive big bodies of data // and you don't want this library to // to load all of it in memory. // // When using this option the resp.Body // field will be set to null and you'll // need to use the response struct as an io.ReadCloser // for streaming the data wherever you need to. // // Don't forget to close it afterwards. // // Also note that there is no need to call resp.Close() // if you are not using the Stream option or if the call // returns an error. Stream bool }
RequestData describes the optional arguments for all the http methods of this client.
func (*RequestData) SetDefaultsIfNecessary ¶
func (r *RequestData) SetDefaultsIfNecessary()
SetDefaultsIfNecessary sets the default values for the RequestData structure