-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.go
114 lines (96 loc) · 2.72 KB
/
search.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
package gobgg
import (
"context"
"encoding/xml"
"fmt"
"net/http"
"strings"
)
// searchItems is the result of the search in xmlapi2 bgg
type searchItems struct {
XMLName xml.Name `xml:"items"`
Text string `xml:",chardata"`
Total string `xml:"total,attr"`
Termsofuse string `xml:"termsofuse,attr"`
Item []struct {
Text string `xml:",chardata"`
Type string `xml:"type,attr"`
ID int64 `xml:"id,attr"`
Name []NameStruct `xml:"name"`
YearPublished struct {
Text string `xml:",chardata"`
Value string `xml:"value,attr"`
} `xml:"yearpublished"`
} `xml:"item"`
}
// SearchResult is the result for the search
type SearchResult struct {
ID int64
Name string
AlternateNames []string
Type ItemType
YearPublished int // Zero means no data
}
const searchPath = "xmlapi2/search"
// SearchOption is used to handle func option ins earch api
type SearchOption struct {
types []string
exact bool
}
// SearchOptionSetter is used to handle the func option in search api
type SearchOptionSetter func(*SearchOption)
// SearchExact set the exact argument for bgg
func SearchExact() SearchOptionSetter {
return func(opt *SearchOption) {
opt.exact = true
}
}
// SearchTypes set the valid types for the api
func SearchTypes(types ...ItemType) SearchOptionSetter {
return func(opt *SearchOption) {
opt.types = make([]string, len(types))
for i := range types {
opt.types[i] = string(types[i])
}
}
}
// Search using search api of the bgg, it get the list of requested items
func (bgg *BGG) Search(ctx context.Context, query string, setter ...SearchOptionSetter) ([]SearchResult, error) {
opt := SearchOption{}
for i := range setter {
setter[i](&opt)
}
args := map[string]string{
"query": query,
}
if opt.exact {
args["exact"] = "1"
}
if len(opt.types) > 0 {
args["type"] = strings.Join(opt.types, ",")
}
u := bgg.buildURL(searchPath, args)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, fmt.Errorf("create request failed: %w", err)
}
resp, err := bgg.do(req)
if err != nil {
return nil, fmt.Errorf("http call failed: %w", err)
}
defer resp.Body.Close()
var result searchItems
if err = decode(resp.Body, &result); err != nil {
return nil, fmt.Errorf("XML decoding failed: %w", err)
}
ret := make([]SearchResult, len(result.Item))
for i := range result.Item {
ret[i] = SearchResult{
ID: result.Item[i].ID,
Type: ItemType(result.Item[i].Type),
YearPublished: int(safeInt(result.Item[i].YearPublished.Value)),
}
ret[i].Name, ret[i].AlternateNames = nameStructToString(result.Item[i].Name)
}
return ret, nil
}