Documentation ¶
Overview ¶
Package httpclient is a high level wrapper around net/http with more types and easier to use interface TODO: ref https://github.com/bradfitz/exp-httpclient
Index ¶
- Constants
- func DrainAndClose(res *http.Response)
- func DrainResponseBody(res *http.Response) ([]byte, error)
- func IsSuccess(status int) bool
- func JoinPath(base string, sub string) string
- type BasicErrorHandler
- type Client
- func (c *Client) Delete(ctx *Context, path string, resBody interface{}) error
- func (c *Client) DeleteIgnoreRes(ctx *Context, path string) error
- func (c *Client) DeleteRaw(ctx *Context, path string) (*http.Response, error)
- func (c *Client) Do(ctx *Context, method httputil.Method, path string, reqBody interface{}) (*http.Response, error)
- func (c *Client) FetchTo(ctx *Context, method httputil.Method, path string, reqBody interface{}, ...) error
- func (c *Client) FetchToNull(ctx *Context, method httputil.Method, path string, reqBody interface{}) error
- func (c *Client) Get(ctx *Context, path string, resBody interface{}) error
- func (c *Client) GetHeaders() map[string]string
- func (c *Client) GetIgnoreRes(ctx *Context, path string) error
- func (c *Client) GetRaw(ctx *Context, path string) (*http.Response, error)
- func (c *Client) NewRequest(ctx *Context, method httputil.Method, path string, reqBody interface{}) (*http.Request, error)
- func (c *Client) Patch(ctx *Context, path string, reqBody interface{}, resBody interface{}) error
- func (c *Client) PatchIgnoreRes(ctx *Context, path string, reqBody interface{}) error
- func (c *Client) PatchRaw(ctx *Context, path string, reqBody interface{}) (*http.Response, error)
- func (c *Client) Post(ctx *Context, path string, reqBody interface{}, resBody interface{}) error
- func (c *Client) PostIgnoreRes(ctx *Context, path string, reqBody interface{}) error
- func (c *Client) PostRaw(ctx *Context, path string, reqBody interface{}) (*http.Response, error)
- func (c *Client) SetHeader(k, v string) *Client
- func (c *Client) Transport() (tr *http.Transport, ok bool)
- type Context
- func (c *Context) Deadline() (deadline time.Time, ok bool)
- func (c *Context) Done() <-chan struct{}
- func (c *Context) Err() error
- func (c *Context) SetBase(s string) *Context
- func (c *Context) SetErrorHandler(h ErrorHandler) *Context
- func (c *Context) SetHeader(k, v string) *Context
- func (c *Context) SetParam(k, v string) *Context
- func (c *Context) Value(key interface{}) interface{}
- type ErrApplication
- type ErrDecoding
- type ErrorHandler
- type ErrorHandlerFunc
- type Option
Constants ¶
const UnixBasePath = "http://localhost"
UnixBasePath is used as a placeholder for unix domain socket client, only the protocol http is needed, host is ignored because dialer use socket path TODO: what about tls over unix domain socket?
Variables ¶
This section is empty.
Functions ¶
func DrainAndClose ¶
DrainAndClose ignore all the errors when drain response body to /dev/null and close it It should be used when are using a raw http.Response and don't want to write server lines for draining and close the body
You should use FetchToNull most of the time, this only useful when you need some header and don't care about the body
func DrainResponseBody ¶
DrainResponseBody reads entire response into a byte slice and close the underlying body so the connection can be reused.
It replace res.Body with a bytes.Reader so it can be passed to func that reads res.Body
Types ¶
type BasicErrorHandler ¶
type BasicErrorHandler struct { }
BasicErrorHandler use value receiver because it does not have any fields
func (BasicErrorHandler) DecodeError ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func NewDefault ¶ added in v0.0.3
NewDefault creates a dev client that use a shared un pooled http.Transport
func NewDefaultSkipVerify ¶ added in v0.0.3
NewDefaultSkipVerify is same as NewDefault but disabled tls certificate verify. You should NOT use it production, it is useful for testing with self signed certs TODO: add links on how to generate and use self signed cert
func (*Client) Do ¶
func (c *Client) Do(ctx *Context, method httputil.Method, path string, reqBody interface{}) (*http.Response, error)
Do handles application level error, default is based on status code and drain entire body as string. User can give a handler when create client or have request specific handler using context
It does not drain response body, use FetchTo if want to decode body as json
func (*Client) FetchToNull ¶
func (*Client) GetHeaders ¶ added in v0.0.3
GetHeaders a copy of headers set on the client, it will return a empty but non nil map even if no header is set
func (*Client) NewRequest ¶
func (c *Client) NewRequest(ctx *Context, method httputil.Method, path string, reqBody interface{}) (*http.Request, error)
NewRequest create a http.Request by concat base path in client and path, it will encode the body if the body is not already encoded and it's a json client
You should use high level wrapper like FetchTo most of the time
func (*Client) PatchIgnoreRes ¶
func (*Client) PostIgnoreRes ¶
func (*Client) Transport ¶
Transport returns the transport of the http.Client being used for user to modify tls config etc. It returns (nil, false) if the transport is the default transport or the type didn't match. You should NOT use http.DefaultTransport in the first place, and modify it is even worse
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context
It is lazy initialized, only call `make` when they are actually write to, so all the maps are EMPTY even when using factory func. User (including this package itself) should use setter when set value.
func Bkg ¶
func Bkg() *Context
Bkg returns a Context that does not embed context.Context, it behaves like context.Background(), however we can't use a singleton like context package because Context in httpclient can be modified in place to store req/response body etc. So we always return pointer to a fresh new instance.
We return pointer because it is meant to be modified along the way, it is not immutable like context.Context Also the context.Context is implemented using pointer receiver
func ConvertContext ¶
ConvertContext returns the original context if it is already *httpclient.Context, Otherwise it wraps the context using NewContext
func NewContext ¶
NewContext returns a context that embed a context.Context
func (*Context) Done ¶
func (c *Context) Done() <-chan struct{}
Done returns Done() from underlying context.Context if set
func (*Context) SetErrorHandler ¶
func (c *Context) SetErrorHandler(h ErrorHandler) *Context
type ErrApplication ¶
type ErrApplication struct { Status int Method httputil.Method // Url is the full url including schema, host and query parameters Url string // Path is just the api path without host etc. Path string // Body is string instead of []byte because it's immutable Body string }
func (*ErrApplication) Error ¶
func (e *ErrApplication) Error() string
type ErrDecoding ¶
func (*ErrDecoding) Error ¶
func (e *ErrDecoding) Error() string
type ErrorHandler ¶
type ErrorHandler interface { IsError(status int, res *http.Response) bool DecodeError(status int, body []byte, res *http.Response) (decodedError error) }
func DefaultHandler ¶
func DefaultHandler() ErrorHandler
type ErrorHandlerFunc ¶
ErrorHandlerFunc use status code for IsError
func (ErrorHandlerFunc) DecodeError ¶
type Option ¶
func WithErrorHandler ¶
func WithErrorHandler(h ErrorHandler) Option
func WithErrorHandlerFunc ¶
func WithErrorHandlerFunc(f ErrorHandlerFunc) Option