-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokemon.go
245 lines (222 loc) · 8.12 KB
/
pokemon.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Package pokemon provides structs and functions for acccessing
// http://pokeapi.co
package pokemon
import (
"encoding/json"
"net/http"
)
const (
endpoint = "http://pokeapi.co/api/v1"
)
type Game struct {
Name string `json:"name"`
Id int `json:"id"`
Resource_uri string `json:"resource_uri"`
Created string `json:"created"`
Modified string `json:"modified"`
Release_year int `json:"release"year"`
Generation int `json:"generation"`
}
type Ability struct {
Name string `json:"name"`
Id int `json:"id"`
Resource_uri string `json:"resource_uri"`
Created string `json:"created"`
Modified string `json:"modified"`
Description string `json:"description"`
}
type Description struct {
Name string `json:"name"`
Id int `json:"id"`
Resource_uri string `json:"resource_uri"`
Created string `json:"created"`
Modified string `json:"modified"`
Games []Game `json:"games"`
Pokemon Pokemon `json:"pokemon"`
}
type EggGroup struct {
Name string `json:"name"`
Id int `json:"id"`
Resource_uri string `json:"resource_uri"`
Created string `json:"created"`
Modified string `json:"modified"`
Pokemon []Pokemon `json:"pokemon"`
}
type Evolution struct {
Level int `json:"level"`
Method string `json:"method"`
Resouce_uri string `json:"resource_uri"`
To string `json:"to"`
}
type Pokemon_Move struct {
Learn_type string `json:"learn_type"`
Name string `json:"name"`
Resource_uri string `json:"resource_uri"`
Level int `json:"level"`
}
type Move struct {
Name string `json:"name"`
Id int `json:"id"`
Resource_uri string `json:"resource_uri"`
Created string `json:"created"`
Modified string `json:"modified"`
Description string `json:"description"`
Power int `json:"power"`
Accuracy int `json:"accuracy"`
Category string `json:"category"`
Pp int `json:"pp"`
}
type Sprite struct {
Name string `json:"name"`
Id int `json:"id"`
Resouce_uri string `json:"resource_uri"`
Created string `json:"created"`
Modified string `json:"modified"`
Pokemon Pokemon `json:"pokemon"`
Image string `json:"image"`
}
type Type struct {
Name string `json:"name"`
Id int `json:"id"`
Resource_uri string `json:"resource_uri"`
Created string `json:created"`
Modified string `json:"modified"`
Ineffective []Type `json:"ineffective"`
No_effect []Type `json:"no_effect"`
Resistance []Type `json:"resistance"`
Super_effective []Type `json:"super_effective"`
Weakness []Type `json:"weakness"`
}
type Pokemon struct {
Name string `json:"name"`
National_id int `json:"national_id"`
Resource_uri string `json:"resource_uri"`
Created string `json:"created"`
Modified string `json:"modified"`
Abilites []Ability `json:"abilities"`
Egg_groups []EggGroup `json:"egg_groups"`
Evolutions []Evolution `json:"evolutions"`
Descriptions []Description `json:"descriptions"`
Moves []Pokemon_Move `json:"moves"`
Types []Type `json:"types"`
Catch_rate int `json:"catch_rate"`
Species string `json:"species"`
Hp int `json:"hp"`
Attack int `json:"attack"`
Defense int `json:"defense"`
Sp_atk int `json:"sp_atk"`
Sp_def int `json:"sp_def"`
Speed int `json:"speed"`
Egg_cycles int `json:"egg_cycles"`
Ev_yield string `json:"ev_yield"`
Exp int `json:"exp"`
Growth_rate string `json:"growth_rate"`
Happiness int `json:"happiness"`
Height string `json:"height"`
Male_female_ratio string `json:"male_female_ratio"`
Pkdx_id int `json:"pkdx_id"`
Sprites []Sprite `json:"sprites"`
Total int `json:"total"`
Weight string `json:"weight"`
}
type Pokedex struct {
Name string `json:"name"`
Resource_uri string `json:"resource_uri"`
Created string `json:"created"`
Modified string `json:"modified"`
Pokemon []Pokemon `json:"pokemon"`
}
// This function gets the JSON from the API and populates the value field
// which is passed by reference to it
func endpointRequest(url string, value interface{}) error {
res, err := http.Get(url)
if err != nil {
return err
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
return decoder.Decode(&value)
}
// Returns a Pokedex object according to the input identifier. If an error
// occurs, returns an empty Pokedex object and a corresponding error object
// This is a special call and will only work for the identifier "1"
func GetPokedex(identifier string) (pokedex Pokedex, err error) {
url := endpoint + "/pokedex/" + identifier
if err = endpointRequest(url, &pokedex); err != nil {
return Pokedex{}, err
}
return pokedex, nil
}
// Returns a Pokemon object according to the input identifier. If an error
// occurs, returns an empty Pokemon object and a corresponding error object
// The identifier can be either the Pokemon ID or Name
func GetPokemon(identifier string) (pokemon Pokemon, err error) {
url := endpoint + "/pokemon/" + identifier
if err = endpointRequest(url, &pokemon); err != nil {
return Pokemon{}, err
}
return pokemon, nil
}
// Returns a Game object according to the input identifier. If an error
// occurs, returns an empty Game object and a corresponding error object
func GetGame(identifier string) (game Game, err error) {
url := endpoint + "/game/" + identifier
if err = endpointRequest(url, &game); err != nil {
return Game{}, err
}
return game, nil
}
// Returns a Type object according to the input identifier. If an error
// occurs, returns an empty Type object and a corresponding error object
func GetType(identifier string) (type_ Type, err error) {
url := endpoint + "/type/" + identifier
if err = endpointRequest(url, &type_); err != nil {
return Type{}, err
}
return type_, nil
}
// Returns a Move object according to the input identifier. If an error
// occurs, returns an empty Move object and a corresponding error object
func GetMove(identifier string) (move Move, err error) {
url := endpoint + "/move/" + identifier
if err = endpointRequest(url, &move); err != nil {
return Move{}, err
}
return move, nil
}
// Returns an Ability object according to the input identifier. If an error
// occurs, returns an empty Ability object and a corresponding error object
func GetAbility(identifier string) (ability Ability, err error) {
url := endpoint + "/ability/" + identifier
if err = endpointRequest(url, &ability); err != nil {
return Ability{}, err
}
return ability, nil
}
// Returns an EggGroup object according to the input identifier. If an error
// occurs, returns an empty EggGroup object and a corresponding error object
func GetEggGroup(identifier string) (eggGroup EggGroup, err error) {
url := endpoint + "/egg/" + identifier
if err = endpointRequest(url, &eggGroup); err != nil {
return EggGroup{}, err
}
return eggGroup, nil
}
// Returns a Description object according to the input identifier. If an error
// occurs, returns an empty Description object and a corresponding error object
func GetDescription(identifier string) (description Description, err error) {
url := endpoint + "/description/" + identifier
if err = endpointRequest(url, &description); err != nil {
return Description{}, err
}
return description, nil
}
// Returns a Sprite object according to the input identifier. If an error
// occurs, returns an empty Sprite object and a corresponding error object
func GetSprite(identifier string) (sprite Sprite, err error) {
url := endpoint + "/sprite/" + identifier
if err = endpointRequest(url, &sprite); err != nil {
return Sprite{}, err
}
return sprite, nil
}