-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.go
56 lines (45 loc) · 1.02 KB
/
extract.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
package extract
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"time"
)
// Cities struct is data structure of result
type Cities struct {
Name string `json:"name"`
Country string `json:"country"`
Slug string `json:"slug"`
}
// func main() {
// url_city := "https://myfave.com/api/mobile/cities"
// fmt.Println(extractCity(url_city))
// }
// GetCity is func to extract city
func GetCity(urlCity string) []string {
spaceClient := http.Client{
Timeout: time.Second * 2, // Maximum of 2 secs
}
req, err := http.NewRequest(http.MethodGet, urlCity, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("User-Agent", "fave-testcoding")
res, getErr := spaceClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
var cities []Cities
var cityList []string
json.Unmarshal([]byte(body), &cities)
for _, city := range cities {
cityList = append(cityList, city.Slug)
}
// fmt.Println(cityList)
return cityList
}