From b4ad65e7f2b8f85b1c9d3861113c302099a8f311 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Tue, 21 Oct 2014 11:34:21 -0400 Subject: [PATCH] slight style tweaks, use New and NewForWork, take a RoundTripper --- backoff.go | 7 ++----- backoff_test.go | 3 ++- maps.go | 18 +++++++++++------- maps_test.go | 16 ++++++++-------- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/backoff.go b/backoff.go index 2187cd5..366e301 100644 --- a/backoff.go +++ b/backoff.go @@ -7,16 +7,13 @@ import ( ) type backoff struct { - Transport *http.RoundTripper + Transport http.RoundTripper MaxTries int tries int sleep func(time.Duration) } func (bt *backoff) RoundTrip(req *http.Request) (*http.Response, error) { - if bt.Transport == nil { - bt.Transport = &http.DefaultTransport - } if bt.MaxTries == 0 { bt.MaxTries = 5 } @@ -27,7 +24,7 @@ func (bt *backoff) RoundTrip(req *http.Request) (*http.Response, error) { var err error for ; bt.tries < bt.MaxTries; bt.tries++ { var resp *http.Response - resp, err = (*bt.Transport).RoundTrip(req) + resp, err = bt.Transport.RoundTrip(req) if err != nil { // fallthrough, retry } else if resp.StatusCode >= http.StatusInternalServerError { diff --git a/backoff_test.go b/backoff_test.go index 2dea33d..37acc9a 100644 --- a/backoff_test.go +++ b/backoff_test.go @@ -15,7 +15,8 @@ func TestBackoff(t *testing.T) { tries := 10 sleeps := 0 bt := &backoff{ - MaxTries: tries, + MaxTries: tries, + Transport: http.DefaultTransport, sleep: func(d time.Duration) { sleeps += 1 t.Logf("sleeping %s", d) diff --git a/maps.go b/maps.go index 2966fbb..39b09ac 100644 --- a/maps.go +++ b/maps.go @@ -52,21 +52,25 @@ type Client struct { PrivateKey string } -// NewClient returns a Client using the specified API key. -func NewClient(key string) Client { - return Client{Key: key, Transport: http.DefaultTransport} +// New returns a Client using the specified API key and RoundTripper. +func New(key string, rt http.RoundTripper) Client { + return Client{Key: key, Transport: rt} } -// NewWorkClient returns a Client using the specified Google Maps API for Work client ID and signature. +// NewForWork returns a Client using the specified Google Maps API for Work client ID and signature, and RoundTripper. // // See https://developers.google.com/maps/documentation/business/ -func NewWorkClient(clientID, privateKey string) Client { - return Client{ClientID: clientID, PrivateKey: privateKey, Transport: http.DefaultTransport} +func NewForWork(clientID, privateKey string, rt http.RoundTripper) Client { + return Client{ClientID: clientID, PrivateKey: privateKey, Transport: rt} } func (c Client) do(url string) (*http.Response, error) { + t := c.Transport + if t == nil { + t = http.DefaultTransport + } cl := &http.Client{Transport: &backoff{ - Transport: &c.Transport, + Transport: t, }} req, err := http.NewRequest("GET", url, nil) if err != nil { diff --git a/maps_test.go b/maps_test.go index 63cffa4..394da73 100644 --- a/maps_test.go +++ b/maps_test.go @@ -6,7 +6,7 @@ import ( ) func TestDirections(t *testing.T) { - c := NewClient("") + c := New("", nil) orig, dest := Address("111 8th Ave, NYC"), Address("170 E 92nd St, NYC") opts := &DirectionsOpts{ Mode: ModeTransit, @@ -22,7 +22,7 @@ func TestDirections(t *testing.T) { } func TestStaticMap(t *testing.T) { - c := NewClient("") + c := New("", nil) s := Size{512, 512} opts := &StaticMapOpts{ Center: LatLng{-5, -5}, @@ -78,7 +78,7 @@ func TestStaticMap(t *testing.T) { } func TestStreetView(t *testing.T) { - c := NewClient("") + c := New("", nil) s := Size{600, 300} opts := &StreetViewOpts{ Location: &LatLng{46.414382, 10.013988}, @@ -92,7 +92,7 @@ func TestStreetView(t *testing.T) { } func TestTimeZone(t *testing.T) { - c := NewClient("") + c := New("", nil) ll := LatLng{40.7142700, -74.0059700} tm := time.Now() t.Logf("%s", baseURL+timezone(ll, tm, nil)) @@ -104,7 +104,7 @@ func TestTimeZone(t *testing.T) { } func TestElevation(t *testing.T) { - c := NewClient("") + c := New("", nil) ll := []LatLng{{39.7391536, -104.9847034}} t.Logf("%s", baseURL+elevation(ll)) r, err := c.Elevation(ll) @@ -140,7 +140,7 @@ func TestElevation(t *testing.T) { } func TestGeocode(t *testing.T) { - c := NewClient("") + c := New("", nil) opts := &GeocodeOpts{ Address: Address("1600 Amphitheatre Parkway, Mountain View, CA"), } @@ -161,7 +161,7 @@ func TestGeocode(t *testing.T) { } func TestDistanceMatrix(t *testing.T) { - c := NewClient("") + c := New("", nil) orig := []Location{Address("Vancouver, BC"), Address("Seattle")} dst := []Location{Address("San Francisco"), Address("Victoria, BC")} opts := &DistanceMatrixOpts{ @@ -179,7 +179,7 @@ func TestDistanceMatrix(t *testing.T) { // Based on https://developers.google.com/maps/documentation/business/webservices/auth#signature_examples func TestSignature(t *testing.T) { - c := NewWorkClient("", "vNIXE0xscrmjlyV-12Nj_BvUPaw=") + c := NewForWork("", "vNIXE0xscrmjlyV-12Nj_BvUPaw=", nil) sig, err := c.genSig("/maps/api/geocode/json", "address=New+York&client=clientID") if err != nil { t.Errorf("unexpected error: %v", err)