This repository has been archived by the owner on Oct 27, 2020. It is now read-only.
forked from decker502/dnspod-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnspod.go
234 lines (196 loc) · 5.9 KB
/
dnspod.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Package dnspod implements a client for the dnspod API.
//
// In order to use this package you will need a dnspod account and your API Token.
package dnspod
import (
// "bytes"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
"time"
)
const (
libraryVersion = "0.1"
baseURL = "https://dnsapi.cn/"
userAgent = "dnspod-go/" + libraryVersion
apiVersion = "v1"
timeout = 5
keepAlive = 30
)
// dnspod API docs: https://www.dnspod.cn/docs/info.html
// CommonParams common options for dnspod-go
type CommonParams struct {
LoginToken string
Format string
Lang string
ErrorOnEmpty string
UserID string
Timeout int
KeepAlive int
}
func newPayLoad(params CommonParams) url.Values {
p := url.Values{}
if params.LoginToken != "" {
p.Set("login_token", params.LoginToken)
}
if params.Format != "" {
p.Set("format", params.Format)
}
if params.Lang != "" {
p.Set("lang", params.Lang)
}
if params.ErrorOnEmpty != "" {
p.Set("error_on_empty", params.ErrorOnEmpty)
}
if params.UserID != "" {
p.Set("user_id", params.UserID)
}
return p
}
type Status struct {
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
}
type Client struct {
// HTTP client used to communicate with the API.
HttpClient *http.Client
// CommonParams used communicating with the dnspod API.
CommonParams CommonParams
// Base URL for API requests.
// Defaults to the public dnspod API, but can be set to a different endpoint (e.g. the sandbox).
// BaseURL should always be specified with a trailing slash.
BaseURL string
// User agent used when communicating with the dnspod API.
UserAgent string
// Services used for talking to different parts of the dnspod API.
Domains *DomainsService
}
// NewClient returns a new dnspod API client.
func NewClient(CommonParams CommonParams) *Client {
var _timeout, _keepalive int
_timeout = timeout
_keepalive = keepAlive
if CommonParams.Timeout != 0 {
_timeout = CommonParams.Timeout
}
if CommonParams.KeepAlive != 0 {
_keepalive = CommonParams.KeepAlive
}
cli := http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: time.Duration(_timeout) * time.Second,
KeepAlive: time.Duration(_keepalive) * time.Second,
}).Dial,
},
}
c := &Client{HttpClient: &cli, CommonParams: CommonParams, BaseURL: baseURL, UserAgent: userAgent}
c.Domains = &DomainsService{client: c}
return c
}
// NewRequest creates an API request.
// The path is expected to be a relative path and will be resolved
// according to the BaseURL of the Client. Paths should always be specified without a preceding slash.
func (client *Client) NewRequest(method, path string, payload url.Values) (*http.Request, error) {
url := client.BaseURL + fmt.Sprintf("%s", path)
req, err := http.NewRequest(method, url, strings.NewReader(payload.Encode()))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Accept", "application/json")
req.Header.Add("User-Agent", client.UserAgent)
return req, nil
}
func (c *Client) get(path string, v interface{}) (*Response, error) {
return c.Do("GET", path, nil, v)
}
func (c *Client) post(path string, payload url.Values, v interface{}) (*Response, error) {
return c.Do("POST", path, payload, v)
}
func (c *Client) put(path string, payload url.Values, v interface{}) (*Response, error) {
return c.Do("PUT", path, payload, v)
}
func (c *Client) delete(path string, payload url.Values) (*Response, error) {
return c.Do("DELETE", path, payload, nil)
}
// Do sends an API request and returns the API response.
// The API response is JSON decoded and stored in the value pointed by v,
// or returned as an error if an API error has occurred.
// If v implements the io.Writer interface, the raw response body will be written to v,
// without attempting to decode it.
func (c *Client) Do(method, path string, payload url.Values, v interface{}) (*Response, error) {
req, err := c.NewRequest(method, path, payload)
if err != nil {
return nil, err
}
res, err := c.HttpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
response := &Response{Response: res}
err = CheckResponse(res)
if err != nil {
return response, err
}
if v != nil {
if w, ok := v.(io.Writer); ok {
io.Copy(w, res.Body)
} else {
err = json.NewDecoder(res.Body).Decode(v)
}
}
return response, err
}
// A Response represents an API response.
type Response struct {
*http.Response
}
// An ErrorResponse represents an error caused by an API request.
type ErrorResponse struct {
Response *http.Response // HTTP response that caused this error
Message string `json:"message"` // human-readable message
}
// Error implements the error interface.
func (r *ErrorResponse) Error() string {
return fmt.Sprintf("%v %v: %d %v",
r.Response.Request.Method, r.Response.Request.URL,
r.Response.StatusCode, r.Message)
}
// CheckResponse checks the API response for errors, and returns them if present.
// A response is considered an error if the status code is different than 2xx. Specific requests
// may have additional requirements, but this is sufficient in most of the cases.
func CheckResponse(r *http.Response) error {
if code := r.StatusCode; 200 <= code && code <= 299 {
return nil
}
errorResponse := &ErrorResponse{Response: r}
err := json.NewDecoder(r.Body).Decode(errorResponse)
if err != nil {
return err
}
return errorResponse
}
// Date custom type.
type Date struct {
time.Time
}
// UnmarshalJSON handles the deserialization of the custom Date type.
func (d *Date) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("date should be a string, got %s", data)
}
t, err := time.Parse("2006-01-02", s)
if err != nil {
return fmt.Errorf("invalid date: %v", err)
}
d.Time = t
return nil
}