http

package
v5.0.0-...-abaee90 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2023 License: LGPL-3.0 Imports: 14 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// The maximum number of times to try sending a request before we give up
	// (assuming any unsuccessful attempts can be sensibly tried again).
	MaxSendAttempts = 3
)

Variables

This section is empty.

Functions

func BasicHeaders

func BasicHeaders() http.Header

BasicHeaders constructs basic http.Headers with expected default values.

func DefaultHeaders

func DefaultHeaders(method string, extraHeaders http.Header, contentType, authToken string, payloadExists bool) http.Header

DefaultHeaders creates a set of http.Headers from the given arguments passed in. In this case it applies the headers passed in first, then sets the following:

  • X-Auth-Token
  • Content-Type
  • Accept
  • User-Agent

Types

type Client

type Client struct {
	http.Client
	// contains filtered or unexported fields
}

func New

func New(options ...Option) *Client

New returns a new goose http *Client using the default net/http client.

func (*Client) BinaryRequest

func (c *Client) BinaryRequest(method, url, token string, reqData *RequestData, logger logging.CompatLogger) (err error)

BinaryRequest sends the byte array in reqData.ReqValue (if any) to the specified URL. Optional method arguments are passed using the RequestData object. Relevant RequestData fields: ReqHeaders: additional HTTP header values to add to the request. ExpectedStatus: the allowed HTTP response status values, else an error is returned. ReqReader: an io.Reader providing the bytes to send. RespReader: if non-nil, is assigned an io.ReadCloser instance used to read the returned data.

func (*Client) JsonRequest

func (c *Client) JsonRequest(method, url, token string, reqData *RequestData, logger logging.CompatLogger) error

JsonRequest JSON encodes and sends the object in reqData.ReqValue (if any) to the specified URL. Optional method arguments are passed using the RequestData object. Relevant RequestData fields: ReqHeaders: additional HTTP header values to add to the request. ExpectedStatus: the allowed HTTP response status values, else an error is returned. ReqValue: the data object to send. RespValue: the data object to decode the result into.

type ErrorResponse

type ErrorResponse struct {
	Message string `json:"message"`
	Code    int    `json:"code"`
	Title   string `json:"title"`
}

func (*ErrorResponse) Error

func (e *ErrorResponse) Error() string

type HeadersFunc

type HeadersFunc = func(method string, headers http.Header, contentType, authToken string, hasPayload bool) http.Header

HeadersFunc is type for aligning the creation of a series of client headers.

type HttpClient

type HttpClient interface {
	BinaryRequest(method, url, token string, reqData *RequestData, logger logging.CompatLogger) (err error)
	Do(req *http.Request) (*http.Response, error)
	Get(url string) (resp *http.Response, err error)
	Head(url string) (resp *http.Response, err error)
	JsonRequest(method, url, token string, reqData *RequestData, logger logging.CompatLogger) error
	Post(url, contentType string, body io.Reader) (resp *http.Response, err error)
	PostForm(url string, data url.Values) (resp *http.Response, err error)
}

type HttpError

type HttpError struct {
	StatusCode int
	Data       map[string][]string
	// contains filtered or unexported fields
}

func (*HttpError) Error

func (e *HttpError) Error() string

type Option

type Option func(*options)

Option allows the adaptation of a http client given new options. Both client.Client and http.Client have Options. To allow isolation between layers, we have separate options. If client.Client and http.Client want different options they can do so, without causing conflict.

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

WithHTTPClient allows the setting of the http.Client to use for all the http requests.

func WithHeadersFunc

func WithHeadersFunc(headersFunc HeadersFunc) Option

WithHeadersFunc allows passing in a new headers func for the http.Client to execute for each request.

type RequestData

type RequestData struct {
	ReqHeaders     http.Header
	Params         *url.Values
	ExpectedStatus []int
	ReqValue       interface{}
	// ReqReader is used to read the body of the request. If the
	// request has to be retried for any reason then a replacement
	// ReqReader will be collected using GetReqReader.
	ReqReader io.Reader

	// GetReqReader is called to get a new ReqReader if a request
	// fails and will be retried. If ReqReader is specified but
	// GetReqReader is not then an appropriate GetReqReader function
	// will be generated from ReqReader.
	//
	// If ReqReader implements io.Seeker then the generated
	// GetReqReader function will use Seek to rewind the request.
	// Otherwise the entire body may be kept in memory whilst sending
	// the request.
	GetReqReader func() (io.ReadCloser, error)

	// TODO this should really be int64 not int.
	ReqLength int

	RespStatusCode int
	RespValue      interface{}
	RespLength     int64
	RespReader     io.ReadCloser
	RespHeaders    http.Header
}

Jump to

Keyboard shortcuts

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