Documentation ¶
Index ¶
- Constants
- Variables
- func EnsureResponseFinalized(httpResp *Response)
- func NewDefaultTransport(dialer *net.Dialer) *http.Transport
- type Client
- func (c *Client) AddCookie(cookie *http.Cookie) *Client
- func (c *Client) AddCookies(data []*http.Cookie) *Client
- func (c *Client) AddHeader(name, value string) *Client
- func (c *Client) BaseURL(uri string) *Client
- func (c *Client) CookieJar() *Client
- func (c *Client) Delete() *Request
- func (c *Client) Get() *Request
- func (c *Client) Head() *Request
- func (c *Client) Method(name string) *Client
- func (c *Client) Param(name, value string) *Client
- func (c *Client) Params(params map[string]string) *Client
- func (c *Client) Patch() *Request
- func (c *Client) Path(path string) *Client
- func (c *Client) Post() *Request
- func (c *Client) Put() *Request
- func (c *Client) Request() *Request
- func (c *Client) SetHeader(key, value string) *Client
- func (c *Client) SetHeaders(fields map[string]string) *Client
- func (c *Client) URL(uri string) *Client
- func (c *Client) Use(p plugin.Plugin) *Client
- func (c *Client) UseError(fn context.HandlerFunc) *Client
- func (c *Client) UseHandler(phase string, fn context.HandlerFunc) *Client
- func (c *Client) UseParent(parent *Client) *Client
- func (c *Client) UseRequest(fn context.HandlerFunc) *Client
- func (c *Client) UseResponse(fn context.HandlerFunc) *Client
- type Dispatcher
- type Request
- func (r *Request) AddCookie(cookie *http.Cookie) *Request
- func (r *Request) AddCookies(data []*http.Cookie) *Request
- func (r *Request) AddHeader(name, value string) *Request
- func (r *Request) AddPath(path string) *Request
- func (r *Request) AddQuery(name, value string) *Request
- func (r *Request) BaseURL(uri string) *Request
- func (r *Request) Body(reader io.Reader) *Request
- func (r *Request) BodyString(data string) *Request
- func (r *Request) Clone() *Request
- func (r *Request) CookieJar() *Request
- func (r *Request) Do() (*Response, error)
- func (r *Request) File(name string, reader io.Reader) *Request
- func (r *Request) Files(files []multipart.FormFile) *Request
- func (r *Request) Form(data multipart.FormData) *Request
- func (r *Request) JSON(data interface{}) *Request
- func (r *Request) Method(method string) *Request
- func (r *Request) Mux() *mux.Mux
- func (r *Request) Param(name, value string) *Request
- func (r *Request) Params(params map[string]string) *Request
- func (r *Request) Path(path string) *Request
- func (r *Request) Send() (*Response, error)
- func (r *Request) SetClient(cli *Client) *Request
- func (r *Request) SetHeader(name, value string) *Request
- func (r *Request) SetHeaders(fields map[string]string) *Request
- func (r *Request) SetQuery(name, value string) *Request
- func (r *Request) SetQueryParams(params map[string]string) *Request
- func (r *Request) Type(name string) *Request
- func (r *Request) URL(uri string) *Request
- func (r *Request) Use(p plugin.Plugin) *Request
- func (r *Request) UseError(fn context.HandlerFunc) *Request
- func (r *Request) UseHandler(phase string, fn context.HandlerFunc) *Request
- func (r *Request) UseRequest(fn context.HandlerFunc) *Request
- func (r *Request) UseResponse(fn context.HandlerFunc) *Request
- func (r *Request) XML(data interface{}) *Request
- type Response
- func (r *Response) Bytes() []byte
- func (r *Response) ClearInternalBuffer()
- func (r *Response) Close() error
- func (r *Response) JSON(userStruct interface{}) error
- func (r *Response) Read(p []byte) (n int, err error)
- func (r *Response) SaveToFile(fileName string) error
- func (r *Response) String() string
- func (r *Response) XML(userStruct interface{}, charsetReader utils.XMLCharDecoder) error
Constants ¶
const ( // UserAgent represents the static user agent name and version. UserAgent = "gentleman/" + Version )
const Version = "1.0.3"
Version defines the package semantic version
Variables ¶
var ( // DialTimeout represents the maximum amount of time the network dialer can take. DialTimeout = 30 * time.Second // DialKeepAlive represents the maximum amount of time too keep alive the socket. DialKeepAlive = 30 * time.Second // TLSHandshakeTimeout represents the maximum amount of time that // TLS handshake can take defined in the default http.Transport. TLSHandshakeTimeout = 10 * time.Second // RequestTimeout represents the maximum about of time that // a request can take, including dial / request / redirect processes. RequestTimeout = 60 * time.Second // DefaultDialer defines the default network dialer. DefaultDialer = &net.Dialer{ Timeout: DialTimeout, KeepAlive: DialKeepAlive, } // DefaultTransport stores the default HTTP transport to be used. DefaultTransport = NewDefaultTransport(DefaultDialer) )
var NewContext = context.New
NewContext is a convenient alias to context.New factory.
var NewHandler = context.NewHandler
NewHandler is a convenient alias to context.NewHandler factory.
var NewMiddleware = middleware.New
NewMiddleware is a convenient alias to middleware.New factory.
Functions ¶
func EnsureResponseFinalized ¶
func EnsureResponseFinalized(httpResp *Response)
EnsureResponseFinalized will ensure that when the Response is GCed the request body is closed so we aren't leaking fds.
Types ¶
type Client ¶
type Client struct { // Client entity can inherit behavior from a parent Client. Parent *Client // Each Client entity has it's own Context that will be inherited by requests or child clients. Context *context.Context // Client entity has its own Middleware layer to compose and inherit behavior. Middleware middleware.Middleware }
Client represents a high-level HTTP client entity capable with a built-in middleware and context.
func New ¶
func New() *Client
New creates a new high level client entity able to perform HTTP requests.
func (*Client) AddCookie ¶
AddCookie sets a new cookie field bsaed on the given http.Cookie struct without overwriting any existent cookie.
func (*Client) AddCookies ¶
AddCookies sets a new cookie field based on a list of http.Cookie without overwriting any existent cookie.
func (*Client) AddHeader ¶
AddHeader adds a new header field by name and value without overwriting any existent header.
func (*Client) BaseURL ¶
BaseURL defines the URL schema and host for client requests. Useful to define at client level the base URL used by client child requests.
func (*Client) CookieJar ¶
CookieJar creates a cookie jar to store HTTP cookies when they are sent down.
func (*Client) SetHeader ¶
SetHeader sets a new header field by name and value. If another header exists with the same key, it will be overwritten.
func (*Client) SetHeaders ¶
SetHeaders adds new header fields based on the given map.
func (*Client) URL ¶
URL defines the URL for client requests. Useful to define at client level the base URL and base path used by child requests.
func (*Client) UseError ¶
func (c *Client) UseError(fn context.HandlerFunc) *Client
UseError uses a new middleware function for error phase.
func (*Client) UseHandler ¶
func (c *Client) UseHandler(phase string, fn context.HandlerFunc) *Client
UseHandler uses a new middleware function for the given phase.
func (*Client) UseParent ¶
UseParent uses another Client as parent inheriting its middleware stack and configuration.
func (*Client) UseRequest ¶
func (c *Client) UseRequest(fn context.HandlerFunc) *Client
UseRequest uses a new middleware function for request phase.
func (*Client) UseResponse ¶
func (c *Client) UseResponse(fn context.HandlerFunc) *Client
UseResponse uses a new middleware function for response phase.
type Dispatcher ¶
type Dispatcher struct {
// contains filtered or unexported fields
}
Dispatcher dispatches a given request triggering the middleware layer per specific phase and handling the request/response/error states accondingly.
func NewDispatcher ¶
func NewDispatcher(req *Request) *Dispatcher
NewDispatcher creates a new Dispatcher based on the given Context.
func (*Dispatcher) Dispatch ¶
func (d *Dispatcher) Dispatch() *c.Context
Dispatch triggers the middleware chains and performs the HTTP request.
type Request ¶
type Request struct { // Optional reference to the gentleman.Client instance Client *Client // Request scope Context instance Context *context.Context // Request scope Middleware instance Middleware middleware.Middleware // contains filtered or unexported fields }
Request HTTP entity for gentleman. Provides middleware capabilities, built-in context and convenient methods to easily setup request params.
func (*Request) AddCookie ¶
AddCookie sets a new cookie field bsaed on the given http.Cookie struct without overwriting any existent cookie.
func (*Request) AddCookies ¶
AddCookies sets a new cookie field based on a list of http.Cookie without overwriting any existent cookie.
func (*Request) AddHeader ¶
AddHeader adds a new header field by name and value without overwriting any existent header.
func (*Request) AddQuery ¶
AddQuery adds a new URL query param field without overwriting any existent query field.
func (*Request) BaseURL ¶
BaseURL parses the given URL and uses the URL schema and host in the outgoing request.
func (*Request) BodyString ¶
BodyString defines the request body based on the given string. If using this method, you should define the proper Content-Type header representing the real content MIME type.
func (*Request) CookieJar ¶
CookieJar creates a cookie jar to store HTTP cookies when they are sent down.
func (*Request) File ¶
File serializes and defines the request body as multipart/form-data containing one file field.
func (*Request) Files ¶
Files serializes and defines the request body as multipart/form-data containing the given file fields.
func (*Request) Form ¶
Form serializes and defines the request body as multipart/form-data based on the given form data.
func (*Request) JSON ¶
JSON serializes and defines as request body based on the given input. The proper Content-Type header will be transparently added for you.
func (*Request) Send ¶
Send is an alias to Do(), which executes the current request and returns the response.
func (*Request) SetClient ¶
SetClient Attach a client to the current Request This is mostly done internally.
func (*Request) SetHeader ¶
SetHeader sets a new header field by name and value. If another header exists with the same key, it will be overwritten.
func (*Request) SetHeaders ¶
SetHeaders adds new header fields based on the given map.
func (*Request) SetQuery ¶
SetQuery sets a new URL query param field. If another query param exists with the same key, it will be overwritten.
func (*Request) SetQueryParams ¶
SetQueryParams sets URL query params based on the given map.
func (*Request) Type ¶
Type defines the Content-Type header field based on the given type name alias or value. You can use the following content type aliases: json, xml, form, html, text and urlencoded.
func (*Request) UseError ¶
func (r *Request) UseError(fn context.HandlerFunc) *Request
UseError uses an error middleware handler.
func (*Request) UseHandler ¶
func (r *Request) UseHandler(phase string, fn context.HandlerFunc) *Request
UseHandler uses an new middleware handler for the given phase.
func (*Request) UseRequest ¶
func (r *Request) UseRequest(fn context.HandlerFunc) *Request
UseRequest uses a request middleware handler.
func (*Request) UseResponse ¶
func (r *Request) UseResponse(fn context.HandlerFunc) *Request
UseResponse uses a response middleware handler.
type Response ¶
type Response struct { // Ok is a boolean flag that validates that the server returned a 2xx code. Ok bool // This is the Go error flag – if something went wrong within the request, this flag will be set. Error error // Sugar to check if the response status code is a client error (4xx). ClientError bool // Sugar to check if the response status code is a server error (5xx). ServerError bool // StatusCode is the HTTP Status Code returned by the HTTP Response. Taken from resp.StatusCode. StatusCode int // Header stores the response headers as http.Header interface. Header http.Header // Cookies stores the parsed response cookies. Cookies []*http.Cookie // Expose the native Go http.Response object for convenience. RawResponse *http.Response // Expose the native Go http.Request object for convenience. RawRequest *http.Request // Expose original request Context for convenience. Context *context.Context // contains filtered or unexported fields }
Response provides a more convenient and higher level Response struct. Implements an io.ReadCloser interface.
func (*Response) ClearInternalBuffer ¶
func (r *Response) ClearInternalBuffer()
ClearInternalBuffer is a function that will clear the internal buffer that we use to hold the .String() and .Bytes() data. Once you have used these functions you may want to free up the memory.
func (*Response) Close ¶
Close is part of our ability to support io.ReadCloser if someone wants to make use of the raw body.
func (*Response) JSON ¶
JSON is a method that will populate a struct that is provided `userStruct` with the JSON returned within the response body.
func (*Response) Read ¶
Read is part of our ability to support io.ReadCloser if someone wants to make use of the raw body.
func (*Response) SaveToFile ¶
SaveToFile allows you to download the contents of the response to a file.
Directories ¶
Path | Synopsis |
---|---|
_examples
|
|
Package context implements a simple request-aware HTTP context used by plugins and exposed by the middleware layer, designed to share polymorfic data types across plugins in the middleware call chain.
|
Package context implements a simple request-aware HTTP context used by plugins and exposed by the middleware layer, designed to share polymorfic data types across plugins in the middleware call chain. |
Package middleware implements an HTTP client domain-specific phase-oriented middleware layer used internally by gentleman packages.
|
Package middleware implements an HTTP client domain-specific phase-oriented middleware layer used internally by gentleman packages. |
Package mux implements an HTTP domain-specific traffic multiplexer with built-in matchers and features for easy plugin composition and activable logic.
|
Package mux implements an HTTP domain-specific traffic multiplexer with built-in matchers and features for easy plugin composition and activable logic. |
Package plugin implements a plugin layer for gentleman components.
|
Package plugin implements a plugin layer for gentleman components. |
plugins
|
|
Package utils provides a set of reusable HTTP client utilities used internally in gentleman for required functionality and testing.
|
Package utils provides a set of reusable HTTP client utilities used internally in gentleman for required functionality and testing. |