Documentation
¶
Overview ¶
Package jsonhttpc provides a way to easily send and receive HTTP requests with JSON bodies.
Index ¶
- Variables
- func Do(ctx context.Context, method, pathOrURL string, body, receiver interface{}) error
- func Path(s string, p Params) string
- type Client
- func (c *Client) Clone() *Client
- func (c *Client) Do(ctx context.Context, method, pathOrURL string, body, receiver interface{}) error
- func (c *Client) WithBaseURL(u *url.URL) *Client
- func (c *Client) WithClient(hc *http.Client) *Client
- func (c *Client) WithHeader(h http.Header) *Client
- func (c *Client) WithRequestLogger(l RequestLogger) *Client
- func (c *Client) WithRequestResponseLogger(l RequestResponseLogger) *Client
- func (c *Client) WithResponseLogger(l ResponseLogger) *Client
- type Error
- type Params
- type RequestLogger
- type RequestResponseLogger
- type ResponseLogger
- type SystemError
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEmptyResponse is received empty response unexpectedly. // Ex. StatusCode shows success but Content-Length is 0. // This is a server side error. ErrEmptyResponse = errors.New("unexpected empty response") // ErrReceiverAbsence means `receiver` is nil. // Server responded with body, but user did't provide any receivers. // This is a user side error. ErrReceiverAbsence = errors.New("receiver absence") )
var DefaultClient = &Client{}
DefaultClient is used for `jsonhttpc.Do()` for convenience.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a JSON specialized HTTP client.
func New ¶
New creates a new Client with base URL. You can pass nil to baseURL, in which case you should pass a complete URL to the pathOrURL argument of Do() function.
func (*Client) Do ¶
func (c *Client) Do(ctx context.Context, method, pathOrURL string, body, receiver interface{}) error
Do makes a HTTP request to the server with JSON body, and parse a JSON in response body.
`ctx` can be nil, in that case it use `context.Background()` instead.
This returns an error when `pathOrURL` starts with "jsonhttpc.Parse error: " to treat `Path()`'s failure as error.
func (*Client) WithBaseURL ¶
WithBaseURL overrides base URL to use.
func (*Client) WithClient ¶
WithClient overrides *http.Client to use.
func (*Client) WithHeader ¶
WithHeader overrides http.Header to use.
func (*Client) WithRequestLogger ¶
func (c *Client) WithRequestLogger(l RequestLogger) *Client
WithRequestLogger overrides RequestLogger of the client.
func (*Client) WithRequestResponseLogger ¶
func (c *Client) WithRequestResponseLogger(l RequestResponseLogger) *Client
WithRequestResponseLogger overrides RequestLogger and ResponseLogger with an object in same time.
func (*Client) WithResponseLogger ¶
func (c *Client) WithResponseLogger(l ResponseLogger) *Client
WithResponseLogger overrides ResponseLogger of the client.
type Error ¶
type Error struct { StatusCode int Status string ContentType string Type string Title string Detail string Instance string Properties map[string]interface{} }
Error is general structure to store error. This supports https://tools.ietf.org/html/rfc7807, if "status" field has some troubles (not string or differ from `StatusCode`, its raw value is put into `Properties`.
type RequestLogger ¶
RequestLogger is a callback with request before make round trip.
type RequestResponseLogger ¶
type RequestResponseLogger interface { RequestLogger ResponseLogger }
RequestResponseLogger provides RequestLogger and Response by one object. It is supposed to use with WithRequestResponseLogger.
type ResponseLogger ¶
ResponseLogger is a callback with response after round trip succeeded.
type SystemError ¶
SystemError is for problems on jsonhttp.
func (*SystemError) Error ¶
func (se *SystemError) Error() string