-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
144 lines (115 loc) · 3.29 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
package cloudflare
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
)
const (
endPoint = "https://www.cloudflare.com/api_json.html"
)
type Client struct {
Email, Token string
}
func (client *Client) GetZoneList() (zones ZoneList, err error) {
var params map[string]string
resp, err := client.post("zone_load_multi", params)
if err == nil {
return makeZoneList(resp)
} else {
return ZoneList{}, err
}
}
func (client *Client) GetRecordList(zone Zone, offset int) (records RecordList, err error) {
params := make(map[string]string, 2)
params["z"] = zone.ZoneName
if offset > 0 {
params["o"] = string(offset)
}
resp, err := client.post("rec_load_all", params)
if err != nil {
return RecordList{}, err
} else {
return makeRecordList(resp)
}
}
func (client *Client) AddRecord(zone Zone, record Record) (r Record, err error) {
params := make(map[string]string, 5)
params["z"] = zone.ZoneName
params["type"] = record.Type
params["name"] = record.Name
params["content"] = record.Content
params["ttl"] = record.Ttl
response, err := client.post("rec_new", params)
rec, err := makeRecordFromResponse(response)
return rec, err
}
func (client *Client) UpdateRecord(zone Zone, record Record) (r Record, err error) {
params := make(map[string]string, 7)
params["z"] = zone.ZoneName
params["type"] = record.Type
params["id"] = record.RecId
params["name"] = record.Name
params["content"] = record.Content
params["ttl"] = record.Ttl
params["service_mode"] = record.ServiceMode
response, err := client.post("rec_edit", params)
rec, err := makeRecordFromResponse(response)
return rec, err
}
func (client *Client) RemoveRecord(zone Zone, record Record) (success bool, err error) {
params := make(map[string]string, 2)
params["z"] = zone.ZoneName
params["id"] = record.RecId
response, err := client.post("rec_delete", params)
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
var data CfResponse
err = json.Unmarshal(body, &data)
if data.Msg != "" {
return false, errors.New(data.Msg)
}
return data.Result == "success", nil
}
func makeZoneList(resp *http.Response) (zones ZoneList, err error) {
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ZoneList{}, err
}
var response CfResponse
err = json.Unmarshal(contents, &response)
return response.Response.Zones, nil
}
func makeRecordList(resp *http.Response) (records RecordList, err error) {
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return RecordList{}, err
}
var response CfResponse
err = json.Unmarshal(contents, &response)
return response.Response.Records, nil
}
func (client *Client) post(act string, params map[string]string) (resp *http.Response, err error) {
clientParams := url.Values{}
clientParams.Set("a", act)
clientParams.Set("tkn", client.Token)
clientParams.Set("email", client.Email)
if len(params) > 0 {
for k, v := range params {
clientParams.Set(k, v)
}
}
return http.PostForm(endPoint, clientParams)
}
func makeRecordFromResponse(response *http.Response) (r Record, err error) {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
var data CfRecordResponse
err = json.Unmarshal(body, &data)
if data.Msg != "" {
err = errors.New(data.Msg)
return Record{}, err
}
return data.Response.Rec.Record, nil
}