-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotspot.go
100 lines (83 loc) · 3.42 KB
/
hotspot.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
package ebird
import (
"context"
"fmt"
)
type HotspotInfo struct {
LocId string `json:"locId,omitempty"`
Name string `json:"name,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
CountryCode string `json:"countryCode,omitempty"`
CountryName string `json:"countryName,omitempty"`
Subnational1Name string `json:"subnational1Name,omitempty"`
Subnational1Code string `json:"subnational1Code,omitempty"`
Subnational2Code string `json:"subnational2Code,omitempty"`
Subnational2Name string `json:"subnational2Name,omitempty"`
IsHotspot bool `json:"isHotspot,omitempty"`
LocID string `json:"locID,omitempty"`
LocName string `json:"locName,omitempty"`
Lat float64 `json:"lat,omitempty"`
Lng float64 `json:"lng,omitempty"`
HierarchicalName string `json:"hierarchicalName,omitempty"`
}
type NearbyHotspot struct {
LocId string `json:"locId,omitempty"`
LocName string `json:"locName,omitempty"`
CountryCode string `json:"countryCode,omitempty"`
Subnational1Code string `json:"subnational1Code,omitempty"`
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
LatestObsDt string `json:"latestObsDt,omitempty"`
NumSpeciesAllTime int `json:"numSpeciesAllTime,omitempty"`
}
type HotspotInRegion struct {
LocId string `json:"locId,omitempty"`
LocName string `json:"locName,omitempty"`
CountryCode string `json:"countryCode,omitempty"`
Subnational1Code string `json:"subnational1Code,omitempty"`
Subnational2Code string `json:"subnational2Code,omitempty"`
Lat float64 `json:"lat,omitempty"`
Lng float64 `json:"lng,omitempty"`
LatestObsDt string `json:"latestObsDt,omitempty"`
NumSpeciesAllTime int `json:"numSpeciesAllTime,omitempty"`
}
func (c *Client) HotspotsInRegion(ctx context.Context, regionCode string, opts ...RequestOption) ([]HotspotInRegion, error) {
if regionCode == "" {
return nil, fmt.Errorf("regionCode cannot be empty")
}
endpoint := fmt.Sprintf(APIEndpoints.HotspotsInRegion, regionCode)
params := processOptions(opts...)
var hotspots []HotspotInRegion
err := c.get(ctx, endpoint, params.URLParams, &hotspots)
if err != nil {
return nil, fmt.Errorf("failed to get hotspots in region: %w", err)
}
return hotspots, nil
}
func (c *Client) NearbyHotspots(ctx context.Context, opts ...RequestOption) ([]NearbyHotspot, error) {
params := processOptions(opts...)
params.URLParams.Set("fmt", "json")
if params.URLParams.Get("lat") == "" || params.URLParams.Get("lng") == "" {
return nil, fmt.Errorf("must provide Lat and Lng parameters")
}
var hotspots []NearbyHotspot
err := c.get(ctx, APIEndpoints.NearbyHotspots, params.URLParams, &hotspots)
if err != nil {
return nil, fmt.Errorf("failed to get nearby hotspots: %w", err)
}
return hotspots, nil
}
func (c *Client) HotspotInfo(ctx context.Context, locId string, opts ...RequestOption) (*HotspotInfo, error) {
if locId == "" {
return nil, fmt.Errorf("locId cannot be empty")
}
endpoint := fmt.Sprintf(APIEndpoints.HotspotInfo, locId)
params := processOptions(opts...)
var hotspot HotspotInfo
err := c.get(ctx, endpoint, params.URLParams, &hotspot)
if err != nil {
return nil, fmt.Errorf("failed to get hotspot info: %w", err)
}
return &hotspot, nil
}