-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapi.go
175 lines (143 loc) · 4 KB
/
api.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
package dnsimple
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/hashicorp/go-cleanhttp"
)
// Client provides a client to the DNSimple API
type Client struct {
// Access Token
Token string
// User Email
Email string
// Domain Token
DomainToken string
// URL to the DO API to use
URL string
// HttpClient is the client to use. A client with
// default values will be used if not provided.
Http *http.Client
}
// DNSimpleError is the error format that they return
// to us if there is a problem
type DNSimpleError struct {
Errors map[string][]string `json:"errors"`
}
func (d *DNSimpleError) Join() string {
var errs []string
for k, v := range d.Errors {
errs = append(errs, fmt.Sprintf("%s errors: %s", k, strings.Join(v, ", ")))
}
return strings.Join(errs, ", ")
}
// NewClient returns a new dnsimple client,
// requires an authorization token. You can generate
// an OAuth token by visiting the Apps & API section
// of the DNSimple control panel for your account.
func NewClient(email string, token string) (*Client, error) {
client := Client{
Token: token,
Email: email,
URL: "https://api.dnsimple.com/v1",
Http: cleanhttp.DefaultClient(),
}
return &client, nil
}
func NewClientWithDomainToken(domainToken string) (*Client, error) {
client := Client{
DomainToken: domainToken,
URL: "https://api.dnsimple.com/v1",
Http: cleanhttp.DefaultClient(),
}
return &client, nil
}
// Creates a new request with the params
func (c *Client) NewRequest(body map[string]interface{}, method string, endpoint string) (*http.Request, error) {
u, err := url.Parse(c.URL + endpoint)
if err != nil {
return nil, fmt.Errorf("Error parsing base URL: %s", err)
}
rBody, err := encodeBody(body)
if err != nil {
return nil, fmt.Errorf("Error encoding request body: %s", err)
}
// Build the request
req, err := http.NewRequest(method, u.String(), rBody)
if err != nil {
return nil, fmt.Errorf("Error creating request: %s", err)
}
// Add the authorization header
if c.DomainToken != "" {
req.Header.Add("X-DNSimple-Domain-Token", c.DomainToken)
} else {
req.Header.Add("X-DNSimple-Token", fmt.Sprintf("%s:%s", c.Email, c.Token))
}
req.Header.Add("Accept", "application/json")
// If it's a not a get, add a content-type
if method != "GET" {
req.Header.Add("Content-Type", "application/json")
}
return req, nil
}
// parseErr is used to take an error json resp
// and return a single string for use in error messages
func parseErr(resp *http.Response) error {
dnsError := DNSimpleError{}
err := decodeBody(resp, &dnsError)
// if there was an error decoding the body, just return that
if err != nil {
return fmt.Errorf("Error parsing error body for non-200 request: %s", err)
}
return fmt.Errorf("API Error: %s", dnsError.Join())
}
// decodeBody is used to JSON decode a body
func decodeBody(resp *http.Response, out interface{}) error {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if err = json.Unmarshal(body, &out); err != nil {
return err
}
return nil
}
func encodeBody(obj interface{}) (io.Reader, error) {
buf := bytes.NewBuffer(nil)
enc := json.NewEncoder(buf)
if err := enc.Encode(obj); err != nil {
return nil, err
}
return buf, nil
}
// checkResp wraps http.Client.Do() and verifies that the
// request was successful. A non-200 request returns an error
// formatted to included any validation problems or otherwise
func checkResp(resp *http.Response, err error) (*http.Response, error) {
// If the err is already there, there was an error higher
// up the chain, so just return that
if err != nil {
return resp, err
}
switch i := resp.StatusCode; {
case i == 200:
return resp, nil
case i == 201:
return resp, nil
case i == 202:
return resp, nil
case i == 204:
return resp, nil
case i == 422:
return nil, fmt.Errorf("API Error: %s", resp.Status)
case i == 400:
return nil, parseErr(resp)
default:
return nil, fmt.Errorf("API Error: %s", resp.Status)
}
}