-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapCmds.go
48 lines (42 loc) · 959 Bytes
/
mapCmds.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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
)
const baseUrl = "https://pokeapi.co/api/v2/location-area/"
var mapResponse MapResponse
func MapCmd(areaUrl string) {
if areaUrl == "next" {
if mapResponse.Next != nil {
areaUrl = *mapResponse.Next
} else {
areaUrl = "https://pokeapi.co/api/v2/location-area/"
}
} else if areaUrl == "prev" {
areaUrl = *mapResponse.Previous
} else {
areaUrl = "https://pokeapi.co/api/v2/location-area/"
}
res, err := http.Get(areaUrl)
if err != nil {
log.Fatal(err)
}
body, err := io.ReadAll(res.Body)
res.Body.Close()
if res.StatusCode > 299 {
log.Fatalf("Response failed with status code: %d and\nbody: %s\n", res.StatusCode, body)
}
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(body, &mapResponse)
if err != nil {
log.Fatalf("Failed to unmarshal JSON: %v", err)
}
for _, location := range mapResponse.Results {
fmt.Println(location.Name)
}
}