3pl/partnerapi/jtexpress/jtexpress.go

120 lines
2.6 KiB
Go
Raw Normal View History

2023-09-13 10:42:06 +00:00
package jtexpress
import (
"crypto/md5"
"fmt"
"io"
"github.com/go-resty/resty/v2"
"git.selly.red/Selly-Modules/3pl/util/pjson"
)
2023-09-14 07:07:34 +00:00
func New(digestKey, companyID string, isProd, debug bool) *Client {
2023-09-13 10:42:06 +00:00
host := apiHostDev
if isProd {
host = apiHostProd
}
c := &Client{
2023-09-14 07:07:34 +00:00
DigestKey: digestKey,
2023-09-13 10:42:06 +00:00
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
}
2023-09-14 07:07:34 +00:00
func (c *Client) EstimateFee(req *EstimateFeeReq) (r Response) {
2023-09-13 10:42:06 +00:00
path := c.host + apiPathEstimateFee
data := pjson.ToJSONString(req)
2023-09-14 07:07:34 +00:00
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
2023-09-13 10:42:06 +00:00
resp, err := c.httpClient.R().
2023-09-14 07:07:34 +00:00
SetFormData(body).
2023-09-13 10:42:06 +00:00
Post(path)
if err != nil {
2023-09-14 07:07:34 +00:00
r.Error = fmt.Errorf("jtepxress: request %s, err %v", path, err)
return r
2023-09-13 10:42:06 +00:00
}
2023-09-14 07:07:34 +00:00
r.Response.StatusCode = resp.StatusCode()
r.Response.Body = resp.Body()
return r
2023-09-13 10:42:06 +00:00
}
2023-09-14 07:07:34 +00:00
func (c *Client) CancelOrder(req *CancelOrderReq) (r Response) {
2023-09-13 10:42:06 +00:00
path := c.host + apiPathCancelOrder
data := pjson.ToJSONString(req)
2023-09-14 07:07:34 +00:00
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
2023-09-13 10:42:06 +00:00
resp, err := c.httpClient.R().
2023-09-14 07:07:34 +00:00
SetFormData(body).
2023-09-13 10:42:06 +00:00
Post(path)
if err != nil {
2023-09-14 07:07:34 +00:00
r.Error = fmt.Errorf("jtepxress: request %s, err %v", path, err)
return r
2023-09-13 10:42:06 +00:00
}
2023-09-14 07:07:34 +00:00
r.Response.StatusCode = resp.StatusCode()
r.Response.Body = resp.Body()
return r
2023-09-13 10:42:06 +00:00
}
2023-09-14 07:07:34 +00:00
func (c *Client) CreateOrder(req *CreateOrderReq) (r Response) {
2023-09-13 10:42:06 +00:00
path := c.host + apiPathCreateOrder
data := pjson.ToJSONString(req)
2023-09-14 07:07:34 +00:00
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
2023-09-13 10:42:06 +00:00
resp, err := c.httpClient.R().
2023-09-14 07:07:34 +00:00
SetFormData(body).
2023-09-13 10:42:06 +00:00
Post(path)
if err != nil {
2023-09-14 07:07:34 +00:00
r.Error = fmt.Errorf("jtepxress: request %s, err %v", path, err)
return r
2023-09-13 10:42:06 +00:00
}
2023-09-14 07:07:34 +00:00
r.Response.StatusCode = resp.StatusCode()
r.Response.Body = resp.Body()
return r
2023-09-13 10:42:06 +00:00
}
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))
}