-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocation.go
114 lines (91 loc) · 2.18 KB
/
location.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
package rickandmorty
import (
"strconv"
"github.com/mitchellh/mapstructure"
)
func GetLocations(options map[string]interface{}) (*AllLocations, error) {
endpoint := endpointLocation
hasParams := false
params := make(map[string]string)
if options == nil {
options = map[string]interface{}{
"endpoint": endpoint,
}
}
for k, v := range options {
switch v.(type) {
case int:
if k == "page" {
hasParams = true
params[k] = strconv.FormatInt(int64(v.(int)), 10)
}
delete(options, k)
case string:
// Skip endpoint in options
if k == "endpoint" {
continue
}
// Valid parameters to be passed to the parameters map
validParams := []string{"name", "status", "species", "type", "gender"}
exists := containsString(validParams, k)
if exists {
hasParams = true
params[k] = v.(string)
}
// Cleanup the options map
delete(options, k)
default:
// Cleanup the options map
delete(options, k)
// Set the endpoint
options["endpoint"] = endpoint
}
}
if hasParams {
options["endpoint"] = endpoint
options["params"] = params
}
data, err := makePetition(options)
if err != nil {
return &AllLocations{}, err
}
locations := new(AllLocations)
if err := mapstructure.Decode(data, &locations); err != nil {
return &AllLocations{}, err
}
return locations, nil
}
func GetLocation(integer int) (*Location, error) {
endpoint := endpointLocation
options := map[string]interface{}{
"endpoint": endpoint,
"params": map[string]int{
"integer": integer,
},
}
data, err := makePetition(options)
if err != nil {
return &Location{}, err
}
location := new(Location)
if err := mapstructure.Decode(data, &location); err != nil {
return &Location{}, err
}
return location, nil
}
func GetLocationsArray(integers []int) (*MultipleLocations, error) {
endpoint := endpointLocation
options := map[string]interface{}{
"endpoint": endpoint,
"integers": integers,
}
data, err := makePetition(options)
if err != nil {
return &MultipleLocations{}, err
}
locations := new(MultipleLocations)
if err := mapstructure.Decode(data, &locations); err != nil {
return &MultipleLocations{}, err
}
return locations, nil
}