-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistzones.go
84 lines (69 loc) · 1.62 KB
/
listzones.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
package cfdns
import (
"context"
"net/http"
"net/url"
"strconv"
"github.com/simplesurance/cfdns/log"
)
// Listzones lists zones on CloudFlare.
//
// API Reference: https://developers.cloudflare.com/api/operations/zones-get
func (c *Client) ListZones(
_ *ListZonesRequest,
) *Iterator[ListZonesResponseItem] {
page := 0
total := 0
read := 0
return &Iterator[ListZonesResponseItem]{
fetchNext: func(
ctx context.Context,
) ([]*ListZonesResponseItem, bool, error) {
page++
queryParams := url.Values{
"direction": {"asc"},
"per_page": {strconv.Itoa(itemsPerPage)},
}
if page != 0 {
queryParams["page"] = []string{strconv.Itoa(page)}
}
resp, err := sendRequestRetry[*listZoneAPIResponse](
ctx,
c,
c.logger.SubLogger(log.WithPrefix("ListZones"), log.WithInt("page", page)),
&request{
method: http.MethodGet,
path: "zones",
queryParams: queryParams,
body: nil,
})
if err != nil {
return nil, false, err
}
items := make([]*ListZonesResponseItem, len(resp.body.Result))
for i, v := range resp.body.Result {
items[i] = &ListZonesResponseItem{
ID: v.ID,
Name: v.Name,
}
}
total = resp.body.ResultInfo.TotalCount
read += len(resp.body.Result)
isLast := read >= total
return items, isLast, nil
},
}
}
type ListZonesRequest struct{}
type ListZonesResponseItem struct {
ID string
Name string
}
type listZoneAPIResponse struct {
cfResponseCommon
Result []listZoneAPIResponseItem `json:"result"`
}
type listZoneAPIResponseItem struct {
ID string `json:"id"`
Name string `json:"name"`
}