httpc

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidEncodeFn = errors.New("no encode fn provided for body")

ErrInvalidEncodeFn is an error that is returned when calling the Request Do and the encode function is not set.

Functions

This section is empty.

Types

type AuthFn

type AuthFn func(*http.Request) *http.Request

AuthFn adds authorization to an http request.

func BasicAuth

func BasicAuth(user, pass string) AuthFn

BasicAuth sets the basic authFn on the request.

func BearerTokenAuth

func BearerTokenAuth(token string) AuthFn

BearerTokenAuth sets the token authentication on the request.

func HeaderAuth

func HeaderAuth(header, credential string) AuthFn

HeaderAuth sets the header credential on each request.

type Client

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

Client is the httpc client. The client sets the default backoff and encode func on the request that are created when making an http call. Those defaults can be overridden in the request builder.

func New

func New(doer Doer, opts ...ClientOptFn) *Client

New returns a new client.

func (*Client) DELETE

func (c *Client) DELETE(addr string) *Request

DELETE makes a delete request.

func (*Client) GET

func (c *Client) GET(addr string) *Request

GET makes a get request.

func (*Client) HEAD

func (c *Client) HEAD(addr string) *Request

HEAD makes a head request.

func (*Client) PATCH

func (c *Client) PATCH(addr string) *Request

PATCH makes a patch request.

func (*Client) POST

func (c *Client) POST(addr string) *Request

POST makes a post request.

func (*Client) PUT

func (c *Client) PUT(addr string) *Request

PUT makes a put request.

func (*Client) Req

func (c *Client) Req(method, addr string) *Request

Req makes an http request.

type ClientOptFn

type ClientOptFn func(Client) Client

ClientOptFn sets keys on a client type.

func WithAuth

func WithAuth(authFn AuthFn) ClientOptFn

WithAuth sets the authorization func on the client type, and will be used as the default authFn for all requests from this client unless overwritten atn the request lvl.

func WithBackoff

func WithBackoff(b backoff.Backoffer) ClientOptFn

WithBackoff sets the backoff on the client.

func WithBaseURL

func WithBaseURL(baseURL string) ClientOptFn

WithBaseURL sets the base url for all requests. Any path provided will be appended to this WithBaseURL.

func WithEncode

func WithEncode(fn EncodeFn) ClientOptFn

WithEncode sets the encode func for the client.

func WithResetSeekerToZero

func WithResetSeekerToZero() ClientOptFn

WithResetSeekerToZero sets the seek params to zero for all future requests Useful if Body param is a ReadSeeker and should be reset on retry

func WithRetryClientTimeouts

func WithRetryClientTimeouts() ClientOptFn

WithRetryClientTimeouts sets the response retry mechanism. Useful if you want to retry on a client timeout or something of that nature.

func WithRetryResponseErrors

func WithRetryResponseErrors() ClientOptFn

WithRetryResponseErrors sets the response retry mechanism.

type DecodeFn

type DecodeFn func(r io.Reader) error

DecodeFn is a decoder func.

func GobDecode

func GobDecode(v interface{}) DecodeFn

GobDecode sets the client's decodeFn to a gob decoder.

func JSONDecode

func JSONDecode(v interface{}) DecodeFn

JSONDecode sets the client's decodeFn to a json decoder.

type Doer

type Doer interface {
	Do(*http.Request) (*http.Response, error)
}

Doer is an abstraction around a http client.

type EncodeFn

type EncodeFn func(interface{}) (io.Reader, error)

EncodeFn is an encoder func.

func GobEncode

func GobEncode() EncodeFn

GobEncode sets the client's encodeFn to a gob encoder.

func JSONEncode

func JSONEncode() EncodeFn

JSONEncode sets the client's encodeFn to a json encoder.

type Request

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

Request is built up to create an http request.

func (*Request) Auth

func (r *Request) Auth(authFn AuthFn) *Request

Auth sets the authorization for hte request, overriding the authFn set by the client.

func (*Request) Backoff

func (r *Request) Backoff(b backoff.Backoffer) *Request

Backoff sets the backoff of the Request.

func (*Request) Body

func (r *Request) Body(v interface{}) *Request

Body sets the body of the Request.

func (*Request) ContentLength

func (r *Request) ContentLength(t int) *Request

ContentLength sets the content length on the request.

func (*Request) ContentType

func (r *Request) ContentType(t string) *Request

ContentType sets the content type on the request.

func (*Request) Decode

func (r *Request) Decode(fn DecodeFn) *Request

Decode sets the decoder func for the Request.

func (*Request) DecodeJSON

func (r *Request) DecodeJSON(v interface{}) *Request

DecodeJSON is is a short hand for decoding to JSON.

func (*Request) Do

func (r *Request) Do(ctx context.Context) error

Do makes the http request and applies the backoff.

func (*Request) DoAndGetReader added in v0.0.37

func (r *Request) DoAndGetReader(ctx context.Context) (*http.Response, error)

DoAndGetReader makes the http request and does not close the body in the http.Response that is returned

func (*Request) Encode

func (r *Request) Encode(fn EncodeFn) *Request

