forked from go-acme/lego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
71 lines (58 loc) · 1.46 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
package internal
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"time"
querystring "github.com/google/go-querystring/query"
)
const defaultBaseURL = "https://iwantmyname.com/basicauth/ddns"
// Record represents a record.
type Record struct {
Hostname string `url:"hostname,omitempty"`
Type string `url:"type,omitempty"`
Value string `url:"value,omitempty"`
TTL int `url:"ttl,omitempty"`
}
// Client iwantmyname client.
type Client struct {
username string
password string
baseURL *url.URL
HTTPClient *http.Client
}
// NewClient creates a new Client.
func NewClient(username string, password string) *Client {
baseURL, _ := url.Parse(defaultBaseURL)
return &Client{
username: username,
password: password,
baseURL: baseURL,
HTTPClient: &http.Client{Timeout: 10 * time.Second},
}
}
// Do send a request (create/add/delete) to the API.
func (c Client) Do(ctx context.Context, record Record) error {
values, err := querystring.Values(record)
if err != nil {
return err
}
endpoint := c.baseURL
endpoint.RawQuery = values.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), http.NoBody)
if err != nil {
return err
}
req.SetBasicAuth(c.username, c.password)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode/100 != 2 {
data, _ := io.ReadAll(resp.Body)
return fmt.Errorf("status code: %d, %s", resp.StatusCode, string(data))
}
return nil
}