package jtexpress import ( "crypto/md5" "fmt" "io" "github.com/go-resty/resty/v2" "git.selly.red/Selly-Modules/3pl/util/pjson" ) func New(degestKey, companyID string, isProd, debug bool) *Client { host := apiHostDev if isProd { host = apiHostProd } c := &Client{ DigestKey: degestKey, EccompanyID: companyID, IsProduction: isProd, Debug: debug, host: host, httpClient: resty.New().SetDebug(debug), } return c } type Client struct { DigestKey string EccompanyID string IsProduction bool Debug bool host string httpClient *resty.Client } func (c *Client) EstimateFee(req *EstimateFeeReq) (*EstimateFeeItemRes, error) { path := c.host + apiPathEstimateFee data := pjson.ToJSONString(req) 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{}). Post(path) if err != nil { return nil, fmt.Errorf("jtepxress: request %s, err %v", path, err) } res := resp.Result().(*EstimateFeeRes) if len(res.ResponseItems) == 0 { return nil, fmt.Errorf("jtepxress: estimate fee empty response") } return res.ResponseItems[0], nil } func (c *Client) CancelOrder(req *CancelOrderReq) (*CancelOrderRes, error) { path := c.host + apiPathCancelOrder data := pjson.ToJSONString(req) 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{}). 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 } func (c *Client) CreateOrder(req *CreateOrderReq) (*CreateOrderItemRes, error) { path := c.host + apiPathCreateOrder data := pjson.ToJSONString(req) 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{}). Post(path) if err != nil { return nil, fmt.Errorf("jtepxress: request %s, err %v", path, err) } res := resp.Result().(*CreateOrderRes) if len(res.ResponseItems) == 0 { return nil, fmt.Errorf("jtepxress: create order empty response") } return res.ResponseItems[0], nil } func (c *Client) getDigest(data string) string { s := data + c.DigestKey h := md5.New() io.WriteString(h, s) return fmt.Sprintf("%x", h.Sum(nil)) }