api: document jar requirement

This commit is contained in:
Mitchell Hashimoto 2015-03-11 20:02:11 +01:00
parent a4fc46de2a
commit de159fdac8
1 changed files with 20 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package api
import ( import (
"net/http" "net/http"
"net/http/cookiejar"
"net/url" "net/url"
) )
@ -14,7 +15,10 @@ type Config struct {
Address string Address string
// HttpClient is the HTTP client to use. http.DefaultClient will be // HttpClient is the HTTP client to use. http.DefaultClient will be
// used if not specified. // used if not specified. The HTTP client must have the cookie jar set
// to be able to store cookies, otherwise authentication (login) will
// not work properly. If the jar is nil, a default empty cookie jar
// will be set.
HttpClient *http.Client HttpClient *http.Client
} }
@ -43,6 +47,21 @@ func NewClient(c Config) (*Client, error) {
return nil, err return nil, err
} }
// Make a copy of the HTTP client so we can configure it without
// affecting the original
//
// If no cookie jar is set on the client, we set a default empty
// cookie jar.
client := *c.HttpClient
if client.Jar == nil {
jar, err := cookiejar.New(&cookiejar.Options{})
if err != nil {
return nil, err
}
client.Jar = jar
}
return &Client{ return &Client{
addr: u, addr: u,
config: c, config: c,