forked from hooklift/gowsdl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
soap_tmpl.go
141 lines (116 loc) · 2.87 KB
/
soap_tmpl.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
package gowsdl
var soapTmpl = `
var timeout = time.Duration(30 * time.Second)
func dialTimeout(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, timeout)
}
type SoapEnvelope struct {
XMLName xml.Name `+"`"+`xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`+"`"+`
Body SoapBody `+"`"+`xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`+"`"+`
}
type SoapHeader struct {
Header interface{}
}
type SoapBody struct {
Fault *SoapFault `+"`"+`xml:"http://schemas.xmlsoap.org/soap/envelope/ Fault,omitempty"`+"`"+`
Content string `+"`"+`xml:",innerxml"`+"`"+`
}
type SoapFault struct {
Faultcode string `+"`"+`xml:"faultcode,omitempty"`+"`"+`
Faultstring string `+"`"+`xml:"faultstring,omitempty"`+"`"+`
Faultactor string `+"`"+`xml:"faultactor,omitempty"`+"`"+`
Detail string `+"`"+`xml:"detail,omitempty"`+"`"+`
}
type BasicAuth struct {
Login string
Password string
}
type SoapClient struct {
url string
tls bool
auth *BasicAuth
}
func (f *SoapFault) Error() string {
return f.Faultstring
}
func NewSoapClient(url string, tls bool, auth *BasicAuth) *SoapClient {
return &SoapClient{
url: url,
tls: tls,
auth: auth,
}
}
func (s *SoapClient) Call(soapAction string, request, response interface{}) error {
envelope := SoapEnvelope{
//Header: SoapHeader{},
}
if request != nil {
reqXml, err := xml.Marshal(request)
if err != nil {
return err
}
envelope.Body.Content = string(reqXml)
}
buffer := &bytes.Buffer{}
encoder := xml.NewEncoder(buffer)
//encoder.Indent(" ", " ")
err := encoder.Encode(envelope)
if err == nil {
err = encoder.Flush()
}
log.Println(buffer.String())
if err != nil {
return err
}
req, err := http.NewRequest("POST", s.url, buffer)
if s.auth != nil {
req.SetBasicAuth(s.auth.Login, s.auth.Password)
}
req.Header.Add("Content-Type", "text/xml; charset=\"utf-8\"")
if soapAction != "" {
req.Header.Add("SOAPAction", soapAction)
}
req.Header.Set("User-Agent", "gowsdl/0.1")
req.Close = true
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: s.tls,
},
Dial: dialTimeout,
}
client := &http.Client{Transport: tr}
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
rawbody, err := ioutil.ReadAll(res.Body)
if len(rawbody) == 0 {
log.Println("empty response")
return nil
}
log.Println(string(rawbody))
respEnvelope := &SoapEnvelope{}
err = xml.Unmarshal(rawbody, respEnvelope)
if err != nil {
return err
}
body := respEnvelope.Body.Content
fault := respEnvelope.Body.Fault
if body == "" {
log.Println("empty response body", "envelope", respEnvelope, "body", body)
return nil
}
log.Println("response", "envelope", respEnvelope, "body", body)
if fault != nil {
return fault
}
err = xml.Unmarshal([]byte(body), response)
if err != nil {
return err
}
return nil
}
func main() {
}
`