-
Notifications
You must be signed in to change notification settings - Fork 3
/
soap.go
78 lines (62 loc) · 1.3 KB
/
soap.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
package rope
import (
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
)
func StdEnvelope(body string) string {
t := `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
%s
</soapenv:Body>
</soapenv:Envelope>`
return fmt.Sprintf(t, body)
}
type Service interface {
Endpoint() string
RequestTemplate() string
RequestBody() string
}
type BasicAuth struct {
Username string
Password string
}
type SoapClient struct {
UseTLS bool
Auth *BasicAuth
}
func (sc *SoapClient) SendServiceRequest(service Service) (string, error) {
return sc.SendRequest(service.RequestBody(), service.Endpoint())
}
func (sc *SoapClient) SendRequest(body, url string) (string, error) {
bodyBytes := []byte(body)
bytebuffer := bytes.NewBuffer(bodyBytes)
c := &http.Client{}
if sc.UseTLS {
tr := &http.Transport{
TLSClientConfig: &tls.Config{},
}
c.Transport = tr
}
req, err := http.NewRequest("POST", url, bytebuffer)
if err != nil {
return "", err
}
if sc.Auth != nil {
req.SetBasicAuth(sc.Auth.Username, sc.Auth.Password)
}
resp, err := c.Do(req)
if err != nil {
return "", err
} else {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
}