-
Notifications
You must be signed in to change notification settings - Fork 0
/
manatee.go
80 lines (62 loc) · 1.43 KB
/
manatee.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
package manatee
import (
"encoding/xml"
"io/ioutil"
"net/http"
"github.com/hooklift/gowsdl/soap"
"github.com/hooklift/gowsdl"
)
// Manatee is a soap client.
type Manatee struct {
wsdlURL string
wsdl *gowsdl.WSDL
client *soap.Client
}
// Call sends the requests data by calling SOAP action
func (c *Manatee) Call(soapAction string, request, response interface{}) error {
err := c.init()
if err != nil {
return err
}
return c.client.Call(c.getActionEndpoint(soapAction), request, response)
}
func (c *Manatee) init() error {
err := c.parseWSDL()
if err != nil {
return err
}
c.client = soap.NewClient(c.getServiceURL())
return nil
}
func (c *Manatee) parseWSDL() error {
if c.wsdl == nil {
res, err := http.Get(c.wsdlURL)
if err != nil {
return err
}
defer res.Body.Close()
byteValue, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
return xml.Unmarshal(byteValue, &c.wsdl)
}
return nil
}
func (c *Manatee) getServiceURL() string {
return c.wsdl.Service[0].Ports[0].SOAPAddress.Location
}
func (c *Manatee) getActionEndpoint(action string) string {
for _, binding := range c.wsdl.Binding {
for _, operation := range binding.Operations {
if operation.Name == action {
return operation.SOAPOperation.SOAPAction
}
}
}
return ""
}
// NewClient generates a new manatee client with given wsdlURL url.
func NewClient(wsdl string) *Manatee {
return &Manatee{wsdlURL: wsdl}
}