Encode sets the encoder func for the Request.

func (*Request) Exists

func (r *Request) Exists(fn StatusFn) *Request

Exists appends a exists func to the Request.

func (*Request) Header

func (r *Request) Header(key, value string) *Request

Header adds a header to the request.

func (*Request) Meta

func (r *Request) Meta(key, value string, pairs ...string) *Request

Meta adds k/v pairs to the eror message be added in the event of an error.

func (*Request) NotFound

func (r *Request) NotFound(fn StatusFn) *Request

NotFound appends a not found func to the Request.

func (*Request) OnError

func (r *Request) OnError(fn DecodeFn) *Request

OnError provides a decode hook to decode a responses body.

func (*Request) QueryParam

func (r *Request) QueryParam(key, value string) *Request

QueryParam allows a user to set query params on their request. This can be called numerous times. Will add keys for each value that is passed in here. In the case of duplicate query param values, the last pair that is entered will be set and the former will not be available.

func (*Request) QueryParams

func (r *Request) QueryParams(key, value string, pairs ...string) *Request

QueryParams allows a user to set multiple query params at one time. Following the same rules as QueryParam. If a key is provided without a value, it will not be added to the request. If it is desired, pass in "" to add a query param with no string field.

func (*Request) ResponseHeaders

func (r *Request) ResponseHeaders(fn ResponseHeadersFn) *Request

ResponseHeaders provides a hook to get the headers of a response

func (*Request) Retry

func (r *Request) Retry(fn RetryFn) *Request

Retry sets the retry policy(s) on the request.

func (*Request) RetryResponseErrors

func (r *Request) RetryResponseErrors() *Request

RetryResponseErrors tells the request to retry execution errors.

func (*Request) RetryStatus

func (r *Request) RetryStatus(fn StatusFn) *Request

RetryStatus sets the retry policy(s) on the request.

func (*Request) RetryStatusNotIn

func (r *Request) RetryStatusNotIn(status int, rest ...int) *Request

RetryStatusNotIn sets the retry policy(s) to retry if the response status matches that of the given args.

func (*Request) SeekParams

func (r *Request) SeekParams(offset int64, whence int) *Request

SeekParams sets the seek params. this is useful in cases where the body is a seeker and needs to be reset on retry

func (*Request) Success

func (r *Request) Success(fn StatusFn) *Request

Success appends a success func to the Request.

type ResponseErrorFn

type ResponseErrorFn func(error) error

ResponseErrorFn is a response error function that can be used to provide behavior when a response fails to "Do".

type ResponseHeadersFn

type ResponseHeadersFn func(header http.Header)

ResponseHeadersFn is a function that can be used to retrieve headers from a response

type RetryFn

type RetryFn func(*Request) *Request

RetryFn will apply a retry policy to a request.

func RetryClientTimeout

func RetryClientTimeout() RetryFn

RetryClientTimeout will retry the request if the request had been canceled by the http client.

func RetryResponseError

func RetryResponseError(fn ResponseErrorFn) RetryFn

RetryResponseError sets a function to be called on all response errors.

func RetryStatus

func RetryStatus(fn StatusFn) RetryFn

RetryStatus appends a retry func to the Request.

type StatusFn

type StatusFn func(statusCode int) bool

StatusFn is func for comparing expected status code against an expected status code.

func StatusAccepted

func StatusAccepted() StatusFn

StatusAccepted compares the response's status code to match Status Accepted.

func StatusCreated

func StatusCreated() StatusFn

StatusCreated compares the response's status code to match Status Created.

func StatusForbidden

func StatusForbidden() StatusFn

StatusForbidden compares the response's status code to match Status Forbidden.

func StatusIn

func StatusIn(status int, others ...int) StatusFn

StatusIn checks whether the response's status code matches at least 1 of the input status codes provided.

func StatusInRange

func StatusInRange(low, high int) StatusFn

StatusInRange checks the response's status code is in in the range provided. The range is [low, high).

func StatusInternalServerError

func StatusInternalServerError() StatusFn

StatusInternalServerError compares the response's status code to match Status Internal Server Error.

func StatusNoContent

func StatusNoContent() StatusFn

StatusNoContent compares the response's status code to match Status No Content.

func StatusNotFound

func StatusNotFound() StatusFn

StatusNotFound compares the response's status code to match Status Not Found.

func StatusNotIn

func StatusNotIn(status int, others ...int) StatusFn

StatusNotIn checks whether the response's status code does match any of the input status codes provided.

func StatusOK

func StatusOK() StatusFn

StatusOK compares the response's status code to match Status OK.

func StatusPartialContent added in v0.0.37

func StatusPartialContent() StatusFn

StatusPartialContent compares the response's status code to match Status Partial Content

func StatusSuccessfulRange added in v0.0.37

func StatusSuccessfulRange() StatusFn

StatusSuccessfulRange compares the response's status code to match Status SuccessfulRange.

func StatusUnprocessableEntity

func StatusUnprocessableEntity() StatusFn

StatusUnprocessableEntity compares the response's status code to match Status Unprocessable Entity.

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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