From 9e15bfc101c8de6b6533296e182681da98d0612e Mon Sep 17 00:00:00 2001 From: Sinh Date: Thu, 14 Sep 2023 14:07:34 +0700 Subject: [PATCH] update jt express --- partnerapi/jtexpress/jtexpress.go | 98 ++++++++++++++------------ partnerapi/jtexpress/model_request.go | 1 - partnerapi/jtexpress/model_response.go | 18 +++++ 3 files changed, 71 insertions(+), 46 deletions(-) diff --git a/partnerapi/jtexpress/jtexpress.go b/partnerapi/jtexpress/jtexpress.go index 2c9cbbb..cc32ccf 100644 --- a/partnerapi/jtexpress/jtexpress.go +++ b/partnerapi/jtexpress/jtexpress.go @@ -10,13 +10,13 @@ import ( "git.selly.red/Selly-Modules/3pl/util/pjson" ) -func New(degestKey, companyID string, isProd, debug bool) *Client { +func New(digestKey, companyID string, isProd, debug bool) *Client { host := apiHostDev if isProd { host = apiHostProd } c := &Client{ - DigestKey: degestKey, + DigestKey: digestKey, EccompanyID: companyID, IsProduction: isProd, Debug: debug, @@ -35,71 +35,79 @@ type Client struct { httpClient *resty.Client } -func (c *Client) EstimateFee(req *EstimateFeeReq) (*EstimateFeeItemRes, error) { +func (c *Client) EstimateFee(req *EstimateFeeReq) (r Response) { path := c.host + apiPathEstimateFee data := pjson.ToJSONString(req) + body := map[string]string{ + "logistics_interface": data, + "data_digest": c.getDigest(data), + "msg_type": msgTypeEstimateFee, + "eccompanyid": c.EccompanyID, + } + r.Request.Body = pjson.ToBytes(body) + r.Request.URL = path resp, err := c.httpClient.R(). - SetFormData(map[string]string{ - "logistics_interface": data, - "data_digest": c.getDigest(data), - "msg_type": msgTypeEstimateFee, - "eccompanyid": c.EccompanyID, - }). - SetResult(&EstimateFeeRes{}). + SetFormData(body). Post(path) if err != nil { - return nil, fmt.Errorf("jtepxress: request %s, err %v", path, err) + r.Error = fmt.Errorf("jtepxress: request %s, err %v", path, err) + return r } - res := resp.Result().(*EstimateFeeRes) - if len(res.ResponseItems) == 0 { - return nil, fmt.Errorf("jtepxress: estimate fee empty response") - } - return res.ResponseItems[0], nil + r.Response.StatusCode = resp.StatusCode() + r.Response.Body = resp.Body() + + return r } -func (c *Client) CancelOrder(req *CancelOrderReq) (*CancelOrderRes, error) { +func (c *Client) CancelOrder(req *CancelOrderReq) (r Response) { path := c.host + apiPathCancelOrder data := pjson.ToJSONString(req) + body := map[string]string{ + "logistics_interface": data, + "data_digest": c.getDigest(data), + "msg_type": msgTypeEstimateFee, + "eccompanyid": c.EccompanyID, + } + r.Request.Body = pjson.ToBytes(body) + r.Request.URL = path resp, err := c.httpClient.R(). - SetFormData(map[string]string{ - "logistics_interface": data, - "data_digest": c.getDigest(data), - "msg_type": msgTypeEstimateFee, - "eccompanyid": c.EccompanyID, - }). - SetResult(&CancelOrderRes{}). + SetFormData(body). Post(path) if err != nil { - return nil, fmt.Errorf("jtepxress: request %s, err %v", path, err) + r.Error = fmt.Errorf("jtepxress: request %s, err %v", path, err) + return r } - res := resp.Result().(*CancelOrderRes) - if len(res.ResponseItems) == 0 { - return nil, fmt.Errorf("jtepxress: cancel order empty response") - } - // return res.ResponseItems[0], nil - return nil, err + + r.Response.StatusCode = resp.StatusCode() + r.Response.Body = resp.Body() + + return r } -func (c *Client) CreateOrder(req *CreateOrderReq) (*CreateOrderItemRes, error) { +func (c *Client) CreateOrder(req *CreateOrderReq) (r Response) { path := c.host + apiPathCreateOrder data := pjson.ToJSONString(req) + body := map[string]string{ + "logistics_interface": data, + "data_digest": c.getDigest(data), + "msg_type": msgTypeCreateOrder, + "eccompanyid": c.EccompanyID, + } + r.Request.Body = pjson.ToBytes(body) + r.Request.URL = path + resp, err := c.httpClient.R(). - SetFormData(map[string]string{ - "logistics_interface": data, - "data_digest": c.getDigest(data), - "msg_type": msgTypeCreateOrder, - "eccompanyid": c.EccompanyID, - }). - SetResult(&CreateOrderRes{}). + SetFormData(body). Post(path) if err != nil { - return nil, fmt.Errorf("jtepxress: request %s, err %v", path, err) + r.Error = fmt.Errorf("jtepxress: request %s, err %v", path, err) + return r } - res := resp.Result().(*CreateOrderRes) - if len(res.ResponseItems) == 0 { - return nil, fmt.Errorf("jtepxress: create order empty response") - } - return res.ResponseItems[0], nil + + r.Response.StatusCode = resp.StatusCode() + r.Response.Body = resp.Body() + + return r } func (c *Client) getDigest(data string) string { diff --git a/partnerapi/jtexpress/model_request.go b/partnerapi/jtexpress/model_request.go index daddc0d..788c33d 100644 --- a/partnerapi/jtexpress/model_request.go +++ b/partnerapi/jtexpress/model_request.go @@ -9,7 +9,6 @@ type EstimateFeeLocation struct { type EstimateFeeReq struct { SelfAddress int `json:"selfAddress"` ProductType string `json:"producttype"` - CusName string `json:"cusname"` GoodsValue string `json:"goodsvalue"` ItemsValue string `json:"itemsvalue"` Weight string `json:"weight"` diff --git a/partnerapi/jtexpress/model_response.go b/partnerapi/jtexpress/model_response.go index 04b359a..7ce3718 100644 --- a/partnerapi/jtexpress/model_response.go +++ b/partnerapi/jtexpress/model_response.go @@ -41,3 +41,21 @@ type CancelOrderRes struct { LogisticProviderID string `json:"logisticproviderid"` ResponseItems []*CreateOrderItemRes `json:"responseitems"` } + +type Response struct { + Request RequestInfo + Response ResponseInfo + Error error +} + +type ResponseInfo struct { + StatusCode int + Body []byte +} + +type RequestInfo struct { + Method string + URL string + Headers map[string]string + Body []byte +}