Documentation ¶
Overview ¶
Package httpclient provides an HTTP client with several conveniency functions.
This package is cross platform, so it works on both standalone deployments as well as on App Engine.
Also, requests made with an httpclient instance are properly measured when profiling an app.
Index ¶
- Variables
- type Client
- func (c *Client) Clone(ctx Context) *Client
- func (c *Client) Do(req *http.Request) (*Response, error)
- func (c *Client) Get(url string) (*Response, error)
- func (c *Client) GetForm(url string, data url.Values) (*Response, error)
- func (c *Client) HTTPClient() *http.Client
- func (c *Client) Head(url string) (*Response, error)
- func (c *Client) Iter(req *http.Request) *Iter
- func (c *Client) Post(url string, bodyType string, body io.Reader) (*Response, error)
- func (c *Client) PostForm(url string, data url.Values) (*Response, error)
- func (c *Client) Proxy() Proxy
- func (c *Client) SetProxy(proxy Proxy) *Client
- func (c *Client) SetTimeout(timeout time.Duration) *Client
- func (c *Client) SetUserAgent(ua string) *Client
- func (c *Client) SupportsProxy() bool
- func (c *Client) Timeout() time.Duration
- func (c *Client) Transport() Transport
- func (c *Client) Trip(req *http.Request) (*Response, error)
- func (c *Client) UserAgent() string
- type Context
- type Iter
- type Proxy
- type Response
- func (r *Response) Close() error
- func (r *Response) Cookie(name string) string
- func (r *Response) DecodeJSON(out interface{}) error
- func (r *Response) IsOK() bool
- func (r *Response) IsRedirect() bool
- func (r *Response) Read(p []byte) (int, error)
- func (r *Response) ReadAll() ([]byte, error)
- func (r *Response) Redirect() (string, error)
- func (r *Response) String() string
- func (r *Response) URL() *url.URL
- func (r *Response) UnmarshalJSON(out interface{}) error
- type Transport
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultTimeout is the default timeout for Transport instances. DefaultTimeout = 30 * time.Second // DefaultUserAgent is the default user agent for Transport instances. DefaultUserAgent = "Mozilla/5.0 (compatible; Gondola/1.0; +http://www.gondolaweb.com)" )
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func New ¶
New returns a new *Client. The ctx parameter will usually be the *app.Context received by the handler which is calling this function. Note that passing a nil ctx will work under some circunstances but will fail when running on App Engine, so it's advisable to always pass an *app.Context to this function, for portability.
func (*Client) Do ¶
Do is a wrapper around http.Client.Do, returning a Response rather than an http.Response. See http.Client.Do for further details.
func (*Client) Get ¶
Get is a wrapper around http.Client.Get, returning a Response rather than an http.Response. See http.Client.Get for further details.
func (*Client) GetForm ¶
GetForm appends the given data to the given url and performs a GET request with it. See Get for further details.
func (*Client) HTTPClient ¶
HTTPClient returns the *http.Client used by this Client.
func (*Client) Head ¶
Head is a wrapper around http.Client.Head, returning a Response rather than an http.Response. See http.Client.Head for further details.
func (*Client) Iter ¶
Iter returns an iterator for the given *http.Request. See Iter for further details
func (*Client) Post ¶
Post is a wrapper around http.Client.Post, returning a Response rather than an http.Response. See http.Client.Post for further details.
func (*Client) PostForm ¶
PostForm is a wrapper around http.Client.PostForm, returning a Response rather than an http.Response. See http.Client.PostForm for further details.
func (*Client) Proxy ¶
Proxy returns the Proxy function for this client, if any. Note that if SupportsProxy() returns false, this function will always return nil.
func (*Client) SetProxy ¶
SetProxy sets the Proxy function for this client. Setting it to nil disables any previously set proxy function. Note that if SupportsProxy() returns false, this function is a no-op.
func (*Client) SetTimeout ¶
SetTimeout sets the timeout for requests sent by this client. Setting it to 0 disables timeouts.
func (*Client) SetUserAgent ¶
SetUserAgent sets the default user agent.
func (*Client) SupportsProxy ¶
SupportsProxy returns if the current runtime environement supports setting a proxy. Currently, this is false on App Engine and true otherwise.
func (*Client) Timeout ¶
Timeout returns the timeout for this transport. The timeout represents the maximum total time for the request, including DNS resolution, connection establishment and the time spent reading the response body. Client initializes this value to DefaultTimeout.
type Context ¶
type Context interface { // Request returns the current in-flight request. Request() *http.Request }
Context is the interface required to instantiate a *Client.
type Iter ¶
type Iter struct {
// contains filtered or unexported fields
}
Iter allows iterating over a chain of redirects until reaching a Response which is not a redirect.
Example ¶
package main import ( "fmt" "net/http" "gondola/net/httpclient" ) func main() { // Passing nil only works on non-App Engine and while // running tests. Usually you should pass a *app.Context // to httpclient.New. c := httpclient.New(nil) req, err := http.NewRequest("GET", "http://httpbin.org/relative-redirect/3", nil) if err != nil { panic(err) } iter := c.Iter(req) // Don't forget to close the Iter after you're done with it defer iter.Close() var urls []string for iter.Next() { urls = append(urls, iter.Response().URL().String()) } // iter.Assert() could also be used here if err := iter.Err(); err != nil { panic(err) } fmt.Println("Last", iter.Response().URL()) fmt.Println("Intermediate", urls) }
Output: Last http://httpbin.org/get Intermediate [http://httpbin.org/relative-redirect/3 http://httpbin.org/relative-redirect/2 http://httpbin.org/relative-redirect/1]
func (*Iter) Err ¶
Err returns the latest error produced while executing an *http.Request by this Iter.
func (*Iter) Next ¶
Next returns true iff the Iter could perform a trip using Client.Trip with the current *http.Request. Calling Next() closes the previous *Response automatically if it was a redirect.
type Response ¶
Response is a thin wrapper around *http.Response, adding a few conveniency methods.
func (*Response) Cookie ¶
Cookie returns the value for the first Cookie with the given name, or the empty string if no such cookie exists.
func (*Response) DecodeJSON ¶
DecodeJSON uses a json.Decoder to read and decode the next JSON-encoded value from the response body and stores it in the value pointed to by out.
func (*Response) IsRedirect ¶
IsRedirect returns true if the Response is a redirect (status codes 301, 302, 303 and 307).
func (*Response) Redirect ¶
Redirect returns the redirect target as an absolute URL. If the Response is not a redirect of the pointed URL is not valid, an error is returned.
func (*Response) UnmarshalJSON ¶
UnmarshalJSON reads the whole response body and decodes it as JSON in the provided out parameter using json.Unmarshal. If you need to sequentially decode several objects (e.g. a streaming response), see DecodeJSON.
type Transport ¶
type Transport interface { http.RoundTripper // Timeout returns the timeout for this transport. // The timeout represents the maximum total time for // the request, including DNS resolution, connection // establishment and the time spent reading the response // body. Timeout() time.Duration // SetTimeout sets the Transport's timeout. Setting it // to 0 disables timeouts. SetTimeout(time.Duration) // UserAgent returns the default user agent sent by requests // without an User-Agent header set. UserAgent() string // SetUserAgent sets the default user agent. SetUserAgent(string) // Underlying returns the underlying RoundTripper. Note // that most of the time this will return an *http.Transport, // but when running in GAE the returned value will be of type // *urlfetch.Transport. Underlying() http.RoundTripper // SetUnderlying changes the underlying http.RoundTripper. This // is useful if you're using another library which provides an // http.RoundTripper which adds some functionality while providing // composition with another http.RoundTripper. SetUnderlying(http.RoundTripper) }
Transport is the interface used as a transport by *Client.
func NewTransport ¶
NewTransport returns a new Transport for the given Context.