-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
178 lines (139 loc) · 4.13 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
package internal
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
"github.com/entrustcorporation/dv/providers/internal/errutils"
)
const defaultBaseURL = "https://api.dynect.net/REST"
// Client the Dyn API client.
type Client struct {
customerName string
username string
password string
baseURL *url.URL
HTTPClient *http.Client
}
// NewClient Creates a new Client.
func NewClient(customerName string, username string, password string) *Client {
baseURL, _ := url.Parse(defaultBaseURL)
return &Client{
customerName: customerName,
username: username,
password: password,
baseURL: baseURL,
HTTPClient: &http.Client{Timeout: 5 * time.Second},
}
}
// Publish updating Zone settings.
// https://help.dyn.com/update-zone-api/
func (c *Client) Publish(ctx context.Context, zone, notes string) error {
endpoint := c.baseURL.JoinPath("Zone", zone)
payload := &publish{Publish: true, Notes: notes}
req, err := newJSONRequest(ctx, http.MethodPut, endpoint, payload)
if err != nil {
return err
}
_, err = c.do(req)
if err != nil {
return err
}
return nil
}
// AddTXTRecord creating TXT Records.
// https://help.dyn.com/create-txt-record-api/
func (c *Client) AddTXTRecord(ctx context.Context, authZone, fqdn, value string, ttl int) error {
endpoint := c.baseURL.JoinPath("TXTRecord", authZone, fqdn)
payload := map[string]any{
"rdata": map[string]string{
"txtdata": value,
},
"ttl": strconv.Itoa(ttl),
}
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, payload)
if err != nil {
return err
}
_, err = c.do(req)
if err != nil {
return err
}
return nil
}
// RemoveTXTRecord deleting one or all existing TXT Records.
// https://help.dyn.com/delete-txt-records-api/
func (c *Client) RemoveTXTRecord(ctx context.Context, authZone, fqdn string) error {
endpoint := c.baseURL.JoinPath("TXTRecord", authZone, fqdn)
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
if err != nil {
return err
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
return errutils.NewHTTPDoError(req, err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
}
return nil
}
func (c *Client) do(req *http.Request) (*APIResponse, error) {
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, errutils.NewHTTPDoError(req, err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= http.StatusInternalServerError {
return nil, errutils.NewUnexpectedResponseStatusCodeError(req, resp)
}
raw, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errutils.NewReadResponseError(req, resp.StatusCode, err)
}
var response APIResponse
err = json.Unmarshal(raw, &response)
if err != nil {
return nil, errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
}
if resp.StatusCode >= http.StatusBadRequest {
return nil, fmt.Errorf("%s: %w", response.Messages, errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw))
}
if resp.StatusCode == http.StatusTemporaryRedirect {
// TODO add support for HTTP 307 response and long running jobs
return nil, errors.New("API request returned HTTP 307. This is currently unsupported")
}
if response.Status == "failure" {
return nil, fmt.Errorf("%s: %w", response.Messages, errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw))
}
return &response, nil
}
func newJSONRequest(ctx context.Context, method string, endpoint *url.URL, payload any) (*http.Request, error) {
buf := new(bytes.Buffer)
if payload != nil {
err := json.NewEncoder(buf).Encode(payload)
if err != nil {
return nil, fmt.Errorf("failed to create request JSON body: %w", err)
}
}
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Accept", "application/json")
if payload != nil {
req.Header.Set("Content-Type", "application/json")
}
tok := getToken(req.Context())
if tok != "" {
req.Header.Set(authTokenHeader, tok)
}
return req, nil
}