Skip to content

Commit d2108bc

Browse files
committed
Refactor code
1 parent 04e81e4 commit d2108bc

File tree

1 file changed

+182
-7
lines changed

1 file changed

+182
-7
lines changed

client.go

Lines changed: 182 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,36 +255,36 @@ func GetTLSClientConfig(clientCert tls.Certificate, options ...string) (*tls.Con
255255
}
256256
return c, nil
257257
}
258-
func DoRequest(ctx context.Context, client *http.Client, method string, url string, body []byte, headers map[string]string) (*http.Response, error) {
258+
func DoJSON(ctx context.Context, client *http.Client, method string, url string, body []byte, headers map[string]string) (*http.Response, error) {
259259
if body != nil {
260260
b := body
261261
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewBuffer(b))
262262
if err != nil {
263263
return nil, err
264264
}
265-
return AddHeaderAndDo(client, req, headers)
265+
return AddHeaderAndDoJSON(client, req, headers)
266266
} else {
267267
req, err := http.NewRequestWithContext(ctx, method, url, nil)
268268
if err != nil {
269269
return nil, err
270270
}
271-
return AddHeaderAndDo(client, req, headers)
271+
return AddHeaderAndDoJSON(client, req, headers)
272272
}
273273
}
274-
func DoJSON(ctx context.Context, client *http.Client, method string, url string, body []byte, headers map[string]string) (*http.Response, error) {
274+
func DoRequest(ctx context.Context, client *http.Client, method string, url string, body []byte, headers map[string]string) (*http.Response, error) {
275275
if body != nil {
276276
b := body
277277
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewBuffer(b))
278278
if err != nil {
279279
return nil, err
280280
}
281-
return AddHeaderAndDoJSON(client, req, headers)
281+
return AddHeaderAndDo(client, req, headers)
282282
} else {
283283
req, err := http.NewRequestWithContext(ctx, method, url, nil)
284284
if err != nil {
285285
return nil, err
286286
}
287-
return AddHeaderAndDoJSON(client, req, headers)
287+
return AddHeaderAndDo(client, req, headers)
288288
}
289289
}
290290
func DoJSONWithClient(ctx context.Context, client *http.Client, method string, url string, obj interface{}, headers map[string]string, errorStatus int) (*json.Decoder, error) {
@@ -486,7 +486,7 @@ func DoAndBuildDecoder(ctx context.Context, client *http.Client, method string,
486486
end := time.Now()
487487
dur := end.Sub(start).Milliseconds()
488488
if logError != nil && (er1 != nil || res.StatusCode >= 400) {
489-
fs3 := make(map[string]interface{}, 0)
489+
fs3 := make(map[string]interface{})
490490
var c2 LogConfig
491491
if conf != nil {
492492
c2 = *conf
@@ -823,6 +823,181 @@ func DoAndLog(ctx context.Context, client *http.Client, method string, url strin
823823
}
824824
}
825825

826+
func DoAndLogCommon(ctx context.Context, client *http.Client, method string, url string, body []byte, headers map[string]string, conf *LogConfig, options ...func(context.Context, string, map[string]interface{})) (*http.Response, error) {
827+
var logError func(context.Context, string, map[string]interface{})
828+
var logInfo func(context.Context, string, map[string]interface{})
829+
if len(options) > 0 {
830+
logError = options[0]
831+
}
832+
if len(options) > 1 {
833+
logInfo = options[1]
834+
}
835+
start := time.Now()
836+
res, er1 := DoRequest(ctx, client, method, url, body, headers)
837+
end := time.Now()
838+
dur := end.Sub(start).Milliseconds()
839+
if logError != nil && (er1 != nil || res.StatusCode >= 400) {
840+
fs3 := make(map[string]interface{}, 0)
841+
var c2 LogConfig
842+
if conf != nil {
843+
c2 = *conf
844+
} else {
845+
c2.Duration = "duration"
846+
c2.Request = "request"
847+
c2.Response = "response"
848+
c2.ResponseStatus = "status"
849+
c2.Error = "error"
850+
}
851+
if body != nil {
852+
rq := string(body)
853+
if len(rq) > 0 {
854+
fs3[c2.Request] = rq
855+
}
856+
}
857+
if er1 != nil {
858+
if len(c2.Error) > 0 {
859+
fs3[c2.Error] = er1.Error()
860+
}
861+
logError(ctx, method+" "+url, fs3)
862+
return res, er1
863+
}
864+
if len(c2.ResponseStatus) > 0 {
865+
fs3[c2.ResponseStatus] = res.StatusCode
866+
}
867+
if len(c2.Size) > 0 && res.ContentLength > 0 {
868+
fs3[c2.Size] = res.ContentLength
869+
}
870+
if len(c2.Response) > 0 {
871+
dump, er3 := httputil.DumpResponse(res, true)
872+
if er3 != nil {
873+
if len(c2.Error) > 0 {
874+
fs3[c2.Error] = er3.Error()
875+
}
876+
logError(ctx, method+" "+url, fs3)
877+
return res, er3
878+
}
879+
s := string(dump)
880+
if len(c2.Size) > 0 {
881+
fs3[c2.Size] = len(s)
882+
}
883+
if len(c2.Response) > 0 {
884+
fs3[c2.Response] = s
885+
}
886+
if res.StatusCode == 503 {
887+
logError(ctx, method+" "+url, fs3)
888+
var rq string
889+
if body != nil {
890+
rq = string(body)
891+
}
892+
er2 := NewHttpError(http.StatusServiceUnavailable, er1, dur, "503 Service Unavailable", url, rq, s)
893+
return res, er2
894+
}
895+
logError(ctx, method+" "+url, fs3)
896+
return res, nil
897+
} else {
898+
if res.StatusCode == 503 {
899+
logError(ctx, method+" "+url, fs3)
900+
var rq string
901+
if body != nil {
902+
rq = string(body)
903+
}
904+
er2 := NewHttpError(http.StatusServiceUnavailable, er1, dur, "503 Service Unavailable", url, rq)
905+
return res, er2
906+
}
907+
logError(ctx, method+" "+url, fs3)
908+
return res, nil
909+
}
910+
}
911+
if conf != nil && conf.Log == true && logInfo != nil {
912+
canRequest := false
913+
if method != "GET" && method != "DELETE" && method != "OPTIONS" {
914+
canRequest = true
915+
}
916+
if conf.Separate && len(conf.Request) > 0 && body != nil && canRequest {
917+
fs1 := make(map[string]interface{}, 0)
918+
rq := string(body)
919+
if len(rq) > 0 {
920+
fs1[conf.Request] = rq
921+
}
922+
logInfo(ctx, method+" "+url, fs1)
923+
}
924+
fs3 := make(map[string]interface{}, 0)
925+
fs3[conf.Duration] = dur
926+
if !conf.Separate && len(conf.Request) > 0 && body != nil && canRequest {
927+
rq := string(body)
928+
if len(rq) > 0 {
929+
fs3[conf.Request] = rq
930+
}
931+
}
932+
if er1 != nil {
933+
if len(conf.Error) > 0 {
934+
fs3[conf.Error] = er1.Error()
935+
}
936+
logInfo(ctx, method+" "+url, fs3)
937+
return res, er1
938+
}
939+
if len(conf.ResponseStatus) > 0 {
940+
fs3[conf.ResponseStatus] = res.StatusCode
941+
}
942+
if len(conf.Size) > 0 && res.ContentLength > 0 {
943+
fs3[conf.Size] = res.ContentLength
944+
}
945+
if len(conf.Response) > 0 {
946+
dump, er3 := httputil.DumpResponse(res, true)
947+
if er3 != nil {
948+
if len(conf.Error) > 0 {
949+
fs3[conf.Error] = er3.Error()
950+
}
951+
logInfo(ctx, method+" "+url, fs3)
952+
return res, er3
953+
}
954+
s := string(dump)
955+
if len(conf.Size) > 0 {
956+
fs3[conf.Size] = len(s)
957+
}
958+
if len(conf.Response) > 0 {
959+
fs3[conf.Response] = s
960+
}
961+
if res.StatusCode == 503 {
962+
logInfo(ctx, method+" "+url, fs3)
963+
var rq string
964+
if body != nil {
965+
rq = string(body)
966+
}
967+
er2 := NewHttpError(http.StatusServiceUnavailable, er1, dur, "503 Service Unavailable", url, rq, s)
968+
return res, er2
969+
}
970+
logInfo(ctx, method+" "+url, fs3)
971+
return res, nil
972+
} else {
973+
if res.StatusCode == 503 {
974+
logInfo(ctx, method+" "+url, fs3)
975+
var rq string
976+
if body != nil {
977+
rq = string(body)
978+
}
979+
er2 := NewHttpError(http.StatusServiceUnavailable, er1, dur, "503 Service Unavailable", url, rq)
980+
return res, er2
981+
}
982+
logInfo(ctx, method+" "+url, fs3)
983+
return res, nil
984+
}
985+
} else {
986+
if er1 != nil {
987+
return nil, er1
988+
}
989+
if res.StatusCode == 503 {
990+
var rq string
991+
if body != nil {
992+
rq = string(body)
993+
}
994+
er2 := NewHttpError(http.StatusServiceUnavailable, er1, dur, "503 Service Unavailable", url, rq)
995+
return res, er2
996+
}
997+
return res, nil
998+
}
999+
}
1000+
8261001
type HttpError struct {
8271002
StatusCode int
8281003
ErrorMessage string

0 commit comments

Comments
 (0)