diff --git a/directions.go b/directions.go index e687674..6c63854 100644 --- a/directions.go +++ b/directions.go @@ -5,6 +5,8 @@ import ( "net/url" "strings" "time" + + "code.google.com/p/go.net/context" ) const ( @@ -35,9 +37,9 @@ const ( // Directions requests routes between orig and dest Locations. // // See https://developers.google.com/maps/documentation/directions/ -func (c Client) Directions(orig, dest Location, opts *DirectionsOpts) ([]Route, error) { +func (c Client) Directions(ctx context.Context, orig, dest Location, opts *DirectionsOpts) ([]Route, error) { var d directionsResponse - if err := c.doDecode(baseURL+directions(orig, dest, opts), &d); err != nil { + if err := c.doDecode(ctx, baseURL+directions(orig, dest, opts), &d); err != nil { return nil, err } if d.Status != StatusOK { diff --git a/distance.go b/distance.go index 8b9b0f6..ba6d1f8 100644 --- a/distance.go +++ b/distance.go @@ -4,14 +4,16 @@ import ( "fmt" "net/url" "time" + + "code.google.com/p/go.net/context" ) // DistanceMatrix requests travel distance and time for a matrix of origins and destinations. // // See https://developers.google.com/maps/documentation/distancematrix/ -func (c Client) DistanceMatrix(orig, dest []Location, opts *DistanceMatrixOpts) (*DistanceMatrixResult, error) { +func (c Client) DistanceMatrix(ctx context.Context, orig, dest []Location, opts *DistanceMatrixOpts) (*DistanceMatrixResult, error) { var d distanceResponse - if err := c.doDecode(baseURL+distancematrix(orig, dest, opts), &d); err != nil { + if err := c.doDecode(ctx, baseURL+distancematrix(orig, dest, opts), &d); err != nil { return nil, err } if d.Status != StatusOK { diff --git a/elevation.go b/elevation.go index aaf4265..cbf5557 100644 --- a/elevation.go +++ b/elevation.go @@ -3,14 +3,16 @@ package maps import ( "fmt" "net/url" + + "code.google.com/p/go.net/context" ) // Elevation requests elevation data for a series of locations. // // See https://developers.google.com/maps/documentation/elevation/ -func (c Client) Elevation(ll []LatLng) ([]ElevationResult, error) { +func (c Client) Elevation(ctx context.Context, ll []LatLng) ([]ElevationResult, error) { var r elevationResponse - if err := c.doDecode(baseURL+elevation(ll), &r); err != nil { + if err := c.doDecode(ctx, baseURL+elevation(ll), &r); err != nil { return nil, err } if r.Status != StatusOK { @@ -22,9 +24,9 @@ func (c Client) Elevation(ll []LatLng) ([]ElevationResult, error) { // ElevationPolyline requests elevation data for a series of locations as specified as an encoded polyline. // // See https://developers.google.com/maps/documentation/elevation/#Locations -func (c Client) ElevationPolyline(p string) ([]ElevationResult, error) { +func (c Client) ElevationPolyline(ctx context.Context, p string) ([]ElevationResult, error) { var r elevationResponse - if err := c.doDecode(baseURL+elevationpoly(p), &r); err != nil { + if err := c.doDecode(ctx, baseURL+elevationpoly(p), &r); err != nil { return nil, err } if r.Status != StatusOK { @@ -34,9 +36,9 @@ func (c Client) ElevationPolyline(p string) ([]ElevationResult, error) { } // ElevationPath requests elevation data for a number of samples along a path described as a series of locations. -func (c Client) ElevationPath(ll []LatLng, samples int) ([]ElevationResult, error) { +func (c Client) ElevationPath(ctx context.Context, ll []LatLng, samples int) ([]ElevationResult, error) { var r elevationResponse - if err := c.doDecode(baseURL+elevationpath(ll, samples), &r); err != nil { + if err := c.doDecode(ctx, baseURL+elevationpath(ll, samples), &r); err != nil { return nil, err } if r.Status != StatusOK { @@ -46,9 +48,9 @@ func (c Client) ElevationPath(ll []LatLng, samples int) ([]ElevationResult, erro } // ElevationPathPoly requests elevation data for a number of samples along a path described as a series of locations specified as an encoded polyline. -func (c Client) ElevationPathPoly(p string, samples int) ([]ElevationResult, error) { +func (c Client) ElevationPathPoly(ctx context.Context, p string, samples int) ([]ElevationResult, error) { var r elevationResponse - if err := c.doDecode(baseURL+elevationpathpoly(p, samples), &r); err != nil { + if err := c.doDecode(ctx, baseURL+elevationpathpoly(p, samples), &r); err != nil { return nil, err } if r.Status != StatusOK { diff --git a/geocode.go b/geocode.go index 2dc457b..9b6725f 100644 --- a/geocode.go +++ b/geocode.go @@ -4,6 +4,8 @@ import ( "fmt" "net/url" "strings" + + "code.google.com/p/go.net/context" ) const ( @@ -31,9 +33,9 @@ const ( ) // Geocode requests conversion of an address to a latitude/longitude pair. -func (c Client) Geocode(opts *GeocodeOpts) ([]GeocodeResult, error) { +func (c Client) Geocode(ctx context.Context, opts *GeocodeOpts) ([]GeocodeResult, error) { var r geocodeResponse - if err := c.doDecode(baseURL+geocode(opts), &r); err != nil { + if err := c.doDecode(ctx, baseURL+geocode(opts), &r); err != nil { return nil, err } if r.Status != StatusOK { @@ -192,9 +194,9 @@ type GeocodeResult struct { // ReverseGeocode requests conversion of a location to its nearest address. // // See https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding -func (c Client) ReverseGeocode(ll LatLng, opts *ReverseGeocodeOpts) ([]GeocodeResult, error) { +func (c Client) ReverseGeocode(ctx context.Context, ll LatLng, opts *ReverseGeocodeOpts) ([]GeocodeResult, error) { var r geocodeResponse - if err := c.doDecode(baseURL+reversegeocode(ll, opts), &r); err != nil { + if err := c.doDecode(ctx, baseURL+reversegeocode(ll, opts), &r); err != nil { return nil, err } if r.Status != StatusOK { diff --git a/maps.go b/maps.go index 39b09ac..4afe1fa 100644 --- a/maps.go +++ b/maps.go @@ -8,6 +8,8 @@ import ( "fmt" "net/http" "strings" + + "code.google.com/p/go.net/context" ) const ( @@ -64,7 +66,9 @@ 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) { +func (c Client) do(ctx context.Context, url string) (*http.Response, error) { + // TODO: Use ctx here. + t := c.Transport if t == nil { t = http.DefaultTransport @@ -109,8 +113,8 @@ func (c Client) genSig(path, query string) (string, error) { return base64.URLEncoding.EncodeToString(d.Sum(nil)), nil } -func (c Client) doDecode(url string, r interface{}) error { - resp, err := c.do(url) +func (c Client) doDecode(ctx context.Context, url string, r interface{}) error { + resp, err := c.do(ctx, url) if err != nil { return err } diff --git a/maps_test.go b/maps_test.go index 214fc99..03c3459 100644 --- a/maps_test.go +++ b/maps_test.go @@ -4,8 +4,12 @@ import ( "image/color" "testing" "time" + + "code.google.com/p/go.net/context" ) +var ctx = context.Background() + func TestDirections(t *testing.T) { c := New("", nil) orig, dest := Address("111 8th Ave, NYC"), Address("170 E 92nd St, NYC") @@ -15,7 +19,7 @@ func TestDirections(t *testing.T) { Alternatives: true, } t.Logf("%s", baseURL+directions(orig, dest, opts)) - r, err := c.Directions(orig, dest, opts) + r, err := c.Directions(ctx, orig, dest, opts) if err != nil { t.Errorf("unexpected error: %v", err) } @@ -73,7 +77,7 @@ func TestStaticMap(t *testing.T) { }, } t.Logf("%s", baseURL+staticmap(s, opts)) - if _, err := c.StaticMap(s, opts); err != nil { + if _, err := c.StaticMap(ctx, s, opts); err != nil { t.Errorf("unexpected error: %v", err) } } @@ -87,7 +91,7 @@ func TestStreetView(t *testing.T) { Pitch: -0.76, } t.Logf("%s", baseURL+streetview(s, opts)) - if _, err := c.StreetView(s, opts); err != nil { + if _, err := c.StreetView(ctx, s, opts); err != nil { t.Errorf("unexpected error: %v", err) } } @@ -97,7 +101,7 @@ func TestTimeZone(t *testing.T) { ll := LatLng{40.7142700, -74.0059700} tm := time.Now() t.Logf("%s", baseURL+timezone(ll, tm, nil)) - r, err := c.TimeZone(ll, tm, nil) + r, err := c.TimeZone(ctx, ll, tm, nil) if err != nil { t.Errorf("unexpected error: %v", err) } @@ -108,7 +112,7 @@ func TestElevation(t *testing.T) { c := New("", nil) ll := []LatLng{{39.7391536, -104.9847034}} t.Logf("%s", baseURL+elevation(ll)) - r, err := c.Elevation(ll) + r, err := c.Elevation(ctx, ll) if err != nil { t.Errorf("unexpected error: %v", err) } @@ -116,7 +120,7 @@ func TestElevation(t *testing.T) { p := "gfo}EtohhU" t.Logf("%s", baseURL+elevationpoly(p)) - r, err = c.ElevationPolyline(p) + r, err = c.ElevationPolyline(ctx, p) if err != nil { t.Errorf("unexpected error: %v", err) } @@ -125,7 +129,7 @@ func TestElevation(t *testing.T) { samples := 3 ll = []LatLng{{36.578581, -118.291994}, {36.23998, -116.83171}} t.Logf("%s", baseURL+elevationpath(ll, samples)) - r, err = c.ElevationPath(ll, samples) + r, err = c.ElevationPath(ctx, ll, samples) if err != nil { t.Errorf("unexpected error: %v", err) } @@ -133,7 +137,7 @@ func TestElevation(t *testing.T) { p = "gfo}EtohhUxD@bAxJmGF" t.Logf("%s", baseURL+elevationpathpoly(p, samples)) - r, err = c.ElevationPathPoly(p, samples) + r, err = c.ElevationPathPoly(ctx, p, samples) if err != nil { t.Errorf("unexpected error: %v", err) } @@ -146,7 +150,7 @@ func TestGeocode(t *testing.T) { Address: Address("1600 Amphitheatre Parkway, Mountain View, CA"), } t.Logf("%s", baseURL+geocode(opts)) - r, err := c.Geocode(opts) + r, err := c.Geocode(ctx, opts) if err != nil { t.Errorf("unexpected error: %v", err) } @@ -154,7 +158,7 @@ func TestGeocode(t *testing.T) { ll := LatLng{40.714224, -73.961452} t.Logf("%s", baseURL+reversegeocode(ll, nil)) - r, err = c.ReverseGeocode(ll, nil) + r, err = c.ReverseGeocode(ctx, ll, nil) if err != nil { t.Errorf("unexpected error: %v", err) } @@ -170,7 +174,7 @@ func TestDistanceMatrix(t *testing.T) { Language: "fr-FR", } t.Logf("%s", baseURL+distancematrix(orig, dst, opts)) - r, err := c.DistanceMatrix(orig, dst, opts) + r, err := c.DistanceMatrix(ctx, orig, dst, opts) if err != nil { t.Errorf("unexpected error: %v", err) } diff --git a/static.go b/static.go index 027d6ef..df32942 100644 --- a/static.go +++ b/static.go @@ -7,6 +7,8 @@ import ( "net/http" "net/url" "strings" + + "code.google.com/p/go.net/context" ) const ( @@ -46,8 +48,8 @@ const ( ) // StaticMap requests a static map image of a requested size. -func (c Client) StaticMap(s Size, opts *StaticMapOpts) (io.ReadCloser, error) { - resp, err := c.do(baseURL + staticmap(s, opts)) +func (c Client) StaticMap(ctx context.Context, s Size, opts *StaticMapOpts) (io.ReadCloser, error) { + resp, err := c.do(ctx, baseURL+staticmap(s, opts)) if err != nil { return nil, err } diff --git a/streetview.go b/streetview.go index ee63bbc..4ad5007 100644 --- a/streetview.go +++ b/streetview.go @@ -5,11 +5,13 @@ import ( "io" "net/http" "net/url" + + "code.google.com/p/go.net/context" ) // StreetView requests a static StreetView image of the requested size. -func (c Client) StreetView(s Size, opts *StreetViewOpts) (io.ReadCloser, error) { - resp, err := c.do(baseURL + streetview(s, opts)) +func (c Client) StreetView(ctx context.Context, s Size, opts *StreetViewOpts) (io.ReadCloser, error) { + resp, err := c.do(ctx, baseURL+streetview(s, opts)) if err != nil { return nil, err } diff --git a/timezone.go b/timezone.go index 0535526..4a6e1d2 100644 --- a/timezone.go +++ b/timezone.go @@ -4,14 +4,16 @@ import ( "fmt" "net/url" "time" + + "code.google.com/p/go.net/context" ) // TimeZone requests time zone information about a location. // // See https://developers.google.com/maps/documentation/timezone/ -func (c Client) TimeZone(ll LatLng, t time.Time, opts *TimeZoneOpts) (*TimeZoneResult, error) { +func (c Client) TimeZone(ctx context.Context, ll LatLng, t time.Time, opts *TimeZoneOpts) (*TimeZoneResult, error) { var r timeZoneResponse - if err := c.doDecode(baseURL+timezone(ll, t, opts), &r); err != nil { + if err := c.doDecode(ctx, baseURL+timezone(ll, t, opts), &r); err != nil { return nil, err } if r.Status != StatusOK {