httpc

package
v2.7.10 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: MIT Imports: 17 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTransportInsecure http.RoundTripper = &http.Transport{
	Proxy: http.ProxyFromEnvironment,
	DialContext: (&net.Dialer{
		Timeout:   30 * time.Second,
		KeepAlive: 30 * time.Second,
		DualStack: true,
	}).DialContext,
	ForceAttemptHTTP2:     true,
	MaxIdleConns:          100,
	IdleConnTimeout:       90 * time.Second,
	TLSHandshakeTimeout:   10 * time.Second,
	ExpectContinueTimeout: 1 * time.Second,
	TLSClientConfig: &tls.Config{
		InsecureSkipVerify: true,
	},
}

DefaultTransportInsecure is identical to http.DefaultTransport, with the exception that tls.Config is configured with InsecureSkipVerify set to true.

Functions

func BodyEmpty

func BodyEmpty(io.Writer) (string, string, error)

BodyEmpty returns an empty body.

func StatusIn

func StatusIn(code int, rest ...int) func(*http.Response) error

StatusIn validates the status code matches one of the provided statuses.

Types

type BodyFn

type BodyFn func(w io.Writer) (header string, headerVal string, err error)

BodyFn provides a writer to which a value will be written to that will make it's way into the HTTP request.

func BodyGob

func BodyGob(v interface{}) BodyFn

BodyGob gob encodes the value provided for the HTTP request. Sets the Content-Encoding to application/gob.

func BodyJSON

func BodyJSON(v interface{}) BodyFn

BodyJSON JSON encodes the value provided for the HTTP request. Sets the Content-Type to application/json.

type Client

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

Client is a basic http client that can make cReqs with out having to juggle the token and so forth. It provides sane defaults for checking response statuses, sets auth token when provided, and sets the content type to application/json for each request. The token, response checker, and content type can be overridden on the Req as well.

func New

func New(opts ...ClientOptFn) (*Client, error)

New creates a new httpc client.

func (*Client) Clone

func (c *Client) Clone(opts ...ClientOptFn) (*Client, error)

Clone creates a new *Client type from an existing client. This may be useful if you want to have a shared base client, then take a specific client from that base and tack on some extra goodies like specific headers and whatever else that suits you. Note: a new net.http.Client type will not be created. It will share the existing http.Client from the parent httpc.Client. Same connection pool, different specifics.

func (*Client) Delete

func (c *Client) Delete(urlPath ...string) *Req

Delete generates a DELETE request.

func (*Client) Get

func (c *Client) Get(urlPath ...string) *Req

Get generates a GET request.

func (*Client) Patch

func (c *Client) Patch(bFn BodyFn, urlPath ...string) *Req

Patch generates a PATCH request.

func (*Client) PatchJSON

func (c *Client) PatchJSON(v interface{}, urlPath ...string) *Req

PatchJSON generates a PATCH request. This is to be used with value or pointer to value type. Providing a stream/reader will result in disappointment.

func (*Client) Post

func (c *Client) Post(bFn BodyFn, urlPath ...string) *Req

Post generates a POST request.

func (*Client) PostJSON

func (c *Client) PostJSON(v interface{}, urlPath ...string) *Req

PostJSON generates a POST request and json encodes the body. This is to be used with value or pointer to value type. Providing a stream/reader will result in disappointment.

func (*Client) Put

func (c *Client) Put(bFn BodyFn, urlPath ...string) *Req

Put generates a PUT request.

func (*Client) PutJSON

func (c *Client) PutJSON(v interface{}, urlPath ...string) *Req

PutJSON generates a PUT request. This is to be used with value or pointer to value type. Providing a stream/reader will result in disappointment.

func (*Client) Req

func (c *Client) Req(method string, bFn BodyFn, urlPath ...string) *Req

Req constructs a request.

type ClientOptFn

type ClientOptFn func(*clientOpt) error

ClientOptFn are options to set different parameters on the Client.

func WithAddr

func WithAddr(addr string) ClientOptFn

WithAddr sets the host address on the client.

func WithAuth

func WithAuth(fn func(r *http.Request) error) ClientOptFn

WithAuth provides a means to set a custom auth that doesn't match the provided auth types here.

func WithAuthToken

func WithAuthToken(token string) ClientOptFn

WithAuthToken provides token auth for requests.

func WithContentType

func WithContentType(ct string) ClientOptFn

WithContentType sets the content type that will be applied to the requests created by the Client.

func WithHTTPClient

func WithHTTPClient(c *http.Client) ClientOptFn

WithHTTPClient sets the raw http client on the httpc Client.

func WithHeader

func WithHeader(header, val string) ClientOptFn

WithHeader sets a default header that will be applied to all requests created by the client.

func WithInsecureSkipVerify

func WithInsecureSkipVerify(b bool) ClientOptFn

WithInsecureSkipVerify sets the insecure skip verify on the http client's htp transport.

func WithRespFn

func WithRespFn(fn func(*http.Response) error) ClientOptFn

WithRespFn sets the default resp fn for the client that will be applied to all requests generated from it.

func WithSessionCookie

func WithSessionCookie(session string) ClientOptFn

WithSessionCookie provides cookie auth for requests to mimic the browser. Typically, session is influxdb.Session.Key.

func WithStatusFn

func WithStatusFn(fn func(*http.Response) error) ClientOptFn

WithStatusFn sets the default status fn for the client that will be applied to all requests generated from it.

func WithUserAgentHeader

func WithUserAgentHeader(userAgent string) ClientOptFn

WithUserAgentHeader sets the user agent for the http client requests.

func WithWriterFn

func WithWriterFn(fn WriteCloserFn) ClientOptFn

WithWriterFn applies the provided writer behavior to all the request bodies' generated from the client.

func WithWriterGZIP

func WithWriterGZIP() ClientOptFn

WithWriterGZIP gzips the request body generated from this client.

type Req

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

Req is a request type.

func (*Req) Accept

func (r *Req) Accept(contentType string) *Req

Accept sets the Accept header to the provided content type on the request.

func (*Req) Auth

func (r *Req) Auth(authFn func(r *http.Request) error) *Req

Auth sets the authorization for a request.

func (*Req) ContentType

func (r *Req) ContentType(contentType string) *Req

ContentType sets the Content-Type header to the provided content type on the request.

func (*Req) Decode

func (r *Req) Decode(fn func(resp *http.Response) error) *Req

Decode sets the decoding functionality for the request. All Decode calls are called after the status and response functions are called. Decoding will not happen if error encountered in the status check.

func (*Req) DecodeGob

func (r *Req) DecodeGob(v interface{}) *Req

DecodeGob sets the decoding functionality to decode gob for the request.

func (*Req) DecodeJSON

func (r *Req) DecodeJSON(v interface{}) *Req

DecodeJSON sets the decoding functionality to decode json for the request.

func (*Req) Do

func (r *Req) Do(ctx context.Context) error

Do makes the HTTP request. Any errors that had been encountered in the lifetime of the Req type will be returned here first, in place of the call. This makes it safe to call Do at anytime.

func (*Req) Header

func (r *Req) Header(k, v string) *Req

Header adds the header to the http request.

func (*Req) Headers

func (r *Req) Headers(m map[string][]string) *Req

Headers adds all the headers to the http request.

func (*Req) QueryParams

func (r *Req) QueryParams(pairs ...[2]string) *Req

QueryParams adds the query params to the http request.

func (*Req) RespFn

func (r *Req) RespFn(fn func(*http.Response) error) *Req

RespFn provides a means to inspect the entire http response. This function runs first before the status and decode functions are called.

func (*Req) StatusFn

func (r *Req) StatusFn(fn func(*http.Response) error) *Req

StatusFn sets a status check function. This runs after the resp func but before the decode fn.

type WriteCloserFn

type WriteCloserFn func(closer io.WriteCloser) (string, string, io.WriteCloser)

WriteCloserFn is a write closer wrapper than indicates the type of writer by returning the header and header value associated with the writer closer.

i.e. GZIP writer returns header Content-Encoding with value gzip alongside
the writer.

Jump to

Keyboard shortcuts

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