-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffxivapi.go
51 lines (44 loc) · 1.32 KB
/
ffxivapi.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
package ffxivapi // import "roob.re/ffxivapi"
import (
"github.com/PuerkitoBio/goquery"
log "github.com/sirupsen/logrus"
"net/http"
"net/url"
"roob.re/ffxivapi/lodestone"
"strconv"
)
// FFXIVAPI is the main object, containing the region to be targeted and the HTTP client to use
type FFXIVAPI struct {
Lodestone lodestone.Client
}
// New returns a new FFXIVAPI object with http.DefaultClient and the region set to Europe ("eu")
func New() *FFXIVAPI {
return &FFXIVAPI{
Lodestone: &lodestone.HTTPClient{
Server: lodestone.CanonServerFromRegion("eu"),
HTTPClient: http.DefaultClient,
},
}
}
// lodestone queries the given lodestone URL and params (url-encoding them) and returns a goquery document
func (api *FFXIVAPI) lodestone(query string, params map[string]string) (*goquery.Document, error) {
if len(params) > 0 {
query += "?"
urlValues := url.Values{}
for k, v := range params {
urlValues.Add(k, v)
}
query += urlValues.Encode()
}
log.Debugf("lodestone: requesting %s", query)
response, err := api.Lodestone.Request(query)
if err != nil {
return nil, err
}
return goquery.NewDocumentFromReader(response)
}
// silentAtoi discards error from atoi, used to assign numbers assumed to be correctly-formatted into inline initializers
func silentAtoi(s string) int {
i, _ := strconv.Atoi(s)
return i
}