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+
77125func recordAbRequestCostTime (response * http.Response , abRequestStartTime int64 , abRequestEndTime int64 ) {
78126 abRequestId := getAbRequestIdFromResponse (response )
79127 abRequestProcessTime := getAbRequestProcessTimeFromResponse (response )
0 commit comments