-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
73 lines (66 loc) · 1.58 KB
/
api.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
package azrp
import (
"encoding/json"
"fmt"
"net/http"
)
type apiGetter func(url string) (ApiResponse, error)
// apiGet is used to get a single page or results from the retail price API
func apiGet(url string) (ApiResponse, error) {
resp, err := http.Get(url)
if err != nil {
return ApiResponse{}, nil
}
defer resp.Body.Close()
ar := ApiResponse{}
jdec := json.NewDecoder(resp.Body)
err = jdec.Decode(&ar)
if err != nil {
return ApiResponse{}, err
}
return ar, nil
}
// apiGetAll is another version of the apiGetter function. This version checks
// the version returns all results by checking the NextResultLink field and
// keeps fetching items until the list is exhausted.
func apiGetAll(url string) (ApiResponse, error) {
fmt.Println("Getting", url)
r1, err := http.Get(url)
if err != nil {
return ApiResponse{}, err
}
defer r1.Body.Close()
ar := ApiResponse{}
jdec := json.NewDecoder(r1.Body)
err = jdec.Decode(&ar)
if err != nil {
return ApiResponse{}, err
}
next := ar.NextPageLink
for {
if next == "" {
break
} else {
fmt.Println("Getting", next)
r2, err := http.Get(next)
if err != nil {
return ApiResponse{}, err
}
defer r2.Body.Close()
arTmp := ApiResponse{}
jdec := json.NewDecoder(r2.Body)
err = jdec.Decode(&arTmp)
if err != nil {
return ApiResponse{}, nil
}
//Now copy the items to the original response
ar.Items = append(ar.Items, arTmp.Items...)
//Update next link
next = arTmp.NextPageLink
}
}
/* fixes unexpected values */
ar.Count = uint(len(ar.Items))
ar.NextPageLink = ""
return ar, nil
}