提交 81dc6145 authored 作者: mooncake9527's avatar mooncake9527

httpcli print cost

上级 6911bad0
......@@ -22,6 +22,7 @@ import (
const defaultTimeout = 30 * time.Second
type Request struct {
cli *http.Client
url string
method string
headers map[string]string
......@@ -44,8 +45,25 @@ type Response struct {
Header http.Header
}
// ----------------------------------- Request way 1 -----------------------------------
type Client struct {
client *http.Client
}
func NewClient(cli *http.Client) *Client {
return &Client{
client: cli,
}
}
func (x *Client) NewRequest() *Request {
return &Request{
cli: x.client,
response: &Response{},
}
}
// ----------------------------------- Request way 1 -----------------------------------
//
// New create a new Request
func New() *Request {
return &Request{
......@@ -289,7 +307,10 @@ func (x *Request) send(ctx context.Context) {
}
func (x *Request) pushDo(ctx context.Context) {
client := http.Client{Timeout: x.timeout}
client := x.cli
if client == nil {
client = &http.Client{Timeout: x.timeout}
}
var rsp *http.Response
rsp, x.err = client.Do(x.request)
if x.err != nil {
......@@ -403,128 +424,6 @@ func WithTimeout(t time.Duration) Option {
}
}
//
//// Get request, return custom json format
//func Get(ctx context.Context, result interface{}, urlStr string, opts ...Option) error {
// o := defaultOptions()
// o.apply(opts...)
// return gDo(ctx, "GET", result, urlStr, o.params, o.headers, o.timeout)
//}
//
//// Delete request, return custom json format
//func Delete(ctx context.Context, result interface{}, urlStr string, opts ...Option) error {
// o := defaultOptions()
// o.apply(opts...)
// return gDo(ctx, "DELETE", result, urlStr, o.params, o.headers, o.timeout)
//}
//
//// Post request, return custom json format
//func Post(ctx context.Context, result interface{}, urlStr string, body interface{}, opts ...Option) error {
// o := defaultOptions()
// o.apply(opts...)
// return do(ctx, "POST", result, urlStr, body, o.params, o.headers, o.timeout)
//}
//
//// Put request, return custom json format
//func Put(ctx context.Context, result interface{}, urlStr string, body interface{}, opts ...Option) error {
// o := defaultOptions()
// o.apply(opts...)
// return do(ctx, "PUT", result, urlStr, body, o.params, o.headers, o.timeout)
//}
//
//// Patch request, return custom json format
//func Patch(ctx context.Context, result interface{}, urlStr string, body interface{}, opts ...Option) error {
// o := defaultOptions()
// o.apply(opts...)
// return do(ctx, "PATCH", result, urlStr, body, o.params, o.headers, o.timeout)
//}
//
//var requestErr = func(err error) error { return fmt.Errorf("request error, err=%v", err) }
//var jsonParseErr = func(err error) error { return fmt.Errorf("json parsing error, err=%v", err) }
//var notOKErr = func(resp *Response) error {
// body, err := resp.ReadBody()
// if err != nil {
// return err
// }
// if len(body) > 500 {
// body = append(body[:500], []byte(" ......")...)
// }
// return fmt.Errorf("statusCode=%d, body=%s", resp.StatusCode, body)
//}
//
//func do(ctx context.Context, method string, result interface{}, urlStr string, body interface{}, params KV, headers map[string]string, timeout time.Duration) error {
// if result == nil {
// return fmt.Errorf("'result' can not be nil")
// }
//
// req := &Request{}
// req.SetURL(urlStr)
// req.SetContentType("application/json")
// req.SetParams(params)
// req.SetHeaders(headers)
// req.SetBody(body)
// req.SetTimeout(timeout)
//
// var resp *Response
// var err error
// switch method {
// case "POST":
// resp, err = req.POST(ctx)
// case "PUT":
// resp, err = req.PUT(ctx)
// case "PATCH":
// resp, err = req.PATCH(ctx)
// }
// if err != nil {
// return requestErr(err)
// }
// defer resp.Body.Close() //nolint
//
// if resp.StatusCode != 200 {
// return notOKErr(resp)
// }
//
// err = resp.BindJSON(result)
// if err != nil {
// return jsonParseErr(err)
// }
//
// return nil
//}
//
//func gDo(ctx context.Context, method string, result interface{}, urlStr string, params KV, headers map[string]string, timeout time.Duration) error {
// req := &Request{}
// req.SetURL(urlStr)
// req.SetParams(params)
// req.SetHeaders(headers)
// req.SetTimeout(timeout)
//
// var resp *Response
// var err error
// switch method {
// case "GET":
// resp, err = req.GET(ctx)
// case "DELETE":
// resp, err = req.DELETE(ctx)
// }
// if err != nil {
// return requestErr(err)
// }
// defer resp.Body.Close() //nolint
//
// if resp.StatusCode != 200 {
// return notOKErr(resp)
// }
//
// err = resp.BindJSON(result)
// if err != nil {
// return jsonParseErr(err)
// }
//
// return nil
//}
// StdResult standard return data
type StdResult struct {
Code int `json:"code"`
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论