Debian Codegraph @

api.go

raw symbols SCIP
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 // SandboxURL is the URL of the DNS Made Easy Sandbox
18 const SandboxURL = "http://api.sandbox.dnsmadeeasy.com/V2.0"
19
20 // Client provides a client to the dnsmadeeasy API
21 type Client struct {
22 // API Key
23 AKey string
24
25 // Secret Key
26 SKey string
27
28 // URL to the API to use
29 URL string
30
31 // HttpClient is the client to use. Default will be
32 // used if not provided.
33 HTTP *http.Client
34 }
35
36 // Body is the body of a request
37 type Body map[string]interface{}
38
39 // Error is the error format that they return
40 // to us if there is a problem
41 type Error struct {
42 Errors []string `json:"error"`
43 }
44
45 // Join joins all the errors together, separated by spaces.
46 func (d *Error) Join() string {
47 return strings.Join(d.Errors, " ")
48 }
49
50 // NewClient returns a new dnsmadeeasy client. It requires an API key and
51 // secret key. You can generate them by visiting the Config, Account
52 // Information section of the dnsmadeeasy control panel for your account.
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 // NewRequest creates a new request with the params
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 // Build the request
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 // Calculate the hexadecimal HMAC SHA1 of requestDate using sKey
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 // Add the authorization header
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 // If it's a not a get, add a content-type
94 if method != "GET" {
95 req.Header.Add("Content-Type", "application/json")
96 }
97 return req, nil
98 }
99
100 // parseError is used to take an error json resp
101 // and return a single string for use in error messages
102 func parseError(resp *http.Response) error {
103 dnsError := Error{}
104 err := decodeBody(resp, &dnsError)
105
106 // if there was an error decoding the body, just return that
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 // decodeBody is used to JSON decode a body
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 // checkResp wraps http.Client.Do() and verifies that the
129 // request was successful. A non-200 request returns an error
130 // formatted to included any validation problems or otherwise
131 func checkResp(resp *http.Response, err error) (*http.Response, error) {
132 // If the err is already there, there was an error higher
133 // up the chain, so just return that
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 }