-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
client.go
131 lines (108 loc) · 2.59 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
package internal
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"
)
const defaultBaseURL = "https://api.reg.ru/api/regru2/"
// Client the reg.ru client.
type Client struct {
username string
password string
BaseURL string
HTTPClient *http.Client
}
// NewClient Creates a reg.ru client.
func NewClient(username, password string) *Client {
return &Client{
username: username,
password: password,
BaseURL: defaultBaseURL,
HTTPClient: http.DefaultClient,
}
}
// RemoveTxtRecord removes a TXT record.
// https://www.reg.ru/support/help/api2#zone_remove_record
func (c Client) RemoveTxtRecord(domain, subDomain, content string) error {
request := RemoveRecordRequest{
Username: c.username,
Password: c.password,
Domains: []Domain{
{DName: domain},
},
SubDomain: subDomain,
Content: content,
RecordType: "TXT",
OutputContentType: "plain",
}
resp, err := c.do(request, "zone", "remove_record")
if err != nil {
return err
}
return resp.HasError()
}
// AddTXTRecord adds a TXT record.
// https://www.reg.ru/support/help/api2#zone_add_txt
func (c Client) AddTXTRecord(domain, subDomain, content string) error {
request := AddTxtRequest{
Username: c.username,
Password: c.password,
Domains: []Domain{
{DName: domain},
},
SubDomain: subDomain,
Text: content,
OutputContentType: "plain",
}
resp, err := c.do(request, "zone", "add_txt")
if err != nil {
return err
}
return resp.HasError()
}
func (c Client) do(request interface{}, fragments ...string) (*APIResponse, error) {
endpoint, err := c.createEndpoint(fragments...)
if err != nil {
return nil, err
}
inputData, err := json.Marshal(request)
if err != nil {
return nil, err
}
query := endpoint.Query()
query.Add("input_data", string(inputData))
query.Add("input_format", "json")
endpoint.RawQuery = query.Encode()
resp, err := http.Get(endpoint.String())
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode/100 != 2 {
return nil, fmt.Errorf("API error, status code: %d", resp.StatusCode)
}
all, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var apiResp APIResponse
err = json.Unmarshal(all, &apiResp)
if err != nil {
return nil, err
}
return &apiResp, nil
}
func (c Client) createEndpoint(fragments ...string) (*url.URL, error) {
baseURL, err := url.Parse(c.BaseURL)
if err != nil {
return nil, err
}
endpoint, err := baseURL.Parse(path.Join(baseURL.Path, path.Join(fragments...)))
if err != nil {
return nil, err
}
return endpoint, nil
}