Allow configs to be initialised with zero values for Client and Transport.

This commit is contained in:
Burcu Dogan
2014-09-02 14:06:51 -07:00
parent 32b45383ad
commit 8524783bd7
5 changed files with 77 additions and 31 deletions

View File

@@ -3,6 +3,8 @@
package google
import (
"net/http"
"github.com/golang/oauth2"
"appengine"
@@ -25,11 +27,6 @@ type AppEngineConfig struct {
// provided auth scopes.
func NewAppEngineConfig(context appengine.Context, scopes []string) *AppEngineConfig {
return &AppEngineConfig{
Transport: &urlfetch.Transport{
Context: context,
Deadline: 0,
AllowInvalidServerCertificate: false,
},
context: context,
scopes: scopes,
}
@@ -38,7 +35,7 @@ func NewAppEngineConfig(context appengine.Context, scopes []string) *AppEngineCo
// NewTransport returns a transport that authorizes
// the requests with the application's service account.
func (c *AppEngineConfig) NewTransport() *oauth2.Transport {
return oauth2.NewTransport(c.Transport, c, nil)
return oauth2.NewTransport(c.transport(), c, nil)
}
// FetchToken fetches a new access token for the provided scopes.
@@ -52,3 +49,10 @@ func (c *AppEngineConfig) FetchToken(existing *oauth2.Token) (*oauth2.Token, err
Expiry: expiry,
}, nil
}
func (c *AppEngineConfig) transport() http.RoundTripper {
if c.Transport != nil {
return c.Transport
}
return &urlfetch.Transport{Context: c.context}
}

View File

@@ -12,7 +12,7 @@ import (
// AppEngineConfig represents a configuration for an
// App Engine application's Google service account.
type AppEngineConfig struct {
// Transport is the round tripper to be used
// Transport is the http.RoundTripper to be used
// to construct new oauth2.Transport instances from
// this configuration.
Transport http.RoundTripper
@@ -25,16 +25,15 @@ type AppEngineConfig struct {
// provided auth scopes.
func NewAppEngineConfig(context appengine.Context, scopes []string) *AppEngineConfig {
return &AppEngineConfig{
Transport: http.DefaultTransport,
context: context,
scopes: scopes,
context: context,
scopes: scopes,
}
}
// NewTransport returns a transport that authorizes
// the requests with the application's service account.
func (c *AppEngineConfig) NewTransport() *oauth2.Transport {
return oauth2.NewTransport(c.Transport, c, nil)
return oauth2.NewTransport(c.transport(), c, nil)
}
// FetchToken fetches a new access token for the provided scopes.
@@ -48,3 +47,10 @@ func (c *AppEngineConfig) FetchToken(existing *oauth2.Token) (*oauth2.Token, err
Expiry: expiry,
}, nil
}
func (c *AppEngineConfig) transport() http.RoundTripper {
if c.Transport != nil {
return c.Transport
}
return http.DefaultTransport
}

View File

@@ -65,16 +65,12 @@ func NewServiceAccountConfig(opts *oauth2.JWTOptions) (*oauth2.JWTConfig, error)
// from Google Compute Engine instance's metaserver. If no account is
// provided, default is used.
func NewComputeEngineConfig(account string) *ComputeEngineConfig {
return &ComputeEngineConfig{
Client: http.DefaultClient,
Transport: http.DefaultTransport,
account: account,
}
return &ComputeEngineConfig{account: account}
}
// NewTransport creates an authorized transport.
func (c *ComputeEngineConfig) NewTransport() *oauth2.Transport {
return oauth2.NewTransport(c.Transport, c, nil)
return oauth2.NewTransport(c.transport(), c, nil)
}
// FetchToken retrieves a new access token via metadata server.
@@ -89,7 +85,7 @@ func (c *ComputeEngineConfig) FetchToken(existing *oauth2.Token) (token *oauth2.
return
}
req.Header.Add("X-Google-Metadata-Request", "True")
resp, err := c.Client.Do(req)
resp, err := c.client().Do(req)
if err != nil {
return
}
@@ -106,3 +102,17 @@ func (c *ComputeEngineConfig) FetchToken(existing *oauth2.Token) (token *oauth2.
}
return
}
func (c *ComputeEngineConfig) transport() http.RoundTripper {
if c.Transport != nil {
return c.Transport
}
return http.DefaultTransport
}
func (c *ComputeEngineConfig) client() *http.Client {
if c.Client != nil {
return c.Client
}
return http.DefaultClient
}