Documentation
¶
Overview ¶
Package httpx contains http extensions.
Index ¶
Constants ¶
const DefaultMaxBodySize = 1 << 22
DefaultMaxBodySize is the default value for the maximum body size you can fetch using an APIClient.
Variables ¶
var ErrRequestFailed = errors.New("httpx: request failed")
ErrRequestFailed indicates that the server returned >= 400.
Functions ¶
This section is empty.
Types ¶
type APIClient ¶
type APIClient interface {
// GetJSON reads the JSON resource whose path is obtained concatenating
// the baseURL's path with `resourcePath` and unmarshals the results
// into `output`. The request is bounded by the lifetime of the
// context passed as argument. Returns the error that occurred.
GetJSON(ctx context.Context, resourcePath string, output interface{}) error
// GetJSONWithQuery is like GetJSON but also has a query.
GetJSONWithQuery(ctx context.Context, resourcePath string,
query url.Values, output interface{}) error
// PostJSON creates a JSON subresource of the resource whose
// path is obtained concatenating the baseURL'spath with `resourcePath` using
// the JSON document at `input` as value and returning the result into the
// JSON document at output. The request is bounded by the context's
// lifetime. Returns the error that occurred.
PostJSON(ctx context.Context, resourcePath string, input, output interface{}) error
// FetchResource fetches the specified resource and returns it.
FetchResource(ctx context.Context, URLPath string) ([]byte, error)
}
APIClient is a client configured to call a given API identified by a given baseURL and using a given model.HTTPClient.
The resource path argument passed to APIClient methods is appended to the base URL's path for determining the full URL's path.
type APIClientTemplate ¶
type APIClientTemplate struct {
// Accept contains the OPTIONAL accept header.
Accept string
// Authorization contains the OPTIONAL authorization header.
Authorization string
// BaseURL is the MANDATORY base URL of the API.
BaseURL string
// HTTPClient is the MANDATORY underlying http client to use.
HTTPClient model.HTTPClient
// Host allows to OPTIONALLY set a specific host header. This is useful
// to implement, e.g., cloudfronting.
Host string
// LogBody is the OPTIONAL flag to force logging the bodies.
LogBody bool
// Logger is MANDATORY the logger to use.
Logger model.DebugLogger
// UserAgent is the OPTIONAL user agent to use.
UserAgent string
}
APIClientTemplate is a template for constructing an APIClient.
func (*APIClientTemplate) Build ¶
func (tmpl *APIClientTemplate) Build() APIClient
Build creates an APIClient from the APIClientTemplate.
func (*APIClientTemplate) BuildWithAuthorization ¶
func (tmpl *APIClientTemplate) BuildWithAuthorization(authorization string) APIClient
BuildWithAuthorization creates an APIClient from the APIClientTemplate and ensures it uses the given authorization value for APIClient.Authorization in subsequent API calls.
func (*APIClientTemplate) WithBodyLogging ¶
func (tmpl *APIClientTemplate) WithBodyLogging() *APIClientTemplate
WithBodyLogging enables logging of request and response bodies.