-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
154 lines (122 loc) · 3.78 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
145
146
147
148
149
150
151
152
153
154
package gandiapi
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"strings"
)
// Client manages requests Gandi API.
type Client struct {
apiKey string
endpoint string
debug bool
dryRun bool
httpClient *http.Client
}
// NewClient returns a client.
func NewClient(apiKey string, debug, dryRun bool) *Client {
return &Client{apiKey: apiKey, endpoint: "https://api.gandi.net/v5/", debug: debug, dryRun: dryRun, httpClient: &http.Client{}}
}
func (c *Client) SetEndpoint(endpoint string) {
c.endpoint = endpoint
}
func (c *Client) ask(method, path string, params, recipient interface{}) (http.Header, error) {
resp, err := c.do(method, path, params, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
decoder.Decode(recipient)
return resp.Header, nil
}
type StandardResponse struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
UUID string `json:"uuid,omitempty"`
Object string `json:"object,omitempty"`
Cause string `json:"cause,omitempty"`
Status string `json:"status,omitempty"`
Errors []StandardError `json:"errors,omitempty"`
}
type StandardError struct {
Location string `json:"location"`
Name string `json:"name"`
Description string `json:"description"`
}
func (c *Client) do(method, path string, p interface{}, extraHeaders [][2]string) (*http.Response, error) {
var (
err error
req *http.Request
)
params, err := json.Marshal(p)
if err != nil {
return nil, err
}
suffix := ""
if params != nil && string(params) != "null" {
req, err = http.NewRequest(method, c.endpoint+path+suffix, bytes.NewReader(params))
} else {
req, err = http.NewRequest(method, c.endpoint+path+suffix, nil)
}
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Apikey "+c.apiKey)
req.Header.Add("Content-Type", "application/json")
if c.dryRun {
req.Header.Add("Dry-Run", "1")
}
for _, header := range extraHeaders {
req.Header.Add(header[0], header[1])
}
if c.debug {
dump, _ := httputil.DumpRequestOut(req, true)
fmt.Println("=======================================\nREQUEST:")
fmt.Println(string(dump))
}
resp, err := c.httpClient.Do(req)
if err != nil {
return resp, err
}
if c.debug {
dump, _ := httputil.DumpResponse(resp, true)
fmt.Println("=======================================\nRESPONSE:")
fmt.Println(string(dump))
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
defer resp.Body.Close()
var message StandardResponse
decoder := json.NewDecoder(resp.Body)
decoder.Decode(&message)
if message.Message != "" {
err = fmt.Errorf("%d: %s", resp.StatusCode, message.Message)
} else if len(message.Errors) > 0 {
var errors []string
for _, oneError := range message.Errors {
errors = append(errors, fmt.Sprintf("%s: %s", oneError.Name, oneError.Description))
}
err = fmt.Errorf(strings.Join(errors, ", "))
} else {
err = fmt.Errorf("%d", resp.StatusCode)
}
}
return resp, err
}
func (c *Client) get(path string, params, recipient interface{}) (http.Header, error) {
return c.ask(http.MethodGet, path, params, recipient)
}
func (c *Client) post(path string, params, recipient interface{}) (http.Header, error) {
return c.ask(http.MethodPost, path, params, recipient)
}
func (c *Client) put(path string, params, recipient interface{}) (http.Header, error) {
return c.ask(http.MethodPut, path, params, recipient)
}
func (c *Client) patch(path string, params, recipient interface{}) (http.Header, error) {
return c.ask(http.MethodPatch, path, params, recipient)
}
func (c *Client) delete(path string, params, recipient interface{}) (http.Header, error) {
return c.ask(http.MethodDelete, path, params, recipient)
}