Documentation ¶
Index ¶
- func Elapsed(fn func()) time.Duration
- type Client
- func (c *Client) DELETE(ctx context.Context, route ...string) *Request
- func (c *Client) GET(ctx context.Context, route ...string) *Request
- func (c *Client) PATCH(ctx context.Context, route ...string) *Request
- func (c *Client) POST(ctx context.Context, route ...string) *Request
- func (c *Client) PUT(ctx context.Context, route ...string) *Request
- func (c *Client) Request(ctx context.Context, method string, routes ...string) *Request
- type ClientOptions
- type FallbackPolicy
- type Request
- type RequestOption
- func WithRequestAuthorizationBasic(username, password string) RequestOption
- func WithRequestAuthorizationBearer(fn func(ctx context.Context) (string, error)) RequestOption
- func WithRequestBody(body io.Reader) RequestOption
- func WithRequestFormData(form map[string][]byte) RequestOption
- func WithRequestFormDataFile(filePath, field string, opts ...func(content []byte) []byte) RequestOption
- func WithRequestFormURLEncoded(form map[string][]string) RequestOption
- func WithRequestHeader(key string, value any) RequestOption
- func WithRequestJSON(object any) RequestOption
- func WithRequestOptions(opts ...RequestOption) RequestOption
- func WithRequestRetryPolicy(retries int, duration time.Duration, policy FallbackPolicy, statuscodes ...int) RequestOption
- func WithRequestTimeout(duration time.Duration) RequestOption
- func WithRequestURL(rawUrl string) RequestOption
- func WithRequestURLQuery(query map[string][]any) RequestOption
- func WithRequestXML(object any) RequestOption
- type Response
- type ResponseOption
- func WithResponseBody[T any](object *T, unmarshaler func(data []byte, v any) error, statuscodes ...int) ResponseOption
- func WithResponseJSON[T any](object *T, statuscodes ...int) ResponseOption
- func WithResponseStatusCodeAssertion(statusCodes ...int) ResponseOption
- func WithResponseXML[T any](object *T, statuscodes ...int) ResponseOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
ClientOptions is a callback signature for modifying client options.
func New ¶
func New(opts ...ClientOptions) *Client
New initializes a default client. Provide ClientOptions to modify default behavior.
type ClientOptions ¶
type ClientOptions func(client *Client)
ClientOptions is a callback signature for modifying client options.
func WithBaseURL ¶
func WithBaseURL(url string) ClientOptions
WithBaseURL sets a base URL which will be the prefix for all outbound HTTP requests.
func WithClient ¶
func WithClient(httpClient *http.Client) ClientOptions
WithClient sets the client to the given HTTP client instance.
type FallbackPolicy ¶
type FallbackPolicy int
FallbackPolicy specifies the cooldown strategy for failed request issuing an identical request of a failing request
const ( // FallbackPolicyLinear waits for issuing a new request by // given duration multiplied by the attempt. FallbackPolicyLinear FallbackPolicy = iota // FallbackPolicyExponential waits for issuing a new request by // given attempt multiplied with itself and attempt. FallbackPolicyExponential )
type Request ¶
type Request struct { // Request is the underlying standard HTTP request being made. *http.Request // Client is the HTTP client used to perform the request. *http.Client // Error stores any errors generated when creating the request. Error error // Retries specifies the number of times the request will be retried in case of failure. Retries int // FallbackDuration is the duration to wait before attempting the request again. FallbackDuration time.Duration // FallbackPolicy represents the policy used for fallback requests. FallbackPolicy FallbackPolicy // FallbackStatusCodes contains a list of HTTP status codes that will // trigger a new request. FallbackStatusCodes []int }
Request is a wrapper around the standard http.Request providing additional features.
func (*Request) Dry ¶
func (r *Request) Dry(opts ...RequestOption) (err error)
Dry performs a dry run of the request without actually executing it.
type RequestOption ¶
RequestOption callback signature for modifying request
func WithRequestAuthorizationBasic ¶
func WithRequestAuthorizationBasic(username, password string) RequestOption
WithRequestAuthorizationBasic encodes the credentials with basic HTTP authentication. It sets the valkue in the Authorization HTTP header.
func WithRequestAuthorizationBearer ¶
func WithRequestAuthorizationBearer(fn func(ctx context.Context) (string, error)) RequestOption
WithRequestAuthorizationBearer executes the callback to fetch a token, the token from the result will be set in the Authorization header
func WithRequestBody ¶
func WithRequestBody(body io.Reader) RequestOption
WithRequestBody sets the request body.
func WithRequestFormData ¶
func WithRequestFormData(form map[string][]byte) RequestOption
WithRequestFormData writes the content to body using the multipart writer.
func WithRequestFormDataFile ¶
func WithRequestFormDataFile(filePath, field string, opts ...func(content []byte) []byte) RequestOption
WithRequestFormDataFile reads the given files and writes it as multipart form. the functional options allows you to mutate the file content before it's being written.
func WithRequestFormURLEncoded ¶
func WithRequestFormURLEncoded(form map[string][]string) RequestOption
WithRequestFormURLEncoded sets the request body as form-urlencoded.
func WithRequestHeader ¶
func WithRequestHeader(key string, value any) RequestOption
WithRequestHeader sets key value as HTTP header in the request.
func WithRequestJSON ¶
func WithRequestJSON(object any) RequestOption
WithRequestJSON JSON serializes the object and sets the request body as JSON.
func WithRequestOptions ¶
func WithRequestOptions(opts ...RequestOption) RequestOption
WithRequestOptions composes multiple request options.
func WithRequestRetryPolicy ¶
func WithRequestRetryPolicy(retries int, duration time.Duration, policy FallbackPolicy, statuscodes ...int) RequestOption
WithRequestRetryPolicy sets the retry policy for the request.
func WithRequestTimeout ¶
func WithRequestTimeout(duration time.Duration) RequestOption
WithRequestTimeout sets the timeout duration for the request.
func WithRequestURL ¶
func WithRequestURL(rawUrl string) RequestOption
WithRequestURL sets the URL for the request.
func WithRequestURLQuery ¶
func WithRequestURLQuery(query map[string][]any) RequestOption
WithRequestURLQuery sets the URL query parameters for the request.
func WithRequestXML ¶
func WithRequestXML(object any) RequestOption
WithRequestXML XML serializes the object and sets the request body as XML.
type Response ¶
Response is a wrapper around the standard http.Response providing additional features.
func (*Response) Handle ¶
func (r *Response) Handle(opts ...ResponseOption) error
Handle executes the response handling options. If there is an error associated with the response, it returns that error.
type ResponseOption ¶
ResponseOption is a callback signature for modifying response options.
func WithResponseBody ¶
func WithResponseBody[T any](object *T, unmarshaler func(data []byte, v any) error, statuscodes ...int) ResponseOption
WithUnmarshalXML unmarshals the response body to an object using the given unmarshaler. The object parameter should be a pointer to the target type. It will only attempt to deserialize the payload if the response has one of the provided status codes. If the list of status codes is empty, it will attempt to deserialize for all status codes.
func WithResponseJSON ¶
func WithResponseJSON[T any](object *T, statuscodes ...int) ResponseOption
WithResponseJSON unmarshals the JSON response body to an object. The object parameter should be a pointer to the target type. It will only attempt to deserialize the payload if the response has one of the provided status codes. If the list of status codes is empty, it will attempt to deserialize for all status codes.
func WithResponseStatusCodeAssertion ¶
func WithResponseStatusCodeAssertion(statusCodes ...int) ResponseOption
WithResponseStatusCodeAssertion checks if the response status code matches any of the specified codes. If it does, it returns nil. Otherwise, it provides an error message.
func WithResponseXML ¶
func WithResponseXML[T any](object *T, statuscodes ...int) ResponseOption
WithResponseXML unmarshals the XML response body to an object. The object parameter should be a pointer to the target type. It will only attempt to deserialize the payload if the response has one of the provided status codes. If the list of status codes is empty, it will attempt to deserialize for all status codes.