-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
193 lines (147 loc) · 3.81 KB
/
client.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package hcdns
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
)
const baseURL = "https://dns.hetzner.com/api/v1"
var ErrStatus = errors.New("http status")
type Client struct {
inner *http.Client
bu string
token string
}
func NewClient(token string, opts ...Option) *Client {
client := &Client{
inner: http.DefaultClient,
bu: baseURL,
token: token,
}
for _, opt := range opts {
opt(client)
}
return client
}
func (c *Client) Zones(ctx context.Context) ([]Zone, error) {
return c.ZonesByKeyword(ctx, "")
}
func (c *Client) ZonesByKeyword(ctx context.Context, keyword string) ([]Zone, error) {
var (
zones []Zone
query url.Values
page uint = 1
)
if keyword != "" {
query = make(url.Values, 2) //nolint:mnd
query.Set("search_name", keyword)
} else {
query = make(url.Values, 1)
}
for {
query.Set("page", strconv.FormatUint(uint64(page), 10))
root, err := c.do(ctx, http.MethodGet, "zones", http.NoBody, query)
if err != nil {
return nil, fmt.Errorf("request: %w", err)
}
zones = append(zones, root.Zones...)
if root.Meta.Pagination.NextPage == page {
break
}
page = root.Meta.Pagination.NextPage
}
for i := range zones {
z := &zones[i]
z.c = c
}
return zones, nil
}
func (c *Client) Zone(ctx context.Context, id string) (*Zone, error) {
root, err := c.do(ctx, http.MethodGet, "zones/"+id, http.NoBody, nil)
if err != nil {
return nil, fmt.Errorf("request: %w", err)
}
root.Zone.c = c
return &root.Zone, nil
}
func (c *Client) ZoneByName(ctx context.Context, n string) (*Zone, error) {
q := make(url.Values, 1)
q.Set("name", n)
root, err := c.do(ctx, http.MethodGet, "zones", http.NoBody, q)
if err != nil {
return nil, fmt.Errorf("request: %w", err)
}
root.Zones[0].c = c
return &root.Zones[0], nil
}
func (c *Client) CreateZone(ctx context.Context, name string) (*Zone, error) {
return c.CreateZoneWithDefaultTTL(ctx, name, 0)
}
func (c *Client) CreateZoneWithDefaultTTL(ctx context.Context, name string, ttl time.Duration) (*Zone, error) {
payload := zoneReq{
Name: name,
TTL: uint64(ttl.Seconds()),
}
json, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("encode: %w", err)
}
root, err := c.do(ctx, http.MethodPost, "zones", bytes.NewBuffer(json), nil)
if err != nil {
return nil, fmt.Errorf("request: %w", err)
}
root.Zone.c = c
if err := root.Zone.createNSRecords(ctx); err != nil {
return nil, fmt.Errorf("create ns records: %w", err)
}
return &root.Zone, nil
}
func (c *Client) do(ctx context.Context, method, path string, body io.Reader, queryParams url.Values) (*root, error) {
u := fmt.Sprintf("%s/%s", c.bu, path)
req, err := http.NewRequestWithContext(ctx, method, u, body)
if err != nil {
return nil, fmt.Errorf("new request: %w", err)
}
req.Header.Set("Auth-Api-Token", c.token)
if body != http.NoBody {
req.Header.Set("Content-Type", "application/json")
}
if queryParams != nil {
req.URL.RawQuery = queryParams.Encode()
}
res, err := c.inner.Do(req)
if err != nil {
return nil, fmt.Errorf("do request: %w", err)
}
defer func() {
err = errors.Join(err, res.Body.Close())
}()
if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusCreated || res.StatusCode != http.StatusNotFound {
var root root
if err := json.NewDecoder(res.Body).Decode(&root); err != nil {
return nil, fmt.Errorf("decode: %w", err)
}
if root.Error.Code == http.StatusNotFound {
return nil, fmt.Errorf("%w: %s", ErrStatus, root.Error.Message)
}
return &root, nil
}
return nil, fmt.Errorf("%w: %s", ErrStatus, res.Status)
}
func WithClient(hc *http.Client) Option {
return func(c *Client) {
c.inner = hc
}
}
func WithBaseURL(u url.URL) Option {
return func(c *Client) {
c.bu = u.String()
}
}
type Option func(*Client)