-
Notifications
You must be signed in to change notification settings - Fork 14
/
response.go
81 lines (69 loc) · 1.6 KB
/
response.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
package goat
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
type ResponseEnvelope struct {
XMLName xml.Name `xml:"Envelope"`
Header struct {
XMLName xml.Name `xml:"Header"`
Data []byte `xml:",innerxml"`
}
Body struct {
XMLName xml.Name `xml:"Body"`
Data []byte `xml:",innerxml"`
}
}
func (self *Webservice) NewRequest(service, method string, params map[string]interface{}, buf io.Writer) (err error) {
s := self.services[service]
if s == nil {
err = fmt.Errorf("no such service '%s'", service)
return
}
err = s.WriteRequest(method, buf, self.header, params)
return
}
func (self *Webservice) SendBuffer(service string, res interface{}, buf io.Reader) (err error) {
s := self.services[service]
if s == nil {
err = fmt.Errorf("no such service '%s'", service)
return
}
var resp *http.Response
resp, err = self.Client.Post(s.Service.Port.Address.Location, "application/soap+xml", buf)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
var b []byte
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = errors.New(string(b))
return
}
e := new(ResponseEnvelope)
err = xml.NewDecoder(io.TeeReader(resp.Body, os.Stdout)).Decode(e)
if err != nil {
return
}
err = xml.Unmarshal(e.Body.Data, res)
return
}
func (self *Webservice) Do(service, method string, res interface{}, params map[string]interface{}) (err error) {
buf := new(bytes.Buffer)
err = self.NewRequest(service, method, params, buf)
if err != nil {
return
}
err = self.SendBuffer(service, res, buf)
return
}