-
Notifications
You must be signed in to change notification settings - Fork 0
/
berries.go
76 lines (59 loc) · 1.51 KB
/
berries.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
package pokego
import (
"encoding/json"
"fmt"
"log"
berriesCache "github.com/mazylol/pokego/cache/berries"
"github.com/mazylol/pokego/types/berries"
)
// GetBerry returns a berry by name.
func GetBerry(name string) (berries.Berry, error) {
berry, err := berriesCache.GetBerryFromCache(name)
if err != nil {
body, err := callApi(fmt.Sprintf("berry/%v", name))
if err != nil {
log.Fatal("Failed to call api")
}
err = json.Unmarshal(body, &berry)
if err == nil {
err = berriesCache.AddBerryToCache(berry)
}
return berry, err
} else {
return berry, err
}
}
// GetBerryFirmness returns a berry firmness by name.
func GetBerryFirmness(name string) (berries.Firmness, error) {
firmness, err := berriesCache.GetFirmnessFromCache(name)
if err != nil {
body, err := callApi(fmt.Sprintf("berry-firmness/%v", name))
if err != nil {
log.Fatal("Failed to call api")
}
err = json.Unmarshal(body, &firmness)
if err == nil {
err = berriesCache.AddFirmnessToCache(firmness)
}
return firmness, err
} else {
return firmness, err
}
}
// GetBerryFlavor returns a berry flavor by name.
func GetBerryFlavor(name string) (berries.Flavor, error) {
flavor, err := berriesCache.GetFlavorFromCache(name)
if err != nil {
body, err := callApi(fmt.Sprintf("berry-flavor/%v", name))
if err != nil {
log.Fatal("Failed to call api")
}
err = json.Unmarshal(body, &flavor)
if err == nil {
err = berriesCache.AddFlavorToCache(flavor)
}
return flavor, err
} else {
return flavor, err
}
}