-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparks.go
162 lines (141 loc) · 3.17 KB
/
parks.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package npsapi
import (
"context"
"encoding/json"
"errors"
"log"
"regexp"
"strconv"
"strings"
"sync"
"golang.org/x/sync/errgroup"
)
// ErrParkNotFound is a type of error returned when no parks were found.
var ErrParkNotFound = errors.New("park not found")
const (
fetchSize = 100 // Fetch up to 100 parks at once.
maxParks = 999 // Attempt to fetch a total of 999 parks.
)
// Park basics include location, contact, and operating hours for each national park.
type Park struct {
// UUID-style identifier for the park.
ID string
// Park name.
Name string
// Longer park name.
FullName string
// 4-character code to identify the park.
Code string
// States the park is in.
States []string
// Geospatial location of the park.
Location *LatLng
// Decription of the park.
Description string
// URL of the park website.
URL string
}
// LatLng cooordinate.
type LatLng struct {
// Latitude.
Lat float64
// Longitude
Lng float64
}
var latLngRe = regexp.MustCompile(`lat:([\d\.\-]+), long:([\d\.\-]+)`)
// ListParks finds all the parks.
func (c *Client) ListParks(ctx context.Context) ([]Park, error) {
g, ctx := errgroup.WithContext(ctx)
parks := []Park{}
m := sync.Mutex{}
for i := 0; i < maxParks; i += fetchSize {
i := i
g.Go(func() error {
params := map[string]string{
"limit": strconv.Itoa(fetchSize),
"start": strconv.Itoa(i),
}
p, err := c.fetchParks(ctx, params)
if err == nil {
m.Lock()
parks = append(parks, p...)
m.Unlock()
}
return err
})
}
if err := g.Wait(); err != nil {
return nil, err
}
return parks, nil
}
// GetPark finds a specific park with a 4-character code.
func (c *Client) GetPark(ctx context.Context, code string) (*Park, error) {
params := map[string]string{
"parkCode": code,
"limit": "1",
}
park, err := c.fetchParks(ctx, params)
if err != nil {
return nil, err
}
if len(park) >= 1 {
return &park[0], nil
}
return nil, ErrParkNotFound
}
func (c *Client) fetchParks(ctx context.Context, params map[string]string) ([]Park, error) {
resp, err := c.fetch(ctx, "parks", params)
if err != nil {
return nil, err
}
data := struct {
Data []struct {
ID string
Name string
FullName string
ParkCode string
States string
LatLong string
Description string
URL string
}
}{}
if err := json.Unmarshal(resp, &data); err != nil {
return nil, err
}
parks := []Park{}
for _, p := range data.Data {
latLng, err := parseLatLng(p.LatLong)
if err != nil {
log.Printf("error parsing %s: %v", p.LatLong, err)
}
park := Park{
ID: p.ID,
Name: p.Name,
FullName: p.FullName,
Code: p.ParkCode,
States: strings.Split(p.States, ","),
Location: latLng,
Description: p.Description,
URL: p.URL,
}
parks = append(parks, park)
}
return parks, nil
}
func parseLatLng(latLong string) (*LatLng, error) {
ll := latLngRe.FindStringSubmatch(latLong)
if len(ll) < 3 {
return nil, nil
}
lat, err := strconv.ParseFloat(ll[1], 64)
if err != nil {
return nil, err
}
lng, err := strconv.ParseFloat(ll[2], 64)
if err != nil {
return nil, err
}
return &LatLng{lat, lng}, nil
}