-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
164 lines (133 loc) · 3.88 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
// Package freemyip contains a client of the DNS API of freemyip.
package freemyip
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
querystring "github.com/google/go-querystring/query"
)
// RootDomain the root domain of all domains.
const RootDomain = "freemyip.com"
const defaultBaseURL = "https://freemyip.com/update"
const (
codeError = "ERROR"
codeOK = "OK"
)
type query struct {
Token string `url:"token"`
Domain string `url:"domain"`
TXT string `url:"txt,omitempty"`
MyIP string `url:"myip,omitempty"`
Delete string `url:"delete,omitempty"`
Verbose string `url:"verbose,omitempty"`
}
// Client an API client for freemyip.
type Client struct {
HTTPClient *http.Client
baseURL *url.URL
token string
verbose bool
}
// New creates a new Client.
func New(token string, verbose bool) *Client {
baseURL, _ := url.Parse(defaultBaseURL)
return &Client{
HTTPClient: &http.Client{Timeout: 10 * time.Second},
baseURL: baseURL,
token: token,
verbose: verbose,
}
}
// UpdateDomain updates a domain.
// - `domain` is the custom part of the real domain. (ex: `YOUR_DOMAIN` in `YOUR_DOMAIN.freemyip.com`)
// - `myIP` is optional.
func (c Client) UpdateDomain(ctx context.Context, domain, myIP string) (string, error) {
q := query{
Token: c.token,
Domain: fmt.Sprintf("%s.%s", domain, RootDomain),
MyIP: myIP,
Verbose: boolToString(c.verbose),
}
return c.do(ctx, q)
}
// DeleteDomain deletes a domain.
// - `domain` is the custom part of the real domain. (ex: `YOUR_DOMAIN` in `YOUR_DOMAIN.freemyip.com`)
func (c Client) DeleteDomain(ctx context.Context, domain string) (string, error) {
q := query{
Token: c.token,
Domain: fmt.Sprintf("%s.%s", domain, RootDomain),
Delete: "yes",
Verbose: boolToString(c.verbose),
}
return c.do(ctx, q)
}
// EditTXTRecord creates or updates a TXT record value for a domain.
// - `domain` is the custom part of the real domain. (ex: `YOUR_DOMAIN` in `YOUR_DOMAIN.freemyip.com`)
// - `value` is the TXT record content.
func (c Client) EditTXTRecord(ctx context.Context, domain, value string) (string, error) {
q := query{
Token: c.token,
Domain: fmt.Sprintf("%s.%s", domain, RootDomain),
TXT: value,
Verbose: boolToString(c.verbose),
}
return c.do(ctx, q)
}
// DeleteTXTRecord delete a TXT record for a domain.
// - `domain` is the custom part of the real domain. (ex: `YOUR_DOMAIN` in `YOUR_DOMAIN.freemyip.com`)
// - `value` is the TXT record content.
func (c Client) DeleteTXTRecord(ctx context.Context, domain string) (string, error) {
q := query{
Token: c.token,
Domain: fmt.Sprintf("%s.%s", domain, RootDomain),
TXT: "null",
Verbose: boolToString(c.verbose),
}
return c.do(ctx, q)
}
func (c Client) do(ctx context.Context, q query) (string, error) {
endpoint := c.baseURL.JoinPath("/")
values, err := querystring.Values(q)
if err != nil {
return "", fmt.Errorf("query parameters: %w", err)
}
endpoint.RawQuery = values.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return "", fmt.Errorf("creates request: %w", err)
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
return "", fmt.Errorf("do API call: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
all, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("error: %d: %s", resp.StatusCode, string(all))
}
all, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("reads response body: %w", err)
}
body := strings.TrimSpace(string(all))
parts := strings.SplitN(body, "\n", 2)
switch parts[0] {
case codeError:
return "", errors.New(strings.Join(parts, " "))
case codeOK:
return body, nil
default:
return "", errors.New(strings.Join(parts, " "))
}
}
func boolToString(v bool) string {
if v {
return "yes"
}
return ""
}