-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregion.go
64 lines (52 loc) · 1.68 KB
/
region.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
package ebird
import (
"context"
"fmt"
)
type Bounds struct {
MinX float64 `json:"minX,omitempty"`
MaxX float64 `json:"maxX,omitempty"`
MinY float64 `json:"minY,omitempty"`
MaxY float64 `json:"maxY,omitempty"`
}
type RegionInfo struct {
Bounds Bounds `json:"bounds"`
Result string `json:"result,omitempty"`
Code string `json:"code,omitempty"`
Type string `json:"type,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
}
type SubRegion struct {
Code string `json:"code,omitempty"`
Name string `json:"name,omitempty"`
}
func (c *Client) RegionInfo(ctx context.Context, regionCode string, opts ...RequestOption) (*RegionInfo, error) {
if regionCode == "" {
return nil, fmt.Errorf("regionCode cannot be empty")
}
endpoint := fmt.Sprintf(APIEndpoints.RegionInfo, regionCode)
params := processOptions(opts...)
var info RegionInfo
err := c.get(ctx, endpoint, params.URLParams, &info)
if err != nil {
return nil, fmt.Errorf("failed to get region info: %w", err)
}
return &info, nil
}
func (c *Client) SubRegionList(ctx context.Context, regionType, parentRegionCode string, opts ...RequestOption) ([]SubRegion, error) {
if regionType == "" {
return nil, fmt.Errorf("regionType cannot be empty")
}
if parentRegionCode == "" {
return nil, fmt.Errorf("parentRegionCode cannot be empty")
}
endpoint := fmt.Sprintf(APIEndpoints.SubRegionInfo, regionType, parentRegionCode)
params := processOptions(opts...)
var subRegions []SubRegion
err := c.get(ctx, endpoint, params.URLParams, &subRegions)
if err != nil {
return nil, fmt.Errorf("failed to get subregion list: %w", err)
}
return subRegions, nil
}