-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebird.go
217 lines (184 loc) · 6.12 KB
/
ebird.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package ebird
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
const (
APIEndpointBase = "https://api.ebird.org/v2/"
defaultTimeout = 10 * time.Second
)
// APIEndpoints holds all the API endpoints
var APIEndpoints = struct {
HistoricObservationsOnDate string
NearestObservationsOfSpecies string
RecentChecklistsFeed string
RecentNearbyNotableObservations string
RecentNearbyObservations string
RecentNearbyObservationsOfSpecies string
RecentNotableObservationsInRegion string
RecentObservationsInRegion string
RecentObservationsOfSpeciesInRegion string
ChecklistFeedOnDate string
RegionalStatisticsOnDate string
SpeciesListForRegion string
Top100 string
ViewChecklist string
AdjacentRegions string
HotspotInfo string
HotspotsInRegion string
NearbyHotspots string
EbirdTaxonomy string
TaxaLocaleCodes string
TaxonomicForms string
TaxonomicGroups string
TaxonomyVersions string
RegionInfo string
SubRegionInfo string
}{
HistoricObservationsOnDate: "data/obs/%s/historic/%d/%d/%d",
NearestObservationsOfSpecies: "data/nearest/geo/recent/%s",
RecentChecklistsFeed: "product/lists/%s",
RecentNearbyNotableObservations: "data/obs/geo/recent/notable",
RecentNearbyObservations: "data/obs/geo/recent",
RecentNearbyObservationsOfSpecies: "data/obs/geo/recent/%s",
RecentNotableObservationsInRegion: "data/obs/%s/recent/notable",
RecentObservationsInRegion: "data/obs/%s/recent",
RecentObservationsOfSpeciesInRegion: "data/obs/%s/recent/%s",
ChecklistFeedOnDate: "product/lists/%s/%d/%d/%d",
RegionalStatisticsOnDate: "product/stats/%s/%d/%d/%d",
SpeciesListForRegion: "product/spplist/%s",
Top100: "product/top100/%s/%d/%d/%d",
ViewChecklist: "product/checklist/view/%s",
AdjacentRegions: "ref/adjacent/%s",
HotspotInfo: "ref/hotspot/info/%s",
HotspotsInRegion: "ref/hotspot/%s",
NearbyHotspots: "ref/hotspot/geo",
EbirdTaxonomy: "ref/taxonomy/ebird",
TaxaLocaleCodes: "ref/taxa-locales/ebird",
TaxonomicForms: "ref/taxon/forms/%s",
TaxonomicGroups: "ref/sppgroup/%s",
TaxonomyVersions: "ref/taxonomy/versions",
RegionInfo: "ref/region/info/%s",
SubRegionInfo: "ref/region/list/%s/%s",
}
type Client struct {
apikey string
baseURL *url.URL
httpClient *http.Client
acceptLanguage string
}
func WithAcceptLanguage(lang string) ClientOption {
return func(client *Client) {
client.acceptLanguage = lang
}
}
func WithBaseURL(urlStr string) ClientOption {
return func(client *Client) {
parsedURL, err := url.Parse(urlStr)
if err != nil {
panic(err)
}
client.baseURL = parsedURL
}
}
func WithHTTPClient(httpClient *http.Client) ClientOption {
return func(client *Client) {
client.httpClient = httpClient
}
}
type Error struct {
Message string `json:"message"`
Status int `json:"status"`
}
func (e Error) Error() string {
return fmt.Sprintf("eBird API error (status %d): %s", e.Status, e.Message)
}
func NewClient(key string, opts ...ClientOption) (*Client, error) {
if key == "" {
return nil, errors.New("eBird API key is missing. Please set the 'EBIRD_API_KEY' environment variable")
}
c := &Client{
apikey: key,
httpClient: &http.Client{
Timeout: defaultTimeout,
},
}
baseURL, err := url.ParseRequestURI(APIEndpointBase)
if err != nil {
return nil, fmt.Errorf("invalid base URL: %w", err)
}
c.baseURL = baseURL
for _, opt := range opts {
opt(c)
}
return c, nil
}
func (c *Client) get(ctx context.Context, endpoint string, params url.Values, result interface{}) error {
u, err := url.Parse(endpoint)
if err != nil {
return fmt.Errorf("invalid endpoint URL: %w", err)
}
u = c.baseURL.ResolveReference(u)
u.RawQuery = params.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
if c.acceptLanguage != "" {
req.Header.Set("Accept-Language", c.acceptLanguage)
}
req.Header.Set("X-eBirdApiToken", c.apikey)
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNoContent {
return nil
}
if resp.StatusCode != http.StatusOK {
return c.decodeError(resp)
}
if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
return fmt.Errorf("failed to decode response: %w", err)
}
return nil
}
func (c *Client) decodeError(resp *http.Response) error {
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read error response: %w", err)
}
if len(body) == 0 {
return fmt.Errorf("HTTP %d: %s (body empty)", resp.StatusCode, http.StatusText(resp.StatusCode))
}
var apiError struct {
Error Error `json:"error"`
}
if err := json.Unmarshal(body, &apiError); err != nil {
return fmt.Errorf("failed to decode error response: %w", err)
}
if apiError.Error.Message == "" {
apiError.Error.Message = fmt.Sprintf("unexpected HTTP %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
apiError.Error.Status = resp.StatusCode
return apiError.Error
}
func convertToJsonFormat(url string) string {
if !strings.Contains(url, "?") && !strings.Contains(url, "fmt=json") {
url += "?fmt=json"
}
if strings.Contains(url, "?") && !strings.Contains(url, "fmt=json") {
url += "&fmt=json"
}
url = strings.Replace(url, "fmt=csv", "fmt=json", 1)
return url
}