-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdogs.go
More file actions
89 lines (75 loc) · 1.8 KB
/
dogs.go
File metadata and controls
89 lines (75 loc) · 1.8 KB
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
type Life struct {
Max int `json:"max"`
Min int `json:"min"`
}
type Weight struct {
Max int `json:"max"`
Min int `json:"min"`
}
type Attributes struct {
Name string `json:"name"`
Description string `json:"description"`
Life Life `json:"life"`
MaleWeight Weight `json:"male_weight"`
FemaleWeight Weight `json:"female_weight"`
Hypoallergenic bool `json:"hypoallergenic"`
}
type GroupData struct {
ID string `json:"id"`
Type string `json:"type"`
}
type Relationships struct {
Group struct {
Data GroupData `json:"data"`
} `json:"group"`
}
type Breed struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes Attributes `json:"attributes"`
Relationships Relationships `json:"relationships"`
}
type ApiResponse struct {
Data []Breed `json:"data"`
Links struct {
Self string `json:"self"`
Current string `json:"current"`
Next string `json:"next"`
Last string `json:"last"`
} `json:"links"`
}
var Breeds []Breed
// init Breeds from dogapi.dog
func initDogBreeds() {
resp, err := http.Get("https://dogapi.dog/api/v2/breeds")
if err != nil {
fmt.Printf("Failed to fetch dog Breeds")
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("Failed to retrieve dog Breeds")
return
}
var apiResponse ApiResponse
err = json.NewDecoder(resp.Body).Decode(&apiResponse)
if err != nil {
fmt.Printf("Failed to unmarshal dog Breeds")
return
}
Breeds = apiResponse.Data
}
func getDogBreeds(context *gin.Context) {
var breedNames []string
for _, breed := range Breeds {
breedNames = append(breedNames, breed.Attributes.Name)
}
context.IndentedJSON(http.StatusOK, breedNames)
}