-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstash_tabs.go
67 lines (58 loc) · 1.48 KB
/
stash_tabs.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
package poeapi
import (
"encoding/json"
"fmt"
"net/url"
)
// GetStashOptions contains the request parameters for the stash endpoint.
type GetStashOptions struct {
// ID is the unique change ID containing a set of stashes. If ID is omitted,
// the API will return the oldest stash tab possible.
ID string
}
func (opts GetStashOptions) toQueryParams() string {
u := url.Values{}
if opts.ID != "" {
u.Add("id", opts.ID)
}
return u.Encode()
}
func (c *client) GetStashes(opts GetStashOptions) (StashResponse, error) {
url := fmt.Sprintf("%s?%s", c.formatURL(stashTabsEndpoint),
opts.toQueryParams())
resp, err := c.get(url)
if err != nil {
return StashResponse{}, err
}
return parseStashResponse(resp)
}
func parseStashResponse(resp string) (StashResponse, error) {
var s StashResponse
if err := json.Unmarshal([]byte(resp), &s); err != nil {
return StashResponse{}, err
}
return s, nil
}
type latestChange struct {
ID string `json:"next_change_id"`
}
func (c *client) GetLatestStashID() (string, error) {
var url string
if c.useSSL {
url = fmt.Sprintf("https://%s/api/Data/GetStats", c.ninjaHost)
} else {
url = fmt.Sprintf("http://%s/api/Data/GetStats", c.ninjaHost)
}
resp, err := c.get(url)
if err != nil {
return "", err
}
return parseLatestChangeResponse(resp)
}
func parseLatestChangeResponse(resp string) (string, error) {
var latest latestChange
if err := json.Unmarshal([]byte(resp), &latest); err != nil {
return "", err
}
return latest.ID, nil
}