| 1 |
package dnsmadeeasy |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/hmac" |
| 6 |
"crypto/sha1" |
| 7 |
"encoding/hex" |
| 8 |
"encoding/json" |
| 9 |
"fmt" |
| 10 |
"io/ioutil" |
| 11 |
"net/http" |
| 12 |
"net/url" |
| 13 |
"strings" |
| 14 |
"time" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
const SandboxURL = "http://api.sandbox.dnsmadeeasy.com/V2.0" |
| 19 |
|
| 20 |
|
| 21 |
type Client struct { |
| 22 |
|
| 23 |
AKey string |
| 24 |
|
| 25 |
|
| 26 |
SKey string |
| 27 |
|
| 28 |
|
| 29 |
URL string |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
HTTP *http.Client |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
type Body map[string]interface{} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
type Error struct { |
| 42 |
Errors []string `json:"error"` |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
func (d *Error) Join() string { |
| 47 |
return strings.Join(d.Errors, " ") |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
func NewClient(akey string, skey string) (*Client, error) { |
| 54 |
client := Client{ |
| 55 |
AKey: akey, |
| 56 |
SKey: skey, |
| 57 |
URL: "https://api.dnsmadeeasy.com/V2.0", |
| 58 |
HTTP: http.DefaultClient, |
| 59 |
} |
| 60 |
return &client, nil |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
func (c *Client) NewRequest(method, path string, body *bytes.Buffer, |
| 65 |
requestDate string) (*http.Request, error) { |
| 66 |
|
| 67 |
url, err := url.Parse(c.URL + path) |
| 68 |
if err != nil { |
| 69 |
return nil, fmt.Errorf("Error parsing base URL: %s", err) |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
req, err := http.NewRequest(method, url.String(), body) |
| 74 |
if err != nil { |
| 75 |
return nil, fmt.Errorf("Error creating request: %s", err) |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
key := []byte(c.SKey) |
| 80 |
h := hmac.New(sha1.New, key) |
| 81 |
if len(requestDate) == 0 { |
| 82 |
requestDate = time.Now().UTC().Format(http.TimeFormat) |
| 83 |
} |
| 84 |
h.Write([]byte(requestDate)) |
| 85 |
hmacString := hex.EncodeToString(h.Sum(nil)) |
| 86 |
|
| 87 |
|
| 88 |
req.Header.Add("X-Dnsme-Apikey", c.AKey) |
| 89 |
req.Header.Add("X-Dnsme-Requestdate", requestDate) |
| 90 |
req.Header.Add("X-Dnsme-Hmac", hmacString) |
| 91 |
req.Header.Add("Accept", "application/json") |
| 92 |
|
| 93 |
|
| 94 |
if method != "GET" { |
| 95 |
req.Header.Add("Content-Type", "application/json") |
| 96 |
} |
| 97 |
return req, nil |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
func parseError(resp *http.Response) error { |
| 103 |
dnsError := Error{} |
| 104 |
err := decodeBody(resp, &dnsError) |
| 105 |
|
| 106 |
|
| 107 |
if err != nil { |
| 108 |
return fmt.Errorf("Error parsing error body for non-200 request: %s", err) |
| 109 |
} |
| 110 |
return fmt.Errorf("API Error (%d): %s", resp.StatusCode, dnsError.Join()) |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
func decodeBody(resp *http.Response, out interface{}) error { |
| 115 |
body, err := ioutil.ReadAll(resp.Body) |
| 116 |
|
| 117 |
if err != nil { |
| 118 |
return err |
| 119 |
} |
| 120 |
|
| 121 |
if err = json.Unmarshal(body, &out); err != nil { |
| 122 |
return err |
| 123 |
} |
| 124 |
|
| 125 |
return nil |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
func checkResp(resp *http.Response, err error) (*http.Response, error) { |
| 132 |
|
| 133 |
|
| 134 |
if err != nil { |
| 135 |
return resp, err |
| 136 |
} |
| 137 |
|
| 138 |
if resp.StatusCode/100 == 2 { |
| 139 |
return resp, nil |
| 140 |
} else if resp.StatusCode == 404 { |
| 141 |
return nil, fmt.Errorf("Not found") |
| 142 |
} |
| 143 |
return nil, fmt.Errorf("API Error: %s", resp.Status) |
| 144 |
} |