This repository has been archived by the owner on Dec 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch.go
111 lines (90 loc) · 2.99 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
package xivapi
import (
"encoding/json"
"fmt"
"net/url"
"time"
"github.com/google/go-querystring/query"
)
// DefaultResultsPerPage is the default amount of results to return per page
const DefaultResultsPerPage int = 100
// SearchParams is the struct that gets encoded to the list of uri params
type SearchParams struct {
GlobalQueryParameters
Indexes SearchIndexes `url:"indexes"`
Query string `url:"string"`
Algorithm SearchAlgo `url:"string_algo,omitempty"`
SearchColumn string `url:"string_column,omitempty"`
Page int `url:"page,omitempty"`
SortColumn string `url:"sort_field,omitempty"`
SortOrder SortOrder `url:"sort_order,omitempty"`
Limit int `url:"limit"`
Filters SearchFilters `url:"filters"`
}
// SearchResult is the result returned from XIVAPI
type SearchResult struct {
Pagination Pagination
Results []SearchResultEntry `json:"results"`
Milliseconds int64 `json:"SpeedMs"`
}
func (sr SearchResult) String() string {
return fmt.Sprintf("SearchResult{ %v %v }", sr.Pagination, sr.Results)
}
// Duration converts the Milliseconds into a time.Duraction
func (sr *SearchResult) Duration() time.Duration {
return time.Duration(sr.Milliseconds) * time.Millisecond
}
// SearchResultEntry is the result of a search result.
// It holds the 2 common fields to all results and allows converting into the correct type
type SearchResultEntry struct {
*SearchResultCommon
raw json.RawMessage
}
// UnmarshalJSON is called by encoding/json for a custom way to unmarshal the json object
func (e *SearchResultEntry) UnmarshalJSON(bs []byte) error {
var decoded SearchResultCommon
if err := json.Unmarshal(bs, &decoded); err != nil {
return err
}
e.SearchResultCommon = &decoded
e.raw = bs
return nil
}
func (e SearchResultEntry) String() string {
return fmt.Sprintf("%v { %v }", e.Type, e.Name)
}
// Search prepares a simple search request
// This uses SearchRaw under the hood
func (c *XIVAPI) Search(query, searchColumn, sortColumn string, sortOrder SortOrder, page int, limit int, filters SearchFilters, indexes ...SearchIndex) (*SearchResult, error) {
params := SearchParams{
Query: query,
SearchColumn: searchColumn,
SortColumn: sortColumn,
SortOrder: sortOrder,
Limit: limit,
Page: page,
Filters: filters,
Indexes: SearchIndexes(indexes),
}
if limit <= 0 {
params.Limit = DefaultResultsPerPage
}
return c.SearchRaw(params)
}
// SearchRaw accepts a raw SearchParams argument and executes the search
func (c *XIVAPI) SearchRaw(params SearchParams) (*SearchResult, error) {
// xivapi uses 1-based pagination
// lib uses 0-based indexing
params.Page++
qs, err := query.Values(params)
if err != nil {
return nil, err
}
uri, _ := url.Parse(fmt.Sprintf("%v%v", BaseURL, "/Search"))
uri.RawQuery = qs.Encode()
r := new(SearchResult)
if err := c.RequestJSON(MethodGet, uri, nil, r); err != nil {
return nil, err
}
return r, nil
}