-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_id.go
65 lines (54 loc) · 1.4 KB
/
convert_id.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
package brewerydb
import (
"strconv"
"strings"
)
// ConvertIDService provides access to the BreweryDB ID Conversion API.
// Use Client.ConvertID.
//
// See: http://www.brewerydb.com/developers/docs-endpoint/convertid_index
type ConvertIDService struct {
c *Client
}
// ConvertType is the type of ID to convert.
type ConvertType string
// Types of things that have IDs that can be converted.
const (
ConvertBrewery ConvertType = "brewery" // ConvertBrewery converts Brewery IDs.
ConvertBeer = "beer" // ConvertBeer converts Beer IDs.
)
// ConvertIDs converts a series of "old" Beer or Brewery IDs to the "new" format
// (BreweryDB v1 to v2)
//
// See: http://www.brewerydb.com/developers/docs-endpoint/convertid_index#1
func (cs *ConvertIDService) ConvertIDs(t ConvertType, oldIDs ...int) (map[int]string, error) {
// POST: /convertid
var ids []string
for _, id := range oldIDs {
ids = append(ids, strconv.Itoa(id))
}
q := struct {
Type string `url:"type"`
IDs string `url:"ids"`
}{string(t), strings.Join(ids, ",")}
req, err := cs.c.NewRequest("POST", "/convertid", &q)
if err != nil {
return nil, err
}
resp := struct {
Status string
Data []struct {
OldID int
NewID string
}
Message string
}{}
if err := cs.c.Do(req, &resp); err != nil {
return nil, err
}
m := make(map[int]string)
for _, d := range resp.Data {
m[d.OldID] = d.NewID
}
return m, nil
}