-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
230 lines (205 loc) · 4.62 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package opensrs
import (
"bytes"
"crypto/md5"
"encoding/xml"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
)
type XCPClient struct {
url string
username string
privateKey string
}
type NestedStringMap map[string]interface{}
func serializeItem(k string, v interface{}, w io.Writer) {
w.Write([]byte(`<item key="`))
xml.EscapeText(w, []byte(k))
w.Write([]byte(`">`))
switch i := v.(type) {
case string:
xml.EscapeText(w, []byte(i))
case NestedStringMap:
serializeMap(i, w)
case []NestedStringMap:
serializeNSMArray(i, w)
case []string:
serializeStringArray(i, w)
}
w.Write([]byte(`</item>`))
}
func serializeNSMArray(nsm []NestedStringMap, w io.Writer) {
w.Write([]byte(`<dt_array>`))
for i, v := range nsm {
serializeItem(strconv.Itoa(i), v, w)
}
w.Write([]byte(`</dt_array>`))
}
func serializeStringArray(nsm []string, w io.Writer) {
w.Write([]byte(`<dt_array>`))
for i, v := range nsm {
serializeItem(strconv.Itoa(i), v, w)
}
w.Write([]byte(`</dt_array>`))
}
func serializeMap(nsm NestedStringMap, w io.Writer) {
w.Write([]byte(`<dt_assoc>`))
for k, v := range nsm {
serializeItem(k, v, w)
}
w.Write([]byte(`</dt_assoc>`))
}
func writeXMLMessage(nsm NestedStringMap, w io.Writer) {
w.Write([]byte(`<?xml version="1.0" encoding="UTF-8" standalone='yes'?>
<!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
<OPS_envelope>
<header>
<version>0.9</version>
</header>
<body>
<data_block>
`))
serializeMap(nsm, w)
w.Write([]byte(`</data_block>
</body>
</header>`))
}
func NewXCPClient(url string, username string, privateKey string) *XCPClient {
xcp := XCPClient{
url: url,
username: username,
privateKey: privateKey,
}
return &xcp
}
func (c *XCPClient) createSignature(xml string) string {
x := md5.Sum([]byte(xml + c.privateKey))
y := md5.Sum([]byte(fmt.Sprintf("%x", x) + c.privateKey))
return fmt.Sprintf("%x", y)
}
func (c *XCPClient) doRequest(requestNSM NestedStringMap) (response *NestedStringMap, err error) {
var xmlRequest bytes.Buffer
writeXMLMessage(requestNSM, &xmlRequest)
request, err := http.NewRequest("POST", c.url, &xmlRequest)
if err != nil {
return nil, err
}
request.Header.Add("Content-Type", "text/xml")
request.Header.Add("X-Username", c.username)
request.Header.Add("X-Signature", c.createSignature(xmlRequest.String()))
request.ContentLength = int64(len(xmlRequest.String()))
res, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err
}
nsm, err := c.xmlResponseToNSM(res.Body)
return nsm, err
}
func (n NestedStringMap) getString(key string) (string, bool) {
path := strings.Split(key, "/")
last := path[len(path)-1]
path = path[:len(path)-1]
for _, item := range path {
var ok bool
n, ok = n[item].(NestedStringMap)
if !ok {
return "", false
}
}
s, ok := n[last].(string)
if !ok {
return "", false
}
return s, true
}
func (n NestedStringMap) getInteger(key string) (int, bool) {
s, ok := n.getString(key)
if !ok {
return 0, false
}
i, err := strconv.Atoi(s)
if err != nil {
return 0, false
}
return i, true
}
func (n NestedStringMap) getMap(key string) (*NestedStringMap, bool) {
path := strings.Split(key, "/")
for _, item := range path {
var ok bool
n, ok = n[item].(NestedStringMap)
if !ok {
return nil, false
}
}
return &n, true
}
func (c *XCPClient) xmlResponseToNSM(xmlr io.Reader) (*NestedStringMap, error) {
decoder := xml.NewDecoder(xmlr)
var root *NestedStringMap
var stack []*NestedStringMap
var currentKey string
var currentItem *NestedStringMap
var charData string
simple := false
for {
t, xmlerr := decoder.Token()
if t == nil {
break
}
if xmlerr != nil {
return nil, xmlerr
}
switch se := t.(type) {
case xml.StartElement:
switch se.Name.Local {
case "dt_assoc", "dt_array":
assoc := NestedStringMap{}
if currentItem != nil {
(*currentItem)[currentKey] = assoc
}
if root == nil {
root = &assoc
}
stack = append(stack, &assoc)
currentItem = &assoc
simple = false
case "item":
charData = ""
for _, k := range se.Attr {
if k.Name.Local == "key" {
currentKey = k.Value
}
}
simple = true
}
case xml.EndElement:
switch se.Name.Local {
case "dt_assoc", "dt_array":
stack = stack[:len(stack)-1]
if len(stack) > 0 {
currentItem = stack[len(stack)-1]
} else {
currentItem = nil
}
case "item":
if simple {
(*currentItem)[currentKey] = charData
}
}
simple = false
case xml.CharData:
if simple {
charData += string(se)
}
}
}
if root == nil {
return nil, errors.New("No associative array found in response")
}
return root, nil
}