-
Notifications
You must be signed in to change notification settings - Fork 0
/
axl-response.go
78 lines (69 loc) · 1.69 KB
/
axl-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
package main
import (
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"strings"
)
type AxlResponse struct {
id string
url string
response *http.Response
err error
lastMessage string
body string
statusCode int
statusMessage string
}
func (a *AxlResponse) Close() {
if a.response != nil && a.response.Body != nil {
_ = a.response.Body.Close()
}
a.response = nil
}
func (a *AxlResponse) responseReturnData() error {
log.WithField("id", a.id).Debugf("AXL response status [%s]", a.response.Status)
bodies, err := ioutil.ReadAll(a.response.Body)
_ = a.response.Body.Close()
a.body = ""
if err != nil {
log.WithField("id", a.id).Errorf("problem get AXL body from response - [%s]", err)
return err
}
if a.statusCode > 299 {
a.getFailurePart(string(bodies))
} else {
a.getReturnPart(string(bodies))
}
log.WithField("id", a.id).Trace("Body read success")
return nil
}
func (a *AxlResponse) getReturnPart(body string) {
a.getBetween(body, "<return>", "</return>", "<return/>")
}
func (a *AxlResponse) getFailurePart(body string) {
a.getBetween(body, "<soapenv:Fault>", "</soapenv:Fault>", "<soapenv:Fault/>")
}
func (a *AxlResponse) getBetween(body string, start string, end string, short string) {
if strings.Contains(body, start) {
body = body[strings.Index(body, start):]
} else if strings.Index(body, short) > -1 {
a.body = short
return
}
a.body = ""
if !strings.Contains(body, end) {
return
}
a.body = body[:strings.Index(body, end)] + end
}
func (a *AxlResponse) GetResponseBody() string {
if a.response == nil {
return a.body
}
err := a.responseReturnData()
if err != nil {
a.err = err
}
return a.body
}