3pl/partnerapi/jtexpress/jtexpress.go

121 lines
2.7 KiB
Go
Raw Permalink Normal View History

2023-09-13 10:42:06 +00:00
package jtexpress
import (
"crypto/md5"
"fmt"
"io"
"github.com/go-resty/resty/v2"
2023-09-14 08:23:50 +00:00
"git.selly.red/Selly-Modules/3pl/util/base64"
2023-09-13 10:42:06 +00:00
"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-10-13 04:36:13 +00:00
SetMultipartFormData(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),
2023-10-13 07:12:22 +00:00
"msg_type": msgTypeCancelOrder,
2023-09-14 07:07:34 +00:00
"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-10-13 04:36:13 +00:00
SetMultipartFormData(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-10-13 04:36:13 +00:00
SetMultipartFormData(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)
2023-09-14 08:23:50 +00:00
return base64.Encode([]byte(fmt.Sprintf("%x", h.Sum(nil))))
2023-09-13 10:42:06 +00:00
}