httpclient

package
v0.0.0-...-b75375f Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2014 License: MPL-2.0 Imports: 13 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

View Source
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

func New(ctx Context) *Client

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) Clone

func (c *Client) Clone(ctx Context) *Client

func (*Client) Do

func (c *Client) Do(req *http.Request) (*Response, error)

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

func (c *Client) Get(url string) (*Response, error)

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

func (c *Client) GetForm(url string, data url.Values) (*Response, error)

GetForm appends the given data to the given url and performs a GET request with it. See Get for further details.

func (*Client) HTTPClient

func (c *Client) HTTPClient() *http.Client

HTTPClient returns the *http.Client used by this Client.

func (*Client) Head

func (c *Client) Head(url string) (*Response, error)

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

func (c *Client) Iter(req *http.Request) *Iter

Iter returns an iterator for the given *http.Request. See Iter for further details

func (*Client) Post

func (c *Client) Post(url string, bodyType string, body io.Reader) (*Response, error)

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

func (c *Client) PostForm(url string, data url.Values) (*Response, error)

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

func (c *Client) Proxy() 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

func (c *Client) SetProxy(proxy Proxy) *Client

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

func (c *Client) SetTimeout(timeout time.Duration) *Client

SetTimeout sets the timeout for requests sent by this client. Setting it to 0 disables timeouts.

func (*Client) SetUserAgent

func (c *Client) SetUserAgent(ua string) *Client

SetUserAgent sets the default user agent.

func (*Client) SupportsProxy

func (c *Client) SupportsProxy() bool

SupportsProxy returns if the current runtime environement supports setting a proxy. Currently, this is false on App Engine and true otherwise.

func (*Client) Timeout

func (c *Client) Timeout() time.Duration

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.

func (*Client) Transport

func (c *Client) Transport() Transport

Transport returns the Transport used by this Client.

func (*Client) Trip

func (c *Client) Trip(req *http.Request) (*Response, error)

Trip performs a roundtrip with the given http.Request, without following any redirects, and returns the first response, which might be a redirect. It's basically a shorthand for c.Transport().RoundTrip(req).

func (*Client) UserAgent

func (c *Client) UserAgent() string

UserAgent returns the default user agent sent by requests without an User-Agent header set.

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"

	"gnd.la/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/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/redirect/3 http://httpbin.org/redirect/2 http://httpbin.org/redirect/1]

func (*Iter) Assert

func (i *Iter) Assert()

Assert panics if i.Err() returns non-nil.

func (*Iter) Close

func (i *Iter) Close() error

Close closes the last *Response returned by the Iter.

func (*Iter) Err

func (i *Iter) Err() error

Err returns the latest error produced while executing an *http.Request by this Iter.

func (*Iter) Next

func (i *Iter) Next() bool

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.

func (*Iter) Request

func (i *Iter) Request() *http.Request

Request returns the current *http.Request, if any.

func (*Iter) Response

func (i *Iter) Response() *Response

Response returns the current *Response, if any.

type Proxy

type Proxy func(*http.Request) (*url.URL, error)

Proxy is a function type which returns the proxy URL for the given request.

type Response

type Response struct {
	*http.Response
	// contains filtered or unexported fields
}

Response is a thin wrapper around *http.Response, adding a few conveniency methods.

func (*Response) Close

func (r *Response) Close() error

Close is a shorthand for r.Body.Close()

func (*Response) Cookie

func (r *Response) Cookie(name string) string

Cookie returns the value for the first Cookie with the given name, or the empty string if no such cookie exists.

func (*Response) DecodeJSON

func (r *Response) DecodeJSON(out interface{}) error

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) IsOK

func (r *Response) IsOK() bool

IsOK returns true iff the response status code is >= 200 and < 300.

func (*Response) IsRedirect

func (r *Response) IsRedirect() bool

IsRedirect returns true if the Response is a redirect (status codes 301, 302, 303 and 307).

func (*Response) Read

func (r *Response) Read(p []byte) (int, error)

Read wraps r.Body.Read().

func (*Response) ReadAll

func (r *Response) ReadAll() ([]byte, error)

ReadAll is a shorthand for ioutil.ReadAll(r.Body).

func (*Response) Redirect

func (r *Response) Redirect() (string, error)

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) String

func (r *Response) String() string

func (*Response) URL

func (r *Response) URL() *url.URL

URL is a shorthand for r.Request.URL.

func (*Response) UnmarshalJSON

func (r *Response) UnmarshalJSON(out interface{}) error

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

func NewTransport(ctx Context) Transport

NewTransport returns a new Transport for the given Context.

Jump to

Keyboard shortcuts

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