forked from kamilakis/rgwspublic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
161 lines (132 loc) · 3.72 KB
/
request.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
package rgwspublic
import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
var (
ErrInvalidVAT = errors.New("invalid VAT format given")
ErrInvalidCredentials = errors.New("username or password cannot be less than 6 chars")
)
// Version gets web service version
// returns a string or an error
func Version() (*string, error) {
body := `<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:rgw="http://rgwspublic2/RgWsPublic2Service">
<soap:Header/>
<soap:Body>
<rgw:rgWsPublic2VersionInfo/>
</soap:Body>
</soap:Envelope>`
req, err := http.NewRequest("POST", Endpoint, strings.NewReader(body))
if err != nil {
return nil, err
}
header := http.Header{}
header.Set("Content-Type", "application/soap+xml")
header.Set("Connection", "keep-alive")
header.Set("Content-Length", strconv.Itoa(len(body)))
req.Header = header
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP Status: %d, error: %s", resp.StatusCode, resp.Status)
}
// parse response
xmlBody, err := parseXML(resp)
if err != nil {
return nil, err
}
err = xmlBody.error()
if err != nil {
return nil, err
}
xmlBody.Error = nil // to correct parser creating an object
return xmlBody.Version, nil
}
// AFMInfo gets info associated with a VAT number
// accepts a called by VAT and a called for VAT, username and password
// returns AFMData or an error
func GetVATInfo(calledfor, user, pass string) (*VATInfo, error) {
// vat number must be between 9 and 12 chars
if len(calledfor) < 9 || len(calledfor) > 12 {
return nil, ErrInvalidVAT
}
// same for username/password
if len(user) < 6 || len(pass) < 6 {
return nil, ErrInvalidCredentials
}
body := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope
xmlns:env="http://www.w3.org/2003/05/soap-envelope"
xmlns:ns1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:ns2="http://rgwspublic2/RgWsPublic2Service"
xmlns:ns3="http://rgwspublic2/RgWsPublic2">
<env:Header>
<ns1:Security>
<ns1:UsernameToken>
<ns1:Username>%v</ns1:Username>
<ns1:Password>%v</ns1:Password>
</ns1:UsernameToken>
</ns1:Security>
</env:Header>
<env:Body>
<ns2:rgWsPublic2AfmMethod>
<ns2:INPUT_REC>
<ns3:afm_called_by/>
<ns3:afm_called_for>%v</ns3:afm_called_for>
</ns2:INPUT_REC>
</ns2:rgWsPublic2AfmMethod>
</env:Body>
</env:Envelope>`, user, pass, calledfor)
req, err := http.NewRequest("POST", Endpoint, strings.NewReader(body))
if err != nil {
return nil, err
}
header := http.Header{}
header.Set("Content-Type", "application/soap+xml")
header.Set("Connection", "keep-alive")
header.Set("Content-Length", strconv.Itoa(len(body)))
req.Header = header
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP Status: %d, error: %s", resp.StatusCode, resp.Status)
}
xmlBody, err := parseXML(resp)
if err != nil {
return nil, err
}
err = xmlBody.VATInfo.error()
if err != nil {
return nil, err
}
// to correct parser creating an object
xmlBody.VATInfo.Error = nil
return &xmlBody.VATInfo, nil
}
// helper function to parse xml response
func parseXML(r *http.Response) (*XMLBody, error) {
rbody, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
xmlResp := XMLResponse{}
err = xml.Unmarshal([]byte(rbody), &xmlResp)
if err != nil {
return nil, err
}
return &xmlResp.Body, nil
}