-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.go
118 lines (89 loc) · 2.52 KB
/
inventory.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
115
116
117
118
package main
import (
"encoding/json"
"io"
"net/http"
"strconv"
"github.com/ellielle/bootdev-buddy/internal/bootdevapi"
)
const SALMON_URL = "https://api.boot.dev/v1/user_baked_salmon"
const STONES_URL = "https://api.boot.dev/v1/user_seer_stones"
const XP_URL = "https://api.boot.dev/v1/user_xp_potion"
const FLAME_URL = "https://api.boot.dev/v1/user_frozen_flame"
// SeerStones gets the user's current amount of Seer Stones
func (a *App) SeerStones() (int, error) {
req, err := http.NewRequest("GET", STONES_URL, nil)
if err != nil {
return 0, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+a.tokens.AccessToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
stones, err := io.ReadAll(resp.Body)
if err != nil {
return 0, err
}
num, err := strconv.Atoi(string(stones))
return num, err
}
func (a *App) BakedSalmon() (int, error) {
req, err := http.NewRequest("GET", SALMON_URL, nil)
if err != nil {
return 0, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+a.tokens.AccessToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
salmon, err := io.ReadAll(resp.Body)
if err != nil {
return 0, err
}
num, err := strconv.Atoi(string(salmon))
return num, err
}
func (a *App) XPPotion() (bootdevapi.PotionList, error) {
var potions bootdevapi.PotionList
req, err := http.NewRequest("GET", XP_URL, nil)
if err != nil {
return bootdevapi.PotionList{}, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+a.tokens.AccessToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return bootdevapi.PotionList{}, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&potions)
if err != nil {
return bootdevapi.PotionList{}, err
}
return potions, nil
}
func (a *App) FrozenFlames() (int, error) {
req, err := http.NewRequest("GET", FLAME_URL, nil)
if err != nil {
return 0, err
}
var frozenFlames []bootdevapi.FrozenFlame
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+a.tokens.AccessToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&frozenFlames)
// only need the length of the FrozenFlames slice
return len(frozenFlames), err
}