From 2ed752e4ce2d376b3939a399f138d995d6bcc491 Mon Sep 17 00:00:00 2001 From: Jerry <85411418@qq.com> Date: Thu, 2 Nov 2023 15:24:26 +0800 Subject: [PATCH] update xhttp client --- pkg/xhttp/client.go | 42 +++++++++++++++++++++++---------------- pkg/xhttp/model.go | 47 ++++++++++++++++++++++++-------------------- pkg/xhttp/request.go | 18 ++++++++--------- 3 files changed, 60 insertions(+), 47 deletions(-) diff --git a/pkg/xhttp/client.go b/pkg/xhttp/client.go index 93fdfad5..2a655224 100644 --- a/pkg/xhttp/client.go +++ b/pkg/xhttp/client.go @@ -12,9 +12,8 @@ type Client struct { bodySize int // body size limit(MB), default is 10MB } -// NewClient , default tls.Config{InsecureSkipVerify: true} -func NewClient() (client *Client) { - client = &Client{ +func defaultClient() *Client { + return &Client{ HttpClient: &http.Client{ Timeout: 60 * time.Second, Transport: &http.Transport{ @@ -35,7 +34,11 @@ func NewClient() (client *Client) { }, bodySize: 10, // default is 10MB } - return client +} + +// NewClient , default tls.Config{InsecureSkipVerify: true} +func NewClient() (client *Client) { + return defaultClient() } func (c *Client) SetTransport(transport *http.Transport) (client *Client) { @@ -59,38 +62,43 @@ func (c *Client) SetBodySize(sizeMB int) (client *Client) { return c } -func (c *Client) Req(typeStr ...RequestType) *Request { +// typeStr is request type and response type +// default is TypeJSON +// first param is request type +// second param is response data type +func (c *Client) Req(typeStr ...string) *Request { var ( - reqTp = TypeJSON // default - resTp = TypeJSON // default + reqTp = TypeJSON // default + resTp = ResTypeJSON // default tLen = len(typeStr) ) switch { case tLen == 1: tpp := typeStr[0] - if _, ok := types[tpp]; ok { + if _, ok := _ReqContentTypeMap[tpp]; ok { reqTp = tpp - // default resTp = reqTp - resTp = tpp } case tLen > 1: // first param is request type tpp := typeStr[0] - if _, ok := types[tpp]; ok { + if _, ok := _ReqContentTypeMap[tpp]; ok { reqTp = tpp } // second param is response data type stpp := typeStr[1] - if _, ok := types[stpp]; ok { + if _, ok := _ResTypeMap[stpp]; ok { resTp = stpp } } + if c == nil { + c = defaultClient() + } r := &Request{ - client: c, - Header: make(http.Header), - requestType: reqTp, - unmarshalType: string(resTp), + client: c, + Header: make(http.Header), + requestType: reqTp, + responseType: resTp, } - r.Header.Set("Content-Type", types[reqTp]) + r.Header.Set("Content-Type", _ReqContentTypeMap[reqTp]) return r } diff --git a/pkg/xhttp/model.go b/pkg/xhttp/model.go index 97b21942..4343e0ff 100644 --- a/pkg/xhttp/model.go +++ b/pkg/xhttp/model.go @@ -1,26 +1,31 @@ package xhttp -type RequestType string - const ( - GET = "GET" - POST = "POST" - PUT = "PUT" - DELETE = "DELETE" - PATCH = "PATCH" - TypeJSON RequestType = "json" - TypeXML RequestType = "xml" - TypeUrlencoded RequestType = "urlencoded" - TypeForm RequestType = "form" - TypeFormData RequestType = "form-data" - TypeMultipartFormData RequestType = "multipart-form-data" + GET = "GET" + POST = "POST" + PUT = "PUT" + DELETE = "DELETE" + PATCH = "PATCH" + + ResTypeJSON = "application/json" + ResTypeXML = "application/xml" + + TypeJSON = "json" + TypeXML = "xml" + TypeFormData = "form-data" + TypeMultipartFormData = "multipart-form-data" ) -var types = map[RequestType]string{ - TypeJSON: "application/json", - TypeXML: "application/xml", - TypeUrlencoded: "application/x-www-form-urlencoded", - TypeForm: "application/x-www-form-urlencoded", - TypeFormData: "application/x-www-form-urlencoded", - TypeMultipartFormData: "multipart/form-data", -} +var ( + _ReqContentTypeMap = map[string]string{ + TypeJSON: "application/json", + TypeXML: "application/xml", + TypeFormData: "application/x-www-form-urlencoded", + TypeMultipartFormData: "multipart/form-data", + } + + _ResTypeMap = map[string]string{ + ResTypeJSON: "json", + ResTypeXML: "xml", + } +) diff --git a/pkg/xhttp/request.go b/pkg/xhttp/request.go index 36db2c1a..1059f670 100644 --- a/pkg/xhttp/request.go +++ b/pkg/xhttp/request.go @@ -24,8 +24,8 @@ type Request struct { jsonByte []byte url string method string - requestType RequestType - unmarshalType string + requestType string + responseType string multipartBodyMap map[string]any err error } @@ -74,7 +74,7 @@ func (r *Request) SendStruct(v any) (c *Request) { switch r.requestType { case TypeJSON: r.jsonByte = bs - case TypeXML, TypeUrlencoded, TypeForm, TypeFormData: + case TypeXML, TypeFormData: body := make(map[string]any) if err = json.Unmarshal(bs, &body); err != nil { r.err = fmt.Errorf("json.Unmarshal(%s, %+v):%w", string(bs), body, err) @@ -97,7 +97,7 @@ func (r *Request) SendBodyMap(bm map[string]any) (client *Request) { return r } r.jsonByte = bs - case TypeXML, TypeUrlencoded, TypeForm, TypeFormData: + case TypeXML, TypeFormData: r.formString = FormatURLParam(bm) } return r @@ -115,7 +115,7 @@ func (r *Request) SendMultipartBodyMap(bm map[string]any) (client *Request) { return r } r.jsonByte = bs - case TypeXML, TypeUrlencoded, TypeForm, TypeFormData: + case TypeXML, TypeFormData: r.formString = FormatURLParam(bm) case TypeMultipartFormData: r.multipartBodyMap = bm @@ -128,7 +128,7 @@ func (r *Request) SendString(encodeStr string) (client *Request) { switch r.requestType { case TypeJSON: r.jsonByte = []byte(encodeStr) - case TypeXML, TypeUrlencoded, TypeForm, TypeFormData: + case TypeXML, TypeFormData: r.formString = encodeStr } return r @@ -159,7 +159,7 @@ func (r *Request) EndBytes(ctx context.Context) (res *http.Response, bs []byte, if r.jsonByte != nil { body = strings.NewReader(string(r.jsonByte)) } - case TypeForm, TypeFormData, TypeUrlencoded: + case TypeFormData: if r.formString != "" { body = strings.NewReader(r.formString) } @@ -222,7 +222,7 @@ func (r *Request) EndStruct(ctx context.Context, v any) (res *http.Response, err return res, fmt.Errorf("StatusCode(%d) != 200", res.StatusCode) } - switch r.unmarshalType { + switch r.responseType { case string(TypeJSON): err = json.Unmarshal(bs, &v) if err != nil { @@ -236,7 +236,7 @@ func (r *Request) EndStruct(ctx context.Context, v any) (res *http.Response, err } return res, nil default: - return nil, errors.New("unmarshalType Type Wrong") + return nil, errors.New("responseType Type Wrong") } }