-
-
Notifications
You must be signed in to change notification settings - Fork 993
/
client.go
114 lines (92 loc) · 2.58 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
package internal
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
)
// DefaultBaseURL the default API endpoint.
const DefaultBaseURL = "https://api.dreamhost.com"
const (
cmdAddRecord = "dns-add_record"
cmdRemoveRecord = "dns-remove_record"
)
// Client the Dreamhost API client.
type Client struct {
apiKey string
BaseURL string
HTTPClient *http.Client
}
// NewClient Creates a new Client.
func NewClient(apiKey string) *Client {
return &Client{
apiKey: apiKey,
BaseURL: DefaultBaseURL,
HTTPClient: &http.Client{Timeout: 5 * time.Second},
}
}
// AddRecord adds a TXT record.
func (c *Client) AddRecord(ctx context.Context, domain, value string) error {
query, err := c.buildEndpoint(cmdAddRecord, domain, value)
if err != nil {
return err
}
return c.updateTxtRecord(ctx, query)
}
// RemoveRecord removes a TXT record.
func (c *Client) RemoveRecord(ctx context.Context, domain, value string) error {
query, err := c.buildEndpoint(cmdRemoveRecord, domain, value)
if err != nil {
return err
}
return c.updateTxtRecord(ctx, query)
}
// action is either cmdAddRecord or cmdRemoveRecord.
func (c *Client) buildEndpoint(action, domain, txt string) (*url.URL, error) {
endpoint, err := url.Parse(c.BaseURL)
if err != nil {
return nil, err
}
query := endpoint.Query()
query.Set("key", c.apiKey)
query.Set("cmd", action)
query.Set("format", "json")
query.Set("record", domain)
query.Set("type", "TXT")
query.Set("value", txt)
query.Set("comment", url.QueryEscape("Managed By lego"))
endpoint.RawQuery = query.Encode()
return endpoint, nil
}
// updateTxtRecord will either add or remove a TXT record.
func (c *Client) updateTxtRecord(ctx context.Context, endpoint *url.URL) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return fmt.Errorf("unable to create request: %w", 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)
}
raw, err := io.ReadAll(resp.Body)
if err != nil {
return errutils.NewReadResponseError(req, resp.StatusCode, err)
}
var response apiResponse
err = json.Unmarshal(raw, &response)
if err != nil {
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
}
if response.Result == "error" {
return fmt.Errorf("add TXT record failed: %s", response.Data)
}
return nil
}