httputil

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2020 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package httputil provides abstractions around the common needs of HTTP. It also allows swapping in and out the HTTP client.

Index

Constants

This section is empty.

Variables

View Source
var DefaultClient = NewClient()
View Source
var Retries uint = 5

Retries is the default attempts to retry if the API returns an error before giving up.

Functions

func JSONRequest

func JSONRequest(r *http.Request) error

func MultipartRequest

func MultipartRequest(r *http.Request) error

Types

type Client

type Client struct {
	http.Client
	json.Driver
	SchemaEncoder

	Retries uint
}

func NewClient

func NewClient() Client

func (*Client) FastRequest

func (c *Client) FastRequest(
	method, url string, opts ...RequestOption) error

func (*Client) MeanwhileMultipart

func (c *Client) MeanwhileMultipart(
	multipartWriter func(*multipart.Writer) error,
	method, url string, opts ...RequestOption) (*http.Response, error)

func (*Client) Request

func (c *Client) Request(
	method, url string, opts ...RequestOption) (*http.Response, error)

func (*Client) RequestCtx

func (c *Client) RequestCtx(ctx context.Context,
	method, url string, opts ...RequestOption) (*http.Response, error)

func (*Client) RequestCtxJSON

func (c *Client) RequestCtxJSON(ctx context.Context,
	to interface{}, method, url string, opts ...RequestOption) error

func (*Client) RequestJSON

func (c *Client) RequestJSON(
	to interface{}, method, url string, opts ...RequestOption) error

type DefaultSchema

type DefaultSchema struct {
	*schema.Encoder
	// contains filtered or unexported fields
}

func (*DefaultSchema) Encode

func (d *DefaultSchema) Encode(src interface{}) (url.Values, error)

type ErrorCode

type ErrorCode uint

type HTTPError

type HTTPError struct {
	Status int    `json:"-"`
	Body   []byte `json:"-"`

	Code    ErrorCode `json:"code"`
	Message string    `json:"message,omitempty"`
}

func (HTTPError) Error

func (err HTTPError) Error() string

type JSONError

type JSONError struct {
	// contains filtered or unexported fields
}

type RequestError

type RequestError struct {
	// contains filtered or unexported fields
}

type RequestOption

type RequestOption func(*http.Request) error

func WithBody

func WithBody(body io.ReadCloser) RequestOption

func WithContentType

func WithContentType(ctype string) RequestOption

func WithJSONBody

func WithJSONBody(json json.Driver, v interface{}) RequestOption

func WithSchema

func WithSchema(schema SchemaEncoder, v interface{}) RequestOption

type SchemaEncoder

type SchemaEncoder interface {
	Encode(src interface{}) (url.Values, error)
}

SchemaEncoder expects the encoder to read the "schema" tags.

type TransportWrapper

type TransportWrapper struct {
	Default http.RoundTripper
	Pre     func(*http.Request) error
	Cancel  func(*http.Request, error)
	Post    func(*http.Response) error
}

func NewTransportWrapper

func NewTransportWrapper() *TransportWrapper

func (*TransportWrapper) RoundTrip

func (c *TransportWrapper) RoundTrip(req *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

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