-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathperformance_client_request.go
84 lines (77 loc) · 3.11 KB
/
performance_client_request.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
82
83
84
package main
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"regexp"
"time"
)
type FaultResponse struct {
XMLName xml.Name `xml:"Fault"`
Text string `xml:",chardata"`
FaultCode string `xml:"faultcode"`
FaultString string `xml:"faultstring"`
Detail string `xml:"detail"`
}
// perfRequestCreate generate http request wit request ID and body
// - request to https://<API server>:8443/perfmonservice2/services/PerfmonService?wsdl
func perfRequestCreate(requestId string, body string) (req *http.Request, err error) {
log.WithFields(log.Fields{FieldRoutine: "perfRequestCreate", FieldRequestId: requestId}).Trace("prepare request")
if LogRequestDuration {
defer duration(track(log.Fields{FieldRoutine: "perfRequestCreate", FieldRequestId: requestId}, "procedure ends"))
}
server := fmt.Sprintf("https://%s:8443/perfmonservice2/services/PerfmonService?wsdl", config.ApiAddress)
log.WithFields(log.Fields{FieldRoutine: "perfRequestCreate", FieldRequestId: requestId}).Tracef("prepare server API name: %s", server)
req, err = http.NewRequest("POST", server, bytes.NewBuffer([]byte(body)))
if err != nil {
log.WithField(FieldRoutine, "perfRequestCreate").Errorf("problem create request. Error: %s", err)
return nil, err
}
req.Header.Add("User-Agent", httpApplicationName())
req.Header.Add("Content-Type", "text/xml")
req.Header.Add("Accept", "text/xml")
req.Header.Add("Cache-Control", "no-cache")
req.SetBasicAuth(config.ApiUser, config.ApiPassword)
return req, nil
}
func perfRequestResponse(requestId string, client *http.Client, req *http.Request) (body string, resp *http.Response, err error) {
log.WithFields(log.Fields{FieldRoutine: "perfRequestResponse", FieldRequestId: requestId}).Trace("get response")
if LogRequestDuration {
defer duration(track(log.Fields{FieldRoutine: "perfRequestResponse", FieldRequestId: requestId}, "procedure ends"))
}
requestsCount := rateRequest.requests
waitTime := rateRequest.delay()
if waitTime > time.Millisecond {
if waitTime > RateStandardDelay {
log.WithFields(log.Fields{FieldRoutine: "perfRequestResponse", FieldRequestId: requestId}).
Warnf("wait after %d requests for %s", requestsCount, waitTime.String())
} else {
log.WithFields(log.Fields{FieldRoutine: "perfRequestResponse", FieldRequestId: requestId}).
Debugf("wait after %d requests for %s", requestsCount, waitTime.String())
}
time.Sleep(waitTime)
}
resp, err = client.Do(req)
if err != nil {
log.WithFields(log.Fields{FieldRoutine: "perfRequestResponse", FieldRequestId: requestId}).Errorf("problem process request. Error: %s", err)
return "", resp, err
}
s, err := io.ReadAll(resp.Body)
return string(s), resp, err
}
func perfRequestBodyRelevant(body string) (data string, err error) {
var rex = regexp.MustCompile(`(?m)<soapenv:Body>((.|\n)*?)</soapenv:Body>`)
if !rex.Match([]byte(body)) {
return "", errors.New("response body not contains \"<soapenv:Body>\"")
}
x := rex.FindStringSubmatch(body)
if len(x) < 2 {
return "", errors.New("response body not contains \"<soapenv:Body>\"")
}
data = x[1]
return data, nil
}