requestclient

package module
v0.0.0-...-f23a489 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2015 License: BSD-3-Clause Imports: 12 Imported by: 0

README

requestclient

Deprecated in favor of: https://github.com/linkosmos/rehttp

Build Status GoDoc BSD License

HTTP request client exposing:

  • TLS
  • Dialer
  • Transport
  • Client
How to (default options):
client := requestclient.New(nil)

u, _ := url.Parse("http://www.example.com")

responseTransport, err := client.RoundTrip(client.GET(u)) // For lower level API

responseClient, err := client.Do(client.GET(u)) // For higher level API

How to (with custom options):

options := requestclient.NewOptions()

client := requestclient.New(options)

u, _ := url.Parse("http://www.example.com")

responseTransport, err := client.RoundTrip(client.GET(u)) // For lower level API

responseClient, err := client.Do(client.GET(u)) // For higher level API

Options
////////////////////////////////
// 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

//
////////////////////////////////
// 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

//
////////////////////////////////
// Client
////////////////////////////////
//

// 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

//
////////////////////////////////
// TLS
////////////////////////////////
//

// InsecureSkipVerify controls whether a client verifies the
// server's certificate chain and host name.
TLSInsecureSkipVerify bool
Extensibility

Package uses two interfaces one for http.Client, other for http.Transport. One can easily replace default transport or client wiht custom by satisfying interfaces below.

// DialDialer - is an interface for connecting to an address.
type DialDialer interface {
	Dial(network, address string) (net.Conn, 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.
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)
}

// ClientRequester - interface for http.Client
type ClientRequester interface {
	Do(req *http.Request) (*http.Response, error)
}

// Requester - wraps RoundTripper & ClientRequester
type Requester interface {
	Do(*http.Request) (*http.Response, error)
	RoundTrip(*http.Request) (*http.Response, error)
}

Documentation

Index

Constants

View Source
const (
	GET    = "GET"
	HEAD   = "HEAD"
	POST   = "POST"
	PUT    = "PUT"
	DELETE = "DELETE"
)

Request methods

View Source
const (
	DefaultDialerTimeout                = 30 * time.Second
	DefaultDialerDualStack              = false
	DefaultDialerKeepAlive              = 30 * time.Second
	DefaultTransportMaxTries            = 3
	DefaultTransportDisableKeepAlives   = false
	DefaultTransportDisableCompression  = false
	DefaultTransportMaxIdleConnsPerHost = http.DefaultMaxIdleConnsPerHost
	DefaultClientTimeout                = 1 * time.Minute // Default 3 min
	DefaultTLSInsecureSkipVerify        = false
)

RequestClient options default values

View Source
const (
	RequestProto      = "HTTP/1.1"
	RequestProtoMinor = 0
	RequestProtoMajor = 1
)

-

View Source
const VERSION = "0.0.1"

VERSION - RequestClient version

Variables

View Source
var DefaultUserAgent = fmt.Sprintf("Go RequestClient %s", VERSION)

DefaultUserAgent - default user agent for this request client package

Functions

This section is empty.

Types

type ClientRequester

type ClientRequester interface {
	Do(req *http.Request) (*http.Response, error)
}

ClientRequester - higher level API that implements http.Client

type DialDialer

type DialDialer interface {
	Dial(network, address string) (net.Conn, error)
}

DialDialer - is an interface for connecting to an address.

type Options

type Options struct {

	////////////////////////////////
	// 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

	// 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

	// 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

func (o *Options) GetUserAgent() (ua string)

GetUserAgent - sets request user agent

func (*Options) SetUserAgent

func (o *Options) SetUserAgent(ua string)

SetUserAgent - sets request user agent

type RequestClient

type RequestClient struct {

	// A Header represents the key-value pairs in an HTTP header.
	// Used for every http.Request formed by RequestClient
	Headers http.Header // requesting headers

	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

	// A Dialer contains options for connecting to an address.
	//
	// The zero value for each field is equivalent to dialing
	// without that option. Dialing with the zero value of Dialer
	// is therefore equivalent to just calling the Dial function.
	Dialer DialDialer

	// 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 RoundTripper

	// 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
}

RequestClient - http.: Transport, Client wrapper

func New

func New(op *Options) (r *RequestClient)

New - returns Request Client

func (*RequestClient) Do

func (r *RequestClient) Do(req *http.Request) (*http.Response, error)

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 (*RequestClient) GET

func (r *RequestClient) GET(u *url.URL) *http.Request

GET - request

func (*RequestClient) HEAD

func (r *RequestClient) HEAD(u *url.URL) *http.Request

HEAD - request

func (*RequestClient) NewRequest

func (r *RequestClient) NewRequest(method string, u *url.URL, body io.Reader) (req *http.Request)

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.

func (RequestClient) RoundTrip

func (r RequestClient) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the RoundTripper interface.

For higher-level HTTP client support (such as handling of cookies and redirects), see Get, Post, and the Client type.

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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