httpx

package
v3.15.0-alpha Latest Latest
Warning

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

Go to latest
Published: May 20, 2022 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package httpx contains http extensions.

Index

Constants

View Source
const DefaultMaxBodySize = 1 << 22

DefaultMaxBodySize is the default value for the maximum body size you can fetch using an APIClient.

Variables

View Source
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.

Jump to

Keyboard shortcuts

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