Skip to content

Commit f767b40

Browse files
author
闫明
committed
Release 0.1.4
1 parent 54e97cf commit f767b40

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

sensors_abtesting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
const (
11-
SDK_VERSION = "0.1.3"
11+
SDK_VERSION = "0.1.4"
1212
LIB_NAME = "Golang"
1313
)
1414

utils/network.go

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"github.com/sensorsdata/abtesting-sdk-go/beans"
9+
"io"
910
"io/ioutil"
1011
"net"
1112
"net/http"
@@ -43,22 +44,60 @@ func RequestExperiment(url string, requestPrams map[string]interface{}, to time.
4344
client := &http.Client{Timeout: to, Transport: httpTransport}
4445
resp, err := client.Do(req)
4546

47+
if err != nil {
48+
return Response{}, err
49+
}
50+
51+
if resp == nil {
52+
return Response{}, fmt.Errorf("response is nil")
53+
}
54+
4655
if enableRecordRequestCostTime {
47-
abRequestEndTime := time.Now().UnixNano() / int64(time.Millisecond)
48-
recordAbRequestCostTime(resp, abRequestStartTime, abRequestEndTime)
56+
recordRequestCostTime(resp, abRequestStartTime)
57+
}
58+
59+
return processResponse(resp)
60+
}
61+
62+
func truncateBody(arr []byte, maxLen int) string {
63+
bodyStr := string(arr)
64+
if len(bodyStr) > maxLen {
65+
return bodyStr[:maxLen]
4966
}
67+
return bodyStr
68+
}
5069

70+
func processResponse(resp *http.Response) (Response, error) {
71+
defer func(Body io.ReadCloser) {
72+
err := Body.Close()
73+
if err != nil {
74+
fmt.Println("close body error: ", err)
75+
}
76+
}(resp.Body)
77+
78+
body, err := ioutil.ReadAll(resp.Body)
5179
if err != nil {
5280
return Response{}, err
5381
}
5482

55-
defer resp.Body.Close()
56-
body, _ := ioutil.ReadAll(resp.Body)
83+
if !isStatusCodeValid(resp.StatusCode) {
84+
return Response{}, fmt.Errorf("response status code is not valid, status code: %d, response: %s", resp.StatusCode, truncateBody(body, 200))
85+
}
86+
5787
response := Response{}
5888
var resMaps map[string]interface{}
89+
5990
err = json.Unmarshal(body, &response)
91+
if err != nil {
92+
return Response{}, err
93+
}
94+
6095
err = json.Unmarshal(body, &resMaps)
61-
if err == nil && response.Status == "SUCCESS" {
96+
if err != nil {
97+
return Response{}, err
98+
}
99+
100+
if response.Status == "SUCCESS" {
62101
if !strings.Contains(string(body), "track_config") {
63102
response.TrackConfig = beans.TrackConfig{
64103
ItemSwitch: false,
@@ -70,10 +109,19 @@ func RequestExperiment(url string, requestPrams map[string]interface{}, to time.
70109
defaultTrackConfig(&response, resMaps)
71110
return response, nil
72111
} else {
73-
return Response{}, errors.New(string(response.Error))
112+
return Response{}, errors.New(response.Error)
74113
}
75114
}
76115

116+
func recordRequestCostTime(resp *http.Response, abRequestStartTime int64) {
117+
abRequestEndTime := time.Now().UnixNano() / int64(time.Millisecond)
118+
recordAbRequestCostTime(resp, abRequestStartTime, abRequestEndTime)
119+
}
120+
121+
func isStatusCodeValid(statusCode int) bool {
122+
return statusCode >= 200 && statusCode <= 299
123+
}
124+
77125
func recordAbRequestCostTime(response *http.Response, abRequestStartTime int64, abRequestEndTime int64) {
78126
abRequestId := getAbRequestIdFromResponse(response)
79127
abRequestProcessTime := getAbRequestProcessTimeFromResponse(response)

0 commit comments

Comments
 (0)