forked from joeig/go-powerdns
/
powerdns.go
179 lines (147 loc) · 3.43 KB
/
powerdns.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
package powerdns
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
)
type service struct {
client *Client
}
// Client configuration structure
type Client struct {
Scheme string
Hostname string
Port string
VHost string
Headers map[string]string
httpClient *http.Client
common service // Reuse a single struct instead of allocating one for each service on the heap
Cryptokeys *CryptokeysService
Records *RecordsService
Servers *ServersService
Statistics *StatisticsService
Zones *ZonesService
}
// logFatalf makes log.Fatalf testable
var logFatalf = log.Fatalf
// NewClient initializes a new client instance
func NewClient(baseURL string, vHost string, headers map[string]string, httpClient *http.Client) *Client {
scheme, hostname, port, err := parseBaseURL(baseURL)
if err != nil {
logFatalf("%s is not a valid url: %v", baseURL, err)
}
c := &Client{
Scheme: scheme,
Hostname: hostname,
Port: port,
VHost: parseVHost(vHost),
Headers: headers,
}
if httpClient == nil {
c.httpClient = http.DefaultClient
}
c.common.client = c
c.Cryptokeys = (*CryptokeysService)(&c.common)
c.Records = (*RecordsService)(&c.common)
c.Servers = (*ServersService)(&c.common)
c.Statistics = (*StatisticsService)(&c.common)
c.Zones = (*ZonesService)(&c.common)
return c
}
func parseBaseURL(baseURL string) (string, string, string, error) {
u, err := url.Parse(baseURL)
if err != nil {
return "", "", "", err
}
hp := strings.Split(u.Host, ":")
hostname := hp[0]
var port string
if len(hp) > 1 {
port = hp[1]
} else {
if u.Scheme == "https" {
port = "443"
} else {
port = "80"
}
}
return u.Scheme, hostname, port, nil
}
func parseVHost(vHost string) string {
if vHost == "" {
return "localhost"
}
return vHost
}
func generateAPIURL(scheme, hostname, port, path string) url.URL {
u := url.URL{}
u.Scheme = scheme
u.Host = fmt.Sprintf("%s:%s", hostname, port)
u.Path = fmt.Sprintf("/api/v1/%s", path)
return u
}
func trimDomain(domain string) string {
return strings.TrimSuffix(domain, ".")
}
func (p *Client) newRequest(method string, path string, body interface{}) (*http.Request, error) {
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}
apiURL := generateAPIURL(p.Scheme, p.Hostname, p.Port, path)
req, err := http.NewRequest(method, apiURL.String(), buf)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
}
req.Header.Set("User-Agent", "go-powerdns")
for key, value := range p.Headers {
req.Header.Set(key, value)
}
return req, nil
}
func (p *Client) do(req *http.Request, v interface{}) (*http.Response, error) {
resp, err := p.httpClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode == 401 {
return resp, &Error{
Status: resp.Status,
Message: "Unauthorized",
}
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
defer func() {
_ = resp.Body.Close()
}()
apiError := new(Error)
err = json.NewDecoder(resp.Body).Decode(apiError)
if err != nil {
return resp, err
}
return resp, &Error{
Status: resp.Status,
Message: apiError.Message,
}
}
if v != nil {
defer func() {
_ = resp.Body.Close()
}()
err = json.NewDecoder(resp.Body).Decode(v)
}
return resp, err
}