client.go raw
1 package internal
2
3 import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "fmt"
8 "io"
9 "net/http"
10 "net/url"
11 "strconv"
12 "time"
13
14 "github.com/go-acme/lego/v4/challenge/dns01"
15 "github.com/go-acme/lego/v4/providers/dns/internal/errutils"
16 "golang.org/x/oauth2"
17 )
18
19 // DefaultBaseURL default API endpoint.
20 const DefaultBaseURL = "https://api.digitalocean.com"
21
22 // Client the Digital Ocean API client.
23 type Client struct {
24 BaseURL *url.URL
25 httpClient *http.Client
26 }
27
28 // NewClient creates a new Client.
29 func NewClient(hc *http.Client) *Client {
30 baseURL, _ := url.Parse(DefaultBaseURL)
31
32 if hc == nil {
33 hc = &http.Client{Timeout: 5 * time.Second}
34 }
35
36 return &Client{BaseURL: baseURL, httpClient: hc}
37 }
38
39 func (c *Client) AddTxtRecord(ctx context.Context, zone string, record Record) (*TxtRecordResponse, error) {
40 endpoint := c.BaseURL.JoinPath("v2", "domains", dns01.UnFqdn(zone), "records")
41
42 req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record)
43 if err != nil {
44 return nil, err
45 }
46
47 respData := &TxtRecordResponse{}
48
49 err = c.do(req, respData)
50 if err != nil {
51 return nil, err
52 }
53
54 return respData, nil
55 }
56
57 func (c *Client) RemoveTxtRecord(ctx context.Context, zone string, recordID int) error {
58 endpoint := c.BaseURL.JoinPath("v2", "domains", dns01.UnFqdn(zone), "records", strconv.Itoa(recordID))
59
60 req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
61 if err != nil {
62 return err
63 }
64
65 return c.do(req, nil)
66 }
67
68 func (c *Client) do(req *http.Request, result any) error {
69 resp, err := c.httpClient.Do(req)
70 if err != nil {
71 return errutils.NewHTTPDoError(req, err)
72 }
73
74 defer func() { _ = resp.Body.Close() }()
75
76 if resp.StatusCode >= http.StatusBadRequest {
77 return parseError(req, resp)
78 }
79
80 if result == nil {
81 return nil
82 }
83
84 raw, err := io.ReadAll(resp.Body)
85 if err != nil {
86 return errutils.NewReadResponseError(req, resp.StatusCode, err)
87 }
88
89 err = json.Unmarshal(raw, result)
90 if err != nil {
91 return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
92 }
93
94 return nil
95 }
96
97 func newJSONRequest(ctx context.Context, method string, endpoint *url.URL, payload any) (*http.Request, error) {
98 buf := new(bytes.Buffer)
99
100 if payload != nil {
101 err := json.NewEncoder(buf).Encode(payload)
102 if err != nil {
103 return nil, fmt.Errorf("failed to create request JSON body: %w", err)
104 }
105 }
106
107 req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
108 if err != nil {
109 return nil, fmt.Errorf("unable to create request: %w", err)
110 }
111
112 req.Header.Set("Accept", "application/json")
113
114 // NOTE: Even though the body is empty, DigitalOcean API docs still show setting this Content-Type...
115 req.Header.Set("Content-Type", "application/json")
116
117 return req, nil
118 }
119
120 func parseError(req *http.Request, resp *http.Response) error {
121 raw, _ := io.ReadAll(resp.Body)
122
123 var errInfo APIError
124
125 err := json.Unmarshal(raw, &errInfo)
126 if err != nil {
127 return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw)
128 }
129
130 return fmt.Errorf("[status code %d] %w", resp.StatusCode, errInfo)
131 }
132
133 func OAuthStaticAccessToken(client *http.Client, accessToken string) *http.Client {
134 if client == nil {
135 client = &http.Client{Timeout: 5 * time.Second}
136 }
137
138 client.Transport = &oauth2.Transport{
139 Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken}),
140 Base: client.Transport,
141 }
142
143 return client
144 }
145