Skip to content

Commit

Permalink
update xhttp client
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoogle-ink committed Nov 2, 2023
1 parent fd333d0 commit 2ed752e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 47 deletions.
42 changes: 25 additions & 17 deletions pkg/xhttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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) {
Expand All @@ -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
}
47 changes: 26 additions & 21 deletions pkg/xhttp/model.go
Original file line number Diff line number Diff line change
@@ -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",
}
)
18 changes: 9 additions & 9 deletions pkg/xhttp/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
}
}

Expand Down

0 comments on commit 2ed752e

Please sign in to comment.