forked from go-acme/lego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
158 lines (124 loc) · 3.98 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
package internal
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
const defaultBaseURL = "https://api.netlify.com/api/v1"
// Client Netlify API client.
type Client struct {
HTTPClient *http.Client
BaseURL string
token string
}
// NewClient creates a new Client.
func NewClient(token string) *Client {
return &Client{
HTTPClient: http.DefaultClient,
BaseURL: defaultBaseURL,
token: token,
}
}
// GetRecords gets a DNS records.
func (c *Client) GetRecords(zoneID string) ([]DNSRecord, error) {
endpoint, err := c.createEndpoint("dns_zones", zoneID, "dns_records")
if err != nil {
return nil, fmt.Errorf("failed to parse endpoint: %w", err)
}
req, err := http.NewRequest(http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("API call failed: %w", err)
}
defer func() { _ = resp.Body.Close() }()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid status code: %s: %s", resp.Status, string(body))
}
var records []DNSRecord
err = json.Unmarshal(body, &records)
if err != nil {
return nil, fmt.Errorf("failed to marshal response body: %w", err)
}
return records, nil
}
// CreateRecord creates a DNS records.
func (c *Client) CreateRecord(zoneID string, record DNSRecord) (*DNSRecord, error) {
endpoint, err := c.createEndpoint("dns_zones", zoneID, "dns_records")
if err != nil {
return nil, fmt.Errorf("failed to parse endpoint: %w", err)
}
marshaledRecord, err := json.Marshal(record)
if err != nil {
return nil, fmt.Errorf("failed to marshal request body: %w", err)
}
req, err := http.NewRequest(http.MethodPost, endpoint.String(), bytes.NewReader(marshaledRecord))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("API call failed: %w", err)
}
defer func() { _ = resp.Body.Close() }()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("invalid status code: %s: %s", resp.Status, string(body))
}
var recordResp DNSRecord
err = json.Unmarshal(body, &recordResp)
if err != nil {
return nil, fmt.Errorf("failed to marshal response body: %w", err)
}
return &recordResp, nil
}
// RemoveRecord removes a DNS records.
func (c *Client) RemoveRecord(zoneID, recordID string) error {
endpoint, err := c.createEndpoint("dns_zones", zoneID, "dns_records", recordID)
if err != nil {
return fmt.Errorf("failed to parse endpoint: %w", err)
}
req, err := http.NewRequest(http.MethodDelete, endpoint.String(), nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
resp, err := c.HTTPClient.Do(req)
if err != nil {
return fmt.Errorf("API call failed: %w", err)
}
defer func() { _ = resp.Body.Close() }()
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %w", err)
}
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("invalid status code: %s: %s", resp.Status, string(body))
}
return nil
}
func (c *Client) createEndpoint(parts ...string) (*url.URL, error) {
base, err := url.Parse(c.BaseURL)
if err != nil {
return nil, fmt.Errorf("failed to parse base URL: %w", err)
}
return base.JoinPath(parts...), nil
}