1
0
Fork 0
mirror of https://github.com/imjasonh/staticmaps synced 2026-07-18 23:09:11 +00:00

slight style tweaks, use New and NewForWork, take a RoundTripper

This commit is contained in:
Jason Hall 2014-10-21 11:34:21 -04:00
parent 5971468ed1
commit b4ad65e7f2
4 changed files with 23 additions and 21 deletions

View file

@ -7,16 +7,13 @@ import (
) )
type backoff struct { type backoff struct {
Transport *http.RoundTripper Transport http.RoundTripper
MaxTries int MaxTries int
tries int tries int
sleep func(time.Duration) sleep func(time.Duration)
} }
func (bt *backoff) RoundTrip(req *http.Request) (*http.Response, error) { func (bt *backoff) RoundTrip(req *http.Request) (*http.Response, error) {
if bt.Transport == nil {
bt.Transport = &http.DefaultTransport
}
if bt.MaxTries == 0 { if bt.MaxTries == 0 {
bt.MaxTries = 5 bt.MaxTries = 5
} }
@ -27,7 +24,7 @@ func (bt *backoff) RoundTrip(req *http.Request) (*http.Response, error) {
var err error var err error
for ; bt.tries < bt.MaxTries; bt.tries++ { for ; bt.tries < bt.MaxTries; bt.tries++ {
var resp *http.Response var resp *http.Response
resp, err = (*bt.Transport).RoundTrip(req) resp, err = bt.Transport.RoundTrip(req)
if err != nil { if err != nil {
// fallthrough, retry // fallthrough, retry
} else if resp.StatusCode >= http.StatusInternalServerError { } else if resp.StatusCode >= http.StatusInternalServerError {

View file

@ -15,7 +15,8 @@ func TestBackoff(t *testing.T) {
tries := 10 tries := 10
sleeps := 0 sleeps := 0
bt := &backoff{ bt := &backoff{
MaxTries: tries, MaxTries: tries,
Transport: http.DefaultTransport,
sleep: func(d time.Duration) { sleep: func(d time.Duration) {
sleeps += 1 sleeps += 1
t.Logf("sleeping %s", d) t.Logf("sleeping %s", d)

18
maps.go
View file

@ -52,21 +52,25 @@ type Client struct {
PrivateKey string PrivateKey string
} }
// NewClient returns a Client using the specified API key. // New returns a Client using the specified API key and RoundTripper.
func NewClient(key string) Client { func New(key string, rt http.RoundTripper) Client {
return Client{Key: key, Transport: http.DefaultTransport} 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/ // See https://developers.google.com/maps/documentation/business/
func NewWorkClient(clientID, privateKey string) Client { func NewForWork(clientID, privateKey string, rt http.RoundTripper) Client {
return Client{ClientID: clientID, PrivateKey: privateKey, Transport: http.DefaultTransport} return Client{ClientID: clientID, PrivateKey: privateKey, Transport: rt}
} }
func (c Client) do(url string) (*http.Response, error) { func (c Client) do(url string) (*http.Response, error) {
t := c.Transport
if t == nil {
t = http.DefaultTransport
}
cl := &http.Client{Transport: &backoff{ cl := &http.Client{Transport: &backoff{
Transport: &c.Transport, Transport: t,
}} }}
req, err := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {

View file

@ -6,7 +6,7 @@ import (
) )
func TestDirections(t *testing.T) { func TestDirections(t *testing.T) {
c := NewClient("") c := New("", nil)
orig, dest := Address("111 8th Ave, NYC"), Address("170 E 92nd St, NYC") orig, dest := Address("111 8th Ave, NYC"), Address("170 E 92nd St, NYC")
opts := &DirectionsOpts{ opts := &DirectionsOpts{
Mode: ModeTransit, Mode: ModeTransit,
@ -22,7 +22,7 @@ func TestDirections(t *testing.T) {
} }
func TestStaticMap(t *testing.T) { func TestStaticMap(t *testing.T) {
c := NewClient("") c := New("", nil)
s := Size{512, 512} s := Size{512, 512}
opts := &StaticMapOpts{ opts := &StaticMapOpts{
Center: LatLng{-5, -5}, Center: LatLng{-5, -5},
@ -78,7 +78,7 @@ func TestStaticMap(t *testing.T) {
} }
func TestStreetView(t *testing.T) { func TestStreetView(t *testing.T) {
c := NewClient("") c := New("", nil)
s := Size{600, 300} s := Size{600, 300}
opts := &StreetViewOpts{ opts := &StreetViewOpts{
Location: &LatLng{46.414382, 10.013988}, Location: &LatLng{46.414382, 10.013988},
@ -92,7 +92,7 @@ func TestStreetView(t *testing.T) {
} }
func TestTimeZone(t *testing.T) { func TestTimeZone(t *testing.T) {
c := NewClient("") c := New("", nil)
ll := LatLng{40.7142700, -74.0059700} ll := LatLng{40.7142700, -74.0059700}
tm := time.Now() tm := time.Now()
t.Logf("%s", baseURL+timezone(ll, tm, nil)) t.Logf("%s", baseURL+timezone(ll, tm, nil))
@ -104,7 +104,7 @@ func TestTimeZone(t *testing.T) {
} }
func TestElevation(t *testing.T) { func TestElevation(t *testing.T) {
c := NewClient("") c := New("", nil)
ll := []LatLng{{39.7391536, -104.9847034}} ll := []LatLng{{39.7391536, -104.9847034}}
t.Logf("%s", baseURL+elevation(ll)) t.Logf("%s", baseURL+elevation(ll))
r, err := c.Elevation(ll) r, err := c.Elevation(ll)
@ -140,7 +140,7 @@ func TestElevation(t *testing.T) {
} }
func TestGeocode(t *testing.T) { func TestGeocode(t *testing.T) {
c := NewClient("") c := New("", nil)
opts := &GeocodeOpts{ opts := &GeocodeOpts{
Address: Address("1600 Amphitheatre Parkway, Mountain View, CA"), Address: Address("1600 Amphitheatre Parkway, Mountain View, CA"),
} }
@ -161,7 +161,7 @@ func TestGeocode(t *testing.T) {
} }
func TestDistanceMatrix(t *testing.T) { func TestDistanceMatrix(t *testing.T) {
c := NewClient("") c := New("", nil)
orig := []Location{Address("Vancouver, BC"), Address("Seattle")} orig := []Location{Address("Vancouver, BC"), Address("Seattle")}
dst := []Location{Address("San Francisco"), Address("Victoria, BC")} dst := []Location{Address("San Francisco"), Address("Victoria, BC")}
opts := &DistanceMatrixOpts{ opts := &DistanceMatrixOpts{
@ -179,7 +179,7 @@ func TestDistanceMatrix(t *testing.T) {
// Based on https://developers.google.com/maps/documentation/business/webservices/auth#signature_examples // Based on https://developers.google.com/maps/documentation/business/webservices/auth#signature_examples
func TestSignature(t *testing.T) { 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") sig, err := c.genSig("/maps/api/geocode/json", "address=New+York&client=clientID")
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)