httputil

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AcceptHeader is the canonical header name for accept.
	AcceptHeader = "Accept"
	// AcceptEncodingHeader is the canonical header name for accept encoding.
	AcceptEncodingHeader = "Accept-Encoding"
	// ContentEncodingHeader is the canonical header name for content type.
	ContentEncodingHeader = "Content-Encoding"
	// ContentTypeHeader is the canonical header name for content type.
	ContentTypeHeader = "Content-Type"
	// JSONContentType is the JSON content type.
	JSONContentType = "application/json"
	// AltJSONContentType is the alternate JSON content type.
	AltJSONContentType = "application/x-json"
	// ProtoContentType is the protobuf content type.
	ProtoContentType = "application/x-protobuf"
	// AltProtoContentType is the alternate protobuf content type.
	AltProtoContentType = "application/x-google-protobuf"
	// PlaintextContentType is the plaintext content type.
	PlaintextContentType = "text/plain"
	// GzipEncoding is the gzip encoding.
	GzipEncoding = "gzip"
)
View Source
const StandardHTTPTimeout time.Duration = 3 * time.Second

StandardHTTPTimeout is the default timeout to use for HTTP connections.

Variables

DefaultClient is a replacement for http.DefaultClient which defines a standard timeout.

Functions

func Get

func Get(ctx context.Context, url string) (resp *http.Response, err error)

Get does like http.Get but uses the provided context and obeys its cancellation. It also uses the default client with a default 3 second timeout.

func GetJSON

func GetJSON(httpClient http.Client, path string, response protoutil.Message) error

GetJSON uses the supplied client to GET the URL specified by the parameters and unmarshals the result into response. TODO(someone): make this context-aware, see client.go.

func Head(ctx context.Context, url string) (resp *http.Response, err error)

Head does like http.Head but uses the provided context and obeys its cancellation. It also uses the default client with a default 3 second timeout.

func Post

func Post(
	ctx context.Context, url, contentType string, body io.Reader,
) (resp *http.Response, err error)

Post does like http.Post but uses the provided context and obeys its cancellation. It also uses the default client with a default 3 second timeout.

func PostJSON

func PostJSON(httpClient http.Client, path string, request, response protoutil.Message) error

PostJSON uses the supplied client to POST request to the URL specified by the parameters and unmarshals the result into response. TODO(someone): make this context-aware, see client.go.

func PostJSONWithRequest

func PostJSONWithRequest(
	httpClient http.Client, path string, request, response protoutil.Message,
) (*http.Response, error)

PostJSONWithRequest uses the supplied client to POST request to the URL specified by the parameters and unmarshals the result into response.

The response is returned to the caller, though its body will have been closed. TODO(someone): make this context-aware, see client.go.

Types

type Client

type Client struct {
	*http.Client
}

Client is a replacement for http.Client which implements method variants that respect a provided context's cancellation status.

func NewClientWithTimeout

func NewClientWithTimeout(timeout time.Duration) *Client

NewClientWithTimeout defines a http.Client with the given timeout.

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string) (resp *http.Response, err error)

Get does like http.Client.Get but uses the provided context and obeys its cancellation.

func (*Client) Head

func (c *Client) Head(ctx context.Context, url string) (resp *http.Response, err error)

Head does like http.Client.Head but uses the provided context and obeys its cancellation.

func (*Client) Post

func (c *Client) Post(
	ctx context.Context, url, contentType string, body io.Reader,
) (resp *http.Response, err error)

Post does like http.Client.Post but uses the provided context and obeys its cancellation.

Jump to

Keyboard shortcuts

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