package jtexpress import ( "crypto/md5" "fmt" "io" "github.com/go-resty/resty/v2" "git.selly.red/Selly-Modules/3pl/util/pjson" ) func New(digestKey, companyID string, isProd, debug bool) *Client { host := apiHostDev if isProd { host = apiHostProd } c := &Client{ DigestKey: digestKey, 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) (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(body). Post(path) if err != nil { r.Error = fmt.Errorf("jtepxress: request %s, err %v", path, err) return r } r.Response.StatusCode = resp.StatusCode() r.Response.Body = resp.Body() return r } 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(body). Post(path) if err != nil { r.Error = fmt.Errorf("jtepxress: request %s, err %v", path, err) return r } 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) 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(body). Post(path) if err != nil { r.Error = fmt.Errorf("jtepxress: request %s, err %v", path, err) return r } r.Response.StatusCode = resp.StatusCode() r.Response.Body = resp.Body() return r } 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)) }