Documentation
¶
Index ¶
- Constants
- type Client
- func (r *Client) Do(req *http.Request) (*http.Response, error)
- func (r *Client) GET(u *url.URL) *http.Request
- func (r *Client) HEAD(u *url.URL) *http.Request
- func (r *Client) NewRequest(method string, u *url.URL, body io.Reader) (req *http.Request)
- func (r Client) RoundTrip(req *http.Request) (*http.Response, error)
- type ClientRequester
- type Options
- type Requester
- type RoundTripper
Constants ¶
const ( RequestProto = "HTTP/1.1" RequestProtoMinor = 0 RequestProtoMajor = 1 )
-
const ( GET = "GET" HEAD = "HEAD" POST = "POST" PUT = "PUT" PATCH = "PATCH" DELETE = "DELETE" )
Request methods
const ( DefaultDialerTimeout = 30 * time.Second DefaultDialerDualStack = false DefaultDialerKeepAlive = 30 * time.Second DefaultTransportMaxTries = 3 DefaultTransportDisableKeepAlives = false DefaultTransportDisableCompression = false DefaultTransportMaxIdleConnsPerHost = http.DefaultMaxIdleConnsPerHost DefaultTransportRetryAfterTimeout = true DefaultClientTimeout = 1 * time.Minute // Default 3 min DefaultTLSInsecureSkipVerify = false DefaultConnPoolInitial = 2 DefaultConnPoolMax = 5 )
RequestClient options default values
const DefaultUserAgent = "Golang rehttp.Client"
DefaultUserAgent - default user agent for this request client package
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
RequestProto string
RequestProtoMinor, RequestProtoMajor int
// A Config structure is used to configure a TLS client or server.
// After one has been passed to a TLS function it must not be
// modified. A Config may be reused; the tls package will also not
// modify it.
TLS *tls.Config
// Transport is an implementation of RoundTripper that supports HTTP,
// HTTPS, and HTTP proxies (for either HTTP or HTTPS with CONNECT).
// Transport can also cache connections for future re-use.
Transport *httpcontrol.Transport
// A Client is an HTTP client. Its zero value (DefaultClient) is a
// usable client that uses DefaultTransport.
//
// The Client's Transport typically has internal state (cached TCP
// connections), so Clients should be reused instead of created as
// needed. Clients are safe for concurrent use by multiple goroutines.
//
// A Client is higher-level than a RoundTripper (such as Transport)
// and additionally handles HTTP details such as cookies and
// redirects.
Client ClientRequester
// rehttp.Client options
ClientOptions *Options
}
Client - http.: Transport, Client wrapper
func (*Client) Do ¶
Do - sends an HTTP request and returns an HTTP response, following policy (e.g. redirects, cookies, auth) as configured on the client.
An error is returned if caused by client policy (such as CheckRedirect), or if there was an HTTP protocol error. A non-2xx response doesn't cause an error.
When err is nil, resp always contains a non-nil resp.Body.
Callers should close resp.Body when done reading from it. If resp.Body is not closed, the Client's underlying RoundTripper (typically Transport) may not be able to re-use a persistent TCP connection to the server for a subsequent "keep-alive" request.
The request Body, if non-nil, will be closed by the underlying Transport, even on errors.
Generally Get, Post, or PostForm will be used instead of Do.
func (*Client) NewRequest ¶
NewRequest returns a new Request given a method, URL, and optional body.
If the provided body is also an io.Closer, the returned Request.Body is set to body and will be closed by the Client methods Do, Post, and PostForm, and Transport.RoundTrip.
type ClientRequester ¶
ClientRequester - higher level API that implements http.Client
type Options ¶
type Options struct {
////////////////////////////////
// Connection Pool
////////////////////////////////
// Connection pool based on buffered channels with an initial
// capacity and maximum capacity. Factory is used when initial capacity is
// greater than zero to fill the pool. A zero initialCap doesn't fill the Pool
// until a new Get() is called. During a Get(), If there is no new connection
// available in the pool, a new connection will be created via the Factory()
// method.
ConnPoolInitial int
ConnPoolMax int
// KeepAlive specifies the keep-alive period for an active
// network connection.
// If zero, keep-alives are not enabled. Network protocols
// that do not support keep-alives ignore this field.
KeepAlive time.Duration
////////////////////////////////
// Dialer
////////////////////////////////
// Timeout is the maximum amount of time a dial will wait for
// a connect to complete.
//
// With or without a timeout, the operating system may impose
// its own earlier timeout. For instance, TCP timeouts are
// often around 3 minutes.
DialerTimeout time.Duration // The default is no timeout.
// Deadline is the absolute point in time after which dials
// will fail. If Timeout is set, it may fail earlier.
// Zero means no deadline, or dependent on the operating system
// as with the Timeout option.
DialerDeadline time.Time
// DualStack allows a single dial to attempt to establish
// multiple IPv4 and IPv6 connections and to return the first
// established connection when the network is "tcp" and the
// destination is a host name that has multiple address family
// DNS records.
DialerDualStack bool
// KeepAlive specifies the keep-alive period for an active
// network connection.
// If zero, keep-alives are not enabled. Network protocols
// that do not support keep-alives ignore this field.
DialerKeepAlive time.Duration
// FallbackDelay specifies the length of time to wait before
// spawning a fallback connection, when DualStack is enabled.
// If zero, a default delay of 300ms is used.
DialerFallbackDelay time.Duration
//
////////////////////////////////
// Transport
////////////////////////////////
//
// MaxTries, if non-zero, specifies the number of times we will retry on
// failure. Retries are only attempted for temporary network errors or known
// safe failures.
TransportMaxTries uint
// DisableKeepAlives, if true, prevents re-use of TCP connections
// between different HTTP requests.
TransportDisableKeepAlives bool
// DisableCompression, if true, prevents the Transport from
// requesting compression with an "Accept-Encoding: gzip"
TransportDisableCompression bool
// MaxIdleConnsPerHost, if non-zero, controls the maximum idle
// (keep-alive) to keep per-host.
TransportMaxIdleConnsPerHost int
// RequestTimeout, if non-zero, specifies the amount of time for the entire
// request. This includes dialing (if necessary), the response header as well
// as the entire body.
TransportRequestTimeout time.Duration
// ResponseHeaderTimeout, if non-zero, specifies the amount of
// time to wait for a server's response headers after fully
// writing the request (including its body, if any). This
// time does not include the time to read the response body.
TransportResponseHeaderTimeout time.Duration
// RetryAfterTimeout, if true, will enable retries for a number of failures
// that are probably safe to retry for most cases but, depending on the
// context, might not be safe. Retried errors: net.Errors where Timeout()
// returns `true` or timeouts that bubble up as url.Error but were originally
// net.Error, OpErrors where the request was cancelled (either by this lib or
// by the calling code, or finally errors from requests that were cancelled
// before the remote side was contacted.
TransportRetryAfterTimeout bool
// Timeout specifies a time limit for requests made by this
// Client. The timeout includes connection time, any
// redirects, and reading the response body. The timer remains
// running after Get, Head, Post, or Do return and will
// interrupt reading of the Response.Body.
//
// A Timeout of zero means no timeout.
//
// The Client's Transport must support the CancelRequest
// method or Client will return errors when attempting to make
// a request with Get, Head, Post, or Do. Client's default
// Transport (DefaultTransport) supports CancelRequest.
ClientTimeout time.Duration
// InsecureSkipVerify controls whether a client verifies the
// server's certificate chain and host name.
TLSInsecureSkipVerify bool
// Request headers
Headers http.Header
}
Options - keep options for continuous requests
func NewOptions ¶
func NewOptions() (op *Options)
NewOptions - options struct initialized with default values
func (*Options) GetUserAgent ¶
GetUserAgent - sets request user agent
func (*Options) SetHeaderKeepAlive ¶
func (o *Options) SetHeaderKeepAlive()
SetHeaderKeepAlive - sets keep-alive instruction in header
func (*Options) SetHeaderUserAgent ¶
SetHeaderUserAgent - sets request user agent
type Requester ¶
type Requester interface {
Do(*http.Request) (*http.Response, error)
RoundTrip(*http.Request) (*http.Response, error)
}
Requester - wraps RoundTripper & ClientRequester
type RoundTripper ¶
type RoundTripper interface {
// RoundTrip executes a single HTTP transaction, returning
// the Response for the request req. RoundTrip should not
// attempt to interpret the response. In particular,
// RoundTrip must return err == nil if it obtained a response,
// regardless of the response's HTTP status code. A non-nil
// err should be reserved for failure to obtain a response.
// Similarly, RoundTrip should not attempt to handle
// higher-level protocol details such as redirects,
// authentication, or cookies.
//
// RoundTrip should not modify the request, except for
// consuming and closing the Body, including on errors. The
// request's URL and Header fields are guaranteed to be
// initialized.
RoundTrip(*http.Request) (*http.Response, error)
}
RoundTripper is an interface representing the ability to execute a single HTTP transaction, obtaining the Response for a given Request.
A RoundTripper must be safe for concurrent use by multiple goroutines.