finclip-app-manager/infrastructure/client/httpcall/base.go

124 lines
2.9 KiB
Go
Raw Permalink Normal View History

2023-10-31 14:07:26 +08:00
package httpcall
//
//import (
// "bytes"
// "context"
// "gitlab.finogeeks.club/finclip-backend/apm"
// "io/ioutil"
// "net"
// "net/http"
// "time"
//)
//
//const (
// FakeStatusCode = 666
//)
//
//type ClientBase struct {
// RequestHeader map[string]string
// Method string
// URL string
// RequestBody []byte
// StatusCode int
// ResponseBody []byte
// Service string
// NotLogRequestBody bool
// NotLogResponseBody bool
//}
//
//var defaultClient = ClientBase{
// RequestHeader: map[string]string{
// "Accept": "application/json, text/plain, */*",
// "Content-Type": "application/json",
// "url-call": "internal",
// },
// Method: "GET",
//}
//
//func (b *ClientBase) Fetch(ctx context.Context) error {
// if b.Method == "" {
// b.Method = defaultClient.Method
// }
// if b.RequestHeader == nil {
// b.RequestHeader = defaultClient.RequestHeader
// }
// client := &http.Client{
// Transport: &http.Transport{
// Dial: (&net.Dialer{
// Timeout: 30 * time.Second,
// KeepAlive: 30 * time.Second,
// }).Dial,
// TLSHandshakeTimeout: 10 * time.Second,
// ResponseHeaderTimeout: 10 * time.Second,
// ExpectContinueTimeout: 1 * time.Second,
// DisableKeepAlives: true,
// },
// Timeout: 30 * time.Second,
// }
// req, err := http.NewRequest(b.Method, b.URL, bytes.NewReader(b.RequestBody))
// if err != nil {
// return err
// }
// for k, v := range b.RequestHeader {
// req.Header.Set(k, v)
// }
// //b.LogRequest()
// span := apm.ApmClient().CreateHttpExitSpan(ctx, req, req.URL.Host, req.URL.Path)
// defer span.End()
// resp, err := client.Do(req)
// if err != nil {
// return err
// }
// defer resp.Body.Close()
// body, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// // Used for log.
// b.StatusCode = FakeStatusCode
// b.ResponseBody = nil
// return err
// }
// b.StatusCode = resp.StatusCode
// b.ResponseBody = body
// return nil
//}
//
////func (b *ClientBase) LogRequest() {
//// kv := log.KeysAndValues{
//// "Service", b.Service,
//// "Method", b.Method,
//// "URL", b.URL,
//// "Request Header", b.RequestHeader,
//// }
//// if !b.NotLogRequestBody {
//// kv = append(kv, "Request Body", string(b.RequestBody))
//// }
//// log.Infow("Ready to fetch", kv)
////}
////
////func (b *ClientBase) LogError(err error) {
//// kv := log.KeysAndValues{
//// "Service", b.Service,
//// "Method", b.Method,
//// "URL", b.URL,
//// "Request Header", b.RequestHeader,
//// "Status Code", b.StatusCode,
//// }
//// if !b.NotLogRequestBody {
//// kv = append(kv, "Request Body", string(b.RequestBody))
//// }
//// if !b.NotLogResponseBody {
//// kv = append(kv, "Response Body", string(b.ResponseBody))
//// }
//// if err == nil {
//// log.Errorw("Unexpected status code", kv)
//// } else {
//// kv = append(kv, "err", err)
//// log.Errorw("Fetch error", kv)
//// }
////}
//
//func (b *ClientBase) AddHeader(k string, v string) {
// b.RequestHeader[k] = v
//}