1
0
Fork 0
mirror of https://github.com/imjasonh/staticmaps synced 2026-07-08 02:45:10 +00:00

Proactively fail if SnapToRoads is called without an API key

This commit is contained in:
Jason Hall 2015-03-05 13:36:26 -05:00
parent e864f2a8af
commit 70a0403b6d
2 changed files with 15 additions and 4 deletions

View file

@ -11,7 +11,7 @@ import (
var (
apiKey = flag.String("apiKey", "", "Google Maps API key")
ctx = NewContext("key", &http.Client{})
ctx = NewContext("", &http.Client{})
wctx = NewWorkContext("clientID", "privKey", &http.Client{})
)
@ -184,8 +184,6 @@ func TestDistanceMatrix(t *testing.T) {
}
func TestSnapToRoads(t *testing.T) {
ctx = NewContext(*apiKey, &http.Client{})
path := []LatLng{{-35.27801, 149.12958},
{-35.28032, 149.12907},
{-35.28099, 149.12929},
@ -197,7 +195,13 @@ func TestSnapToRoads(t *testing.T) {
opts := &SnapToRoadsOpts{
Interpolate: true,
}
r, err := SnapToRoads(ctx, path, opts)
if _, err := SnapToRoads(ctx, path, opts); err != ErrNoAPIKey {
t.Errorf("unexpected error, got %v, want %v", err, ErrNoAPIKey)
}
keyCtx := NewContext(*apiKey, &http.Client{})
r, err := SnapToRoads(keyCtx, path, opts)
if err != nil {
t.Errorf("unexpected error: %v", err)
}

View file

@ -1,6 +1,7 @@
package maps
import (
"errors"
"net/url"
"code.google.com/p/go.net/context"
@ -8,7 +9,13 @@ import (
const roadsAPIBaseURL = "https://roads.googleapis.com/v1/"
var ErrNoAPIKey = errors.New("must provide an API key")
func SnapToRoads(ctx context.Context, path []LatLng, opts *SnapToRoadsOpts) ([]SnappedPoint, error) {
if key(ctx) == "" {
return nil, ErrNoAPIKey
}
var d snapToRoadsResponse
if err := doDecode(ctx, roadsAPIBaseURL+snapToRoads(path, opts), &d); err != nil {
return nil, err