update jt express

This commit is contained in:
Sinh 2023-09-14 14:07:34 +07:00
parent b15d883542
commit 9e15bfc101
3 changed files with 71 additions and 46 deletions

View File

@ -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)
resp, err := c.httpClient.R().
SetFormData(map[string]string{
body := map[string]string{
"logistics_interface": data,
"data_digest": c.getDigest(data),
"msg_type": msgTypeEstimateFee,
"eccompanyid": c.EccompanyID,
}).
SetResult(&EstimateFeeRes{}).
}
r.Request.Body = pjson.ToBytes(body)
r.Request.URL = path
resp, err := c.httpClient.R().
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)
resp, err := c.httpClient.R().
SetFormData(map[string]string{
body := map[string]string{
"logistics_interface": data,
"data_digest": c.getDigest(data),
"msg_type": msgTypeEstimateFee,
"eccompanyid": c.EccompanyID,
}).
SetResult(&CancelOrderRes{}).
}
r.Request.Body = pjson.ToBytes(body)
r.Request.URL = path
resp, err := c.httpClient.R().
SetFormData(body).
Post(path)
if err != nil {
return nil, fmt.Errorf("jtepxress: request %s, err %v", path, err)
}
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.Error = fmt.Errorf("jtepxress: request %s, err %v", path, err)
return r
}
func (c *Client) CreateOrder(req *CreateOrderReq) (*CreateOrderItemRes, error) {
r.Response.StatusCode = resp.StatusCode()
r.Response.Body = resp.Body()
return r
}
func (c *Client) CreateOrder(req *CreateOrderReq) (r Response) {
path := c.host + apiPathCreateOrder
data := pjson.ToJSONString(req)
resp, err := c.httpClient.R().
SetFormData(map[string]string{
body := map[string]string{
"logistics_interface": data,
"data_digest": c.getDigest(data),
"msg_type": msgTypeCreateOrder,
"eccompanyid": c.EccompanyID,
}).
SetResult(&CreateOrderRes{}).
}
r.Request.Body = pjson.ToBytes(body)
r.Request.URL = path
resp, err := c.httpClient.R().
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 {

View File

@ -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"`

View File

@ -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
}