Documentation ¶
Index ¶
- func RetryIfNotSuccess(resp *http.Response) bool
- func ReusableReader(r io.ReadCloser) (io.ReadCloser, error)
- type Client
- func (c *Client) DELETE(ctx context.Context, endpoint string, options RequestOptions) (Response, error)
- func (c *Client) GET(ctx context.Context, endpoint string, options RequestOptions) (Response, error)
- func (c *Client) POST(ctx context.Context, endpoint string, body io.Reader, options RequestOptions) (Response, error)
- func (c *Client) PUT(ctx context.Context, endpoint string, body io.Reader, options RequestOptions) (Response, error)
- func (c *Client) SetHeader(key, value string)
- type Clock
- type ConcurrentRequestLimiter
- type HTTPListener
- type Option
- type RateLimiter
- type RequestInfo
- type RequestOptions
- type RequestResponse
- type RequestRetrier
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RetryIfNotSuccess ¶
RetryIfNotSuccess implements a basic retry function for a RequestRetrier which will retry on any non 2xx status code
func ReusableReader ¶
func ReusableReader(r io.ReadCloser) (io.ReadCloser, error)
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents a general HTTP client
func (*Client) DELETE ¶
func (c *Client) DELETE(ctx context.Context, endpoint string, options RequestOptions) (Response, error)
DELETE sends a DELETE request to the specified endpoint. If you wish to receive logs from this method supply a logger inside the context using logr.NewContext.
func (*Client) GET ¶
func (c *Client) GET(ctx context.Context, endpoint string, options RequestOptions) (Response, error)
GET sends a GET request to the specified endpoint. If you wish to receive logs from this method supply a logger inside the context using logr.NewContext.
func (*Client) POST ¶
func (c *Client) POST(ctx context.Context, endpoint string, body io.Reader, options RequestOptions) (Response, error)
POST sends a POST request to the specified endpoint with the given body. If you wish to receive logs from this method supply a logger inside the context using logr.NewContext.
func (*Client) PUT ¶
func (c *Client) PUT(ctx context.Context, endpoint string, body io.Reader, options RequestOptions) (Response, error)
PUT sends a PUT request to the specified endpoint with the given body. If you wish to receive logs from this method supply a logger inside the context using logr.NewContext.
type ConcurrentRequestLimiter ¶
type ConcurrentRequestLimiter struct {
// contains filtered or unexported fields
}
ConcurrentRequestLimiter represents a component for limiting concurrent requests.
func NewConcurrentRequestLimiter ¶
func NewConcurrentRequestLimiter(maxConcurrent int) *ConcurrentRequestLimiter
NewConcurrentRequestLimiter creates a new instance of ConcurrentRequestLimiter with the specified limit to apply.
func (*ConcurrentRequestLimiter) Acquire ¶
func (c *ConcurrentRequestLimiter) Acquire()
Acquire acquires a slot from the concurrent request limiter to check for maximum concurrent requests.
func (*ConcurrentRequestLimiter) Release ¶
func (c *ConcurrentRequestLimiter) Release()
Release releases a slot from the concurrent request limiter to allow subsequent requests to proceed.
type HTTPListener ¶
type HTTPListener struct {
Callback func(response RequestResponse)
}
HTTPListener is a struct that can be used to listen for HTTP requests and responses and invoke a user-defined callback function.
type Option ¶
type Option func(*Client)
Option represents a functional Option for the Client.
func WithConcurrentRequestLimit ¶
WithConcurrentRequestLimit sets the maximum number of concurrent requests allowed.
func WithHTTPListener ¶
func WithHTTPListener(listener *HTTPListener) Option
WithHTTPListener sets the HTTPListener for the Client.
func WithRateLimiter ¶
func WithRateLimiter() Option
WithRateLimiter activates a RateLimiter for the Client. The RateLimiter will block subsequent Client calls after a 429 status code is received until the mandated reset time is reached. If the server should not reply with an X-RateLimit-Reset header, a default delay is enforced. Note that a Client with RateLimiter will not automatically retry an API call after a limit was hit, but return the Too Many Requests 429 Response to you. If wish for the Client to retry on errors configure a RequestRetrier as well.
func WithRequestRetrier ¶
func WithRequestRetrier(retrier *RequestRetrier) Option
WithRequestRetrier sets the RequestRetrier for the Client.
func WithTimeout ¶
WithTimeout sets the request timeout for the Client.
type RateLimiter ¶
func NewRateLimiter ¶
func NewRateLimiter() *RateLimiter
func (*RateLimiter) Wait ¶
func (rl *RateLimiter) Wait(ctx context.Context)
Wait blocks in case a hard API limit was reached, or the request/second limit was exceeded. In case of a hard limit, the method will block until after its reset time is reached. In case of the soft request/second limit it will block until requests are available again. See rate.Limiter for details on how soft-limits works.
type RequestInfo ¶
type RequestInfo struct { Method string `json:"method"` // The HTTP method of the request. URL string `json:"url"` // The full URL of the request }
RequestInfo holds information about the original request that led to this response
type RequestOptions ¶
type RequestOptions struct { // QueryParams are HTTP query parameters that shall be appended // to the endpoint url QueryParams url.Values }
RequestOptions are additional options that should be applied to a request
type RequestResponse ¶
type RequestResponse struct { ID string // ID to identify and correlate requests to responses Timestamp time.Time // Timestamp of the recorded request/response Request *http.Request // HTTP request Response *http.Response // HTTP response Error error // Error, if any, during request/response }
RequestResponse represents a recorded HTTP request and response.
func (*RequestResponse) IsRequest ¶
func (r *RequestResponse) IsRequest() (*http.Request, bool)
IsRequest checks if the RequestResponse value represents an HTTP request.
It returns a pointer to an http.Request and true if the RequestResponse contains a request. If the RequestResponse does not contain a request, it returns nil and false.
func (*RequestResponse) IsResponse ¶
func (r *RequestResponse) IsResponse() (*http.Response, bool)
IsResponse checks if the RequestResponse value represents an HTTP response.
It returns a pointer to an http.Response and true if the RequestResponse contains a response. If the RequestResponse does not contain a response, it returns nil and false.
type RequestRetrier ¶
RequestRetrier represents a component for retrying failed HTTP requests.
type Response ¶
type Response struct { // Payload contains the body payload of the response. Payload []byte // StatusCode is the HTTP status code of the response. StatusCode int // RequestInfo holds information about the original request that led to this response RequestInfo RequestInfo }
Response represents an HTTP response returned by the server.
func (Response) Is4xxError ¶
Is4xxError returns true if the response indicates a 4xx client error HTTP status code. A status code between 400 and 499 (inclusive) is considered a client error.
func (Response) Is5xxError ¶
Is5xxError returns true if the response indicates a 5xx server error HTTP status code. A status code between 500 and 599 (inclusive) is considered a server error.