-
Notifications
You must be signed in to change notification settings - Fork 8
/
geocodio.go
72 lines (56 loc) · 1.4 KB
/
geocodio.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package geocodio
import (
"fmt"
"os"
"strings"
)
const (
// GeocodioAPIBaseURLv1 is the Geocod.io Base URL
GeocodioAPIBaseURLv1 = "https://api.geocod.io/v1.6"
)
// Geocodio is the base struct
type Geocodio struct {
APIKey string
}
type Input struct {
AddressComponents Components `json:"address_components"`
FormattedAddress string `json:"formatted_address"`
}
// New creates a Geocodio instance based on an API key in either the environment
// or passed in as the first string value
func New(apiKey ...string) (*Geocodio, error) {
key := os.Getenv(EnvGeocodioAPIKey)
if strings.TrimSpace(key) == "" {
key = os.Getenv(EnvOldAPIKey)
}
if len(apiKey) == 0 && strings.TrimSpace(key) == "" {
return nil, ErrMissingAPIKey
}
if len(apiKey) == 1 {
key = apiKey[0]
}
if strings.TrimSpace(key) == "" {
return nil, ErrMissingAPIKey
}
g := Geocodio{
APIKey: key,
}
return &g, nil
}
// NewGeocodio is a helper to create new Geocodio reference
// since 1.6+ this is kept for backwards compatiblity
// this is deprecatd and will be removed in 2+
func NewGeocodio(apiKey string) (*Geocodio, error) {
fmt.Println(`
NewGeocodio() is deprecated and will be removed in 2+
Use geocodio.New("YOUR_API_KEY")
or with the environment variable ` + EnvGeocodioAPIKey + `
Use geocodio.New()`)
if apiKey == "" {
return nil, ErrMissingAPIKey
}
g := Geocodio{
APIKey: apiKey,
}
return &g, nil
}