Documentation
¶
Index ¶
- Variables
- func JsonMarshaler(body any) (dat []byte, hdrs map[string][]string, err error)
- func UrlEncodedMarshaler(body any) (dat []byte, hdrs map[string][]string, err error)
- func XmlMarshaler(body any) (dat []byte, hdrs map[string][]string, err error)
- type Client
- func (c *Client) CloseIdleConnections()
- func (c *Client) Do(req *http.Request) (res *http.Response, err error)
- func (c *Client) Get(url string) (res *http.Response, err error)
- func (c *Client) Head(url string) (res *http.Response, err error)
- func (c *Client) Post(url string, contentType string, body io.Reader) (res *http.Response, err error)
- func (c *Client) PostForm(url string, data url.Values) (res *http.Response, err error)
- func (c *Client) Use(middlewares ...func(*Context))
- type Context
- type Marshaler
- type RequestBuilder
- func (rb *RequestBuilder) Build(ctx context.Context) (res *http.Request, err error)
- func (rb *RequestBuilder) WithBody(body any, marshaler Marshaler) *RequestBuilder
- func (rb *RequestBuilder) WithHeader(key string, val string) *RequestBuilder
- func (rb *RequestBuilder) WithPathParameters(params ...string) *RequestBuilder
- func (rb *RequestBuilder) WithQueryParameter(key string, vals []string) *RequestBuilder
- func (rb *RequestBuilder) WithRawBody(body []byte) *RequestBuilder
- type Requester
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidBodyType = errors.New("invalid body type")
ErrInvalidBodyType is returned by Marshaler functions when the object can not be marshaled into a byte array due to its type, or the RequestBuilder when the body requires a marshaler but none is provided
var ErrInvalidFormat = errors.New("invalid endpoint format")
ErrInvalidFormat is returned by RequestBuilder.Build when the format has a trailing or incomplate placeholder {}, or if the number of placeholders does not match the number of path parameters
Functions ¶
func JsonMarshaler ¶ added in v0.3.0
JsonMarshaler uses the standard encoding/json marshaler to return the json encoded body and a Content-Type application/json header.
func UrlEncodedMarshaler ¶ added in v0.3.0
UrlEncodedMarshaler uses the standard net/url encoder to return the url encoded body and a Content-Type application/x-www-form-urlencoded header.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps an http Client with additional features.
func NewDefaultClient ¶ added in v0.3.0
func NewDefaultClient() *Client
NewDefaultClient creates a Client from http.DefaultClient.
func (*Client) CloseIdleConnections ¶ added in v0.3.0
func (c *Client) CloseIdleConnections()
CloseIdleConnections closes idle connections on the underlying Requester.
func (*Client) Post ¶
func (c *Client) Post( url string, contentType string, body io.Reader, ) (res *http.Response, err error)
Post sends a POST HTTP request to the specified URL with a content type header and a request body.
type Context ¶ added in v0.3.0
type Context struct {
Request *http.Request
Response *http.Response
Values map[string]any
Errors []error
// contains filtered or unexported fields
}
Context stores details about a request. It does not implement context.Context.
func (*Context) Del ¶ added in v0.3.0
Del removes some value from the context's store by a key. The operation locks the context's mutex for thread safety.
func (*Context) Get ¶ added in v0.3.0
Get retrieves some value from the context's store by a key. The operation locks the context's mutex for thread safety.
func (*Context) Lock ¶ added in v0.3.0
func (ctx *Context) Lock()
Lock locks the mutex in the context.
func (*Context) Next ¶ added in v0.3.0
func (ctx *Context) Next()
Next runs the next middleware function on the context. If there are no more functions, it does nothing.
type Marshaler ¶
Marshaler defines how to process an object into byte array for a request's body, with additional optional headers to set.
type RequestBuilder ¶ added in v0.2.0
type RequestBuilder struct {
// contains filtered or unexported fields
}
RequestBuilder allows gradual creation of http requests with functions to attach a body, headers, query parameters and path parameters.
func NewRequest ¶ added in v0.3.0
func NewRequest( method string, format string, ) *RequestBuilder
NewRequest creates a request builder.
func (*RequestBuilder) Build ¶ added in v0.3.0
Build returns a *http.Request from the values of the request builder.
func (*RequestBuilder) WithBody ¶ added in v0.2.0
func (rb *RequestBuilder) WithBody( body any, marshaler Marshaler, ) *RequestBuilder
WithBody adds a body and a marshaler to the request. If a body or marshaler is already set, it will overwrite it. The headers returned by the marshaler will not overwrite headers set by [WithHeader] or [WithHeaders]
func (*RequestBuilder) WithHeader ¶ added in v0.2.0
func (rb *RequestBuilder) WithHeader( key string, val string, ) *RequestBuilder
WithHeader adds a header to the request. If there was already a header set with the same key, it will overwrite it.
func (*RequestBuilder) WithPathParameters ¶ added in v0.2.0
func (rb *RequestBuilder) WithPathParameters( params ...string, ) *RequestBuilder
WithPathParameter adds path parameters to the request. The parameters get escaped and appended to the list in the request builder. Path parameters replace {} placeholders in the request endpoint in the order they were added.
func (*RequestBuilder) WithQueryParameter ¶ added in v0.2.0
func (rb *RequestBuilder) WithQueryParameter( key string, vals []string, ) *RequestBuilder
WithQueryParameter adds a query parameter to the request. If there was already a parameter set with the same key, it will overwrite it.
func (*RequestBuilder) WithRawBody ¶ added in v0.3.0
func (rb *RequestBuilder) WithRawBody( body []byte, ) *RequestBuilder
WithRawBody sets a byte array as the request body. If a body or marshaler is already set, it will overwrite it.