-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
234 lines (187 loc) · 6.21 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Package porkbun contains a client of the DNS API of Porkdun.
package porkbun
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
)
const defaultBaseURL = "https://api.porkbun.com/api/json/v3/"
const statusSuccess = "SUCCESS"
// DefaultTTL The minimum and the default is 300 seconds.
const DefaultTTL = "300"
// Client an API client for Porkdun.
type Client struct {
secretAPIKey string
apiKey string
BaseURL *url.URL
HTTPClient *http.Client
}
// New creates a new Client.
func New(secretAPIKey, apiKey string) *Client {
baseURL, _ := url.Parse(defaultBaseURL)
return &Client{
secretAPIKey: secretAPIKey,
apiKey: apiKey,
BaseURL: baseURL,
HTTPClient: &http.Client{Timeout: 10 * time.Second},
}
}
// Ping tests communication with the API.
func (c *Client) Ping(ctx context.Context) (string, error) {
endpoint := c.BaseURL.JoinPath("ping")
respBody, err := c.do(ctx, endpoint, nil)
if err != nil {
return "", err
}
pingResp := pingResponse{}
err = json.Unmarshal(respBody, &pingResp)
if err != nil {
return "", fmt.Errorf("failed to unmarshal response: %w", err)
}
if pingResp.Status.Status != statusSuccess {
return "", pingResp.Status
}
return pingResp.YourIP, nil
}
// CreateRecord creates a DNS record.
//
// name (optional): The subdomain for the record being created, not including the domain itself. Leave blank to create a record on the root domain. Use * to create a wildcard record.
// type: The type of record being created. Valid types are: A, MX, CNAME, ALIAS, TXT, NS, AAAA, SRV, TLSA, CAA
// content: The answer content for the record.
// ttl (optional): The time to live in seconds for the record. The minimum and the default is 300 seconds.
// prio (optional) The priority of the record for those that support it.
func (c *Client) CreateRecord(ctx context.Context, domain string, record Record) (int, error) {
endpoint := c.BaseURL.JoinPath("dns", "create", domain)
respBody, err := c.do(ctx, endpoint, record)
if err != nil {
return 0, err
}
createResp := createResponse{}
err = json.Unmarshal(respBody, &createResp)
if err != nil {
return 0, fmt.Errorf("failed to unmarshal response: %w", err)
}
if createResp.Status.Status != statusSuccess {
return 0, createResp.Status
}
return createResp.ID, nil
}
// EditRecord edits a DNS record.
//
// name (optional): The subdomain for the record being created, not including the domain itself. Leave blank to create a record on the root domain. Use * to create a wildcard record.
// type: The type of record being created. Valid types are: A, MX, CNAME, ALIAS, TXT, NS, AAAA, SRV, TLSA, CAA
// content: The answer content for the record.
// ttl (optional): The time to live in seconds for the record. The minimum and the default is 300 seconds.
// prio (optional) The priority of the record for those that support it.
func (c *Client) EditRecord(ctx context.Context, domain string, id int, record Record) error {
endpoint := c.BaseURL.JoinPath("dns", "edit", domain, strconv.Itoa(id))
respBody, err := c.do(ctx, endpoint, record)
if err != nil {
return err
}
statusResp := Status{}
err = json.Unmarshal(respBody, &statusResp)
if err != nil {
return fmt.Errorf("failed to unmarshal response: %w", err)
}
if statusResp.Status != statusSuccess {
return statusResp
}
return nil
}
// DeleteRecord deletes a specific DNS record.
func (c *Client) DeleteRecord(ctx context.Context, domain string, id int) error {
endpoint := c.BaseURL.JoinPath("dns", "delete", domain, strconv.Itoa(id))
respBody, err := c.do(ctx, endpoint, nil)
if err != nil {
return err
}
statusResp := Status{}
err = json.Unmarshal(respBody, &statusResp)
if err != nil {
return fmt.Errorf("failed to unmarshal response: %w", err)
}
if statusResp.Status != statusSuccess {
return statusResp
}
return nil
}
// RetrieveRecords retrieve all editable DNS records associated with a domain.
func (c *Client) RetrieveRecords(ctx context.Context, domain string) ([]Record, error) {
endpoint := c.BaseURL.JoinPath("dns", "retrieve", domain)
respBody, err := c.do(ctx, endpoint, nil)
if err != nil {
return nil, err
}
retrieveResp := retrieveResponse{}
err = json.Unmarshal(respBody, &retrieveResp)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
if retrieveResp.Status.Status != statusSuccess {
return nil, retrieveResp.Status
}
return retrieveResp.Records, nil
}
// RetrieveSSLBundle retrieve the SSL certificate bundle for the domain.
func (c *Client) RetrieveSSLBundle(ctx context.Context, domain string) (SSLBundle, error) {
endpoint := c.BaseURL.JoinPath("ssl", "retrieve", domain)
respBody, err := c.do(ctx, endpoint, nil)
if err != nil {
return SSLBundle{}, err
}
bundleResp := sslBundleResponse{}
err = json.Unmarshal(respBody, &bundleResp)
if err != nil {
return SSLBundle{}, fmt.Errorf("failed to unmarshal response: %w", err)
}
if bundleResp.Status.Status != statusSuccess {
return SSLBundle{}, bundleResp.Status
}
return bundleResp.SSLBundle, nil
}
func (c *Client) do(ctx context.Context, endpoint *url.URL, apiRequest interface{}) ([]byte, error) {
request := authRequest{
APIKey: c.apiKey,
SecretAPIKey: c.secretAPIKey,
apiRequest: apiRequest,
}
reqBody, err := json.Marshal(request)
if err != nil {
return nil, fmt.Errorf("failed to marshal request body: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), bytes.NewReader(reqBody))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to call API: %w", err)
}
defer func() { _ = resp.Body.Close() }()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
switch resp.StatusCode {
case http.StatusOK:
return respBody, nil
case http.StatusServiceUnavailable:
// related to https://github.com/nrdcg/porkbun/issues/5
return nil, &ServerError{
StatusCode: resp.StatusCode,
Message: http.StatusText(http.StatusServiceUnavailable),
}
default:
return nil, &ServerError{
StatusCode: resp.StatusCode,
Message: string(respBody),
}
}
}