client

package
v12.2.0-alpha5 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2021 License: BSD-3-Clause Imports: 14 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindResponse

func BindResponse(resp *http.Response, dest interface{}) (err error)

BindResponse consumes the response's body and binds the result to the "dest" pointer, closing the response's body is up to the caller.

The "dest" will be binded based on the response's content type header. Note that this is strict in order to catch bad actioners fast, e.g. it wont try to read plain text if not specified on the response headers and the dest is a *string.

func DecodeError

func DecodeError(err error, destPtr interface{}) error

DecodeError binds a json error to the "destPtr".

func GetErrorCode

func GetErrorCode(err error) int

GetErrorCode reads an error, which should be a type of APIError, and returns its status code. If the given "err" is nil or is not an APIError it returns 200, acting as we have no error.

Types

type APIError

type APIError struct {
	Response *http.Response
	Body     json.RawMessage // may be any []byte, response body is closed at this point.
}

APIError errors that may return from the Client.

func ExtractError

func ExtractError(resp *http.Response) APIError

ExtractError returns the response wrapped inside an APIError.

func GetError

func GetError(err error) (APIError, bool)

GetError reports whether the given "err" is an APIError.

func (APIError) Error

func (e APIError) Error() string

Error implements the standard error type.

type Client

type Client struct {
	HTTPClient *http.Client

	// BaseURL prepends to all requests.
	BaseURL string

	// A list of persistent request options.
	PersistentRequestOptions []RequestOption
}

the base client

func New

func New(opts ...Option) *Client

func (*Client) Do

func (c *Client) Do(ctx context.Context, method, urlpath string, payload interface{}, opts ...RequestOption) (*http.Response, error)

Do sends an HTTP request and returns an HTTP response.

The payload can be: - io.Reader - raw []byte - JSON raw message - string - struct (JSON).

If method is empty then it defaults to "GET". The final variadic, optional input argument sets the custom request options to use before the request.

Any HTTP returned error will be of type APIError or a timeout error if the given context was canceled.

func (*Client) Form

func (c *Client) Form(ctx context.Context, method, urlpath string, formValues url.Values, opts ...RequestOption) (*http.Response, error)

func (*Client) GetPlainUnquote

func (c *Client) GetPlainUnquote(ctx context.Context, method, urlpath string, payload interface{}, opts ...RequestOption) (string, error)

GetPlainUnquote reads the response body as raw text and tries to unquote it, useful when the remote server sends a single key as a value but due to backend mistake it sends it as JSON (quoted) instead of plain text.

func (*Client) JSON

func (c *Client) JSON(ctx context.Context, method, urlpath string, payload interface{}, opts ...RequestOption) (*http.Response, error)

func (*Client) NewUploader

func (c *Client) NewUploader() *Uploader

func (*Client) ReadJSON

func (c *Client) ReadJSON(ctx context.Context, dest interface{}, method, urlpath string, payload interface{}, opts ...RequestOption) error

func (*Client) ReadPlain

func (c *Client) ReadPlain(ctx context.Context, dest interface{}, method, urlpath string, payload interface{}, opts ...RequestOption) error

ReadPlain like ReadJSON but it accepts a pointer to a string or byte slice or integer and it reads the body as plain text.

func (*Client) WriteTo

func (c *Client) WriteTo(ctx context.Context, dest io.Writer, method, urlpath string, payload interface{}, opts ...RequestOption) (int64, error)

WriteTo reads the response and then copies its data to the "dest" writer. If the "dest" is a type of HTTP response writer then it writes the content-type and content-length of the original request.

Returns the amount of bytes written to "dest".

type Option

type Option func(*Client)

func BaseURL

func BaseURL(uri string) Option

BaseURL registers the base URL of this client. All of its methods will prepend this url.

func PersistentRequestOptions

func PersistentRequestOptions(reqOpts ...RequestOption) Option

PersistentRequestOptions adds one or more persistent request options that all requests made by this Client will respect.

func Timeout

func Timeout(d time.Duration) Option

Timeout specifies a time limit for requests made by this Client. The timeout includes connection time, any redirects, and reading the response body. A Timeout of zero means no timeout.

Defaults to 15 seconds.

type RequestOption

type RequestOption func(*http.Request) error

func RequestAuthorization

func RequestAuthorization(value string) RequestOption

RequestAuthorization sets an Authorization request header. Note that we could do the same with a Transport RoundDrip too.

func RequestAuthorizationBearer

func RequestAuthorizationBearer(accessToken string) RequestOption

RequestAuthorizationBearer sets an Authorization: Bearer $token request header.

func RequestHeader

func RequestHeader(overridePrev bool, key string, values ...string) RequestOption

func RequestParam

func RequestParam(key string, values ...string) RequestOption

RequestParam sets a single URL query parameter to the request.

func RequestQuery

func RequestQuery(query url.Values) RequestOption

RequestQuery adds a set of URL query parameters to the request.

type Uploader

type Uploader struct {
	Writer *multipart.Writer
	// contains filtered or unexported fields
}

func (*Uploader) AddField

func (u *Uploader) AddField(key, value string) error

func (*Uploader) AddFile

func (u *Uploader) AddFile(key, filename string) error

func (*Uploader) AddFileSource

func (u *Uploader) AddFileSource(key, filename string, source io.Reader) error

func (*Uploader) Upload

func (u *Uploader) Upload(ctx context.Context, method, urlpath string, opts ...RequestOption) (*http.Response, error)

Jump to

Keyboard shortcuts

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