-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccx-response.go
68 lines (60 loc) · 1.44 KB
/
ccx-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
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"strings"
)
type CcxResponse struct {
id string
url string
response *http.Response
err error
lastMessage string
body string
bodyBytes []byte
statusCode int
statusMessage string
}
func (r *CcxResponse) close() {
if r.response != nil && r.response.Body != nil {
_ = r.response.Body.Close()
}
r.response = nil
}
func (r *CcxResponse) responseReturnData() error {
log.WithField("id", r.id).Tracef("response status is [%s]", r.response.Status)
bodies, err := ioutil.ReadAll(r.response.Body)
_ = r.response.Body.Close()
r.bodyBytes = bodies
r.body = string(bodies)
if err != nil {
log.WithField("id", r.id).Errorf("problem get body from response with message [%s].", err)
return err
}
if strings.Contains(strings.ToLower(r.body), "<html>") {
r.err = fmt.Errorf("CCX service is down")
r.body = ""
return r.err
}
if log.GetLevel() == log.TraceLevel {
r.storeResponse()
}
log.WithField("id", r.id).Tracef("success read [%d] chars of body", len(r.body))
return nil
}
func (r *CcxResponse) GetResponseBody() (string, error) {
if r.response == nil {
return r.body, nil
}
err := r.responseReturnData()
if err != nil {
r.err = err
}
return r.body, r.err
}
func (r *CcxResponse) storeResponse() {
name := fmt.Sprintf("./log/ccx.%s.log", r.id)
_ = ioutil.WriteFile(name, r.bodyBytes, 0644)
}