This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gandi.go
153 lines (142 loc) · 4.06 KB
/
gandi.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
package gandi
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"strings"
)
const (
gandiEndpoint = "https://dns.api.gandi.net/api/v5/"
// HTTP Methods
mPATCH = http.MethodPatch
mGET = http.MethodGet
mPOST = http.MethodPost
mDELETE = http.MethodDelete
mPUT = http.MethodPut
)
// Gandi makes it easier to interact with Gandi LiveDNS
type Gandi struct {
apikey string
sharing_id string
debug bool
}
// New instantiates a new Gandi instance
func New(apikey string, sharing_id string) *Gandi {
return &Gandi{apikey: apikey, sharing_id: sharing_id}
}
func (g *Gandi) askGandi(method, path string, params, recipient interface{}) (http.Header, error) {
marshalledParams, err := json.Marshal(params)
if err != nil {
return nil, err
}
resp, err := g.doAskGandi(method, path, marshalledParams, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
decoder.Decode(recipient)
return resp.Header, nil
}
func (g *Gandi) askGandiToBytes(method, path string, params interface{}) (http.Header, []byte, error) {
headers := [][2]string{
[2]string{"Accept", "text/plain"},
}
marshalledParams, err := json.Marshal(params)
if err != nil {
return nil, nil, err
}
resp, err := g.doAskGandi(method, path, marshalledParams, headers)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
return resp.Header, content, err
}
func (g *Gandi) askGandiFromBytes(method, path string, params []byte, recipient interface{}) (http.Header, error) {
resp, err := g.doAskGandi(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
}
func (g *Gandi) doAskGandi(method, path string, params []byte, extraHeaders [][2]string) (*http.Response, error) {
var (
err error
req *http.Request
)
client := &http.Client{}
suffix := ""
if len(g.sharing_id) != 0 {
suffix += "?sharing_id=" + g.sharing_id
}
if params != nil && string(params) != "null" {
req, err = http.NewRequest(method, gandiEndpoint+path+suffix, bytes.NewReader(params))
} else {
req, err = http.NewRequest(method, gandiEndpoint+path+suffix, nil)
}
if err != nil {
return nil, err
}
req.Header.Add("X-Api-Key", g.apikey)
req.Header.Add("Content-Type", "application/json")
for _, header := range extraHeaders {
req.Header.Add(header[0], header[1])
}
if g.debug {
dump, _ := httputil.DumpRequestOut(req, true)
fmt.Println("=======================================\nREQUEST:")
fmt.Println(string(dump))
}
resp, err := client.Do(req)
if err != nil {
return resp, err
}
if g.debug {
dump, _ := httputil.DumpResponse(resp, true)
fmt.Println("=======================================\nREQUEST:")
fmt.Println(string(dump))
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
defer resp.Body.Close()
var message StandardResponse
defer resp.Body.Close()
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
}
// StandardResponse is a standard response
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"`
}
// StandardError is embedded in a standard error
type StandardError struct {
Location string `json:"location"`
Name string `json:"name"`
Description string `json:"description"`
}