dw-sdk/bid-normal.go

79 lines
2.2 KiB
Go
Raw Normal View History

2024-09-04 15:44:44 +08:00
package dw_sdk
import (
"fmt"
"net/http"
"github.com/bytedance/sonic"
"github.com/gofiber/fiber/v3/client"
"github.com/google/go-querystring/query"
"github.com/pkg/errors"
"github.com/valyala/fasthttp/fasthttpproxy"
"time"
)
// https://open.dewu.com/#/api/body?apiId=41&id=3&title=%E5%87%BA%E4%BB%B7%E6%9C%8D%E5%8A%A1API
type NormalBidClientImpl struct {
cli *client.Client
cfg PublicConfig
baseUrl string
}
func NewNormalBidClient(cfg Config) BidClient {
baseUrl := GetUrl(cfg.Public.Env)
cli := client.New().SetJSONMarshal(func(v any) ([]byte, error) {
return sonic.Marshal(v)
}).SetJSONUnmarshal(func(data []byte, v any) error {
return sonic.Unmarshal(data, v)
}).AddRequestHook(func(c *client.Client, request *client.Request) error {
request.SetURL(baseUrl + request.URL())
return nil
})
if cfg.Proxy != nil {
cli.SetDial(fasthttpproxy.FasthttpHTTPDialer(fmt.Sprintf("%s:%s@%s:%s", cfg.Proxy.User, cfg.Proxy.Pass, cfg.Proxy.Host, cfg.Proxy.Port)))
}
return &NormalBidClientImpl{
cli: cli,
cfg: cfg.Public,
baseUrl: baseUrl,
}
}
// 获取平台sku最低价【普通现货】
const NormalLowestPriceUrl = "/dop/api/v1/bidding/normal/normal_lowest_price"
func (a *NormalBidClientImpl) LowestPrice(skuId int) (LowestPriceResponse, error) {
if skuId == 0 {
return LowestPriceResponse{}, Err_Params_None
}
data := LowestPriceReq{
PublicParams: PublicParams{
AppKey: a.cfg.Key,
Timestamp: time.Now().UnixMilli(),
},
SkuId: skuId,
}
Sign(&data, a.cfg.Secret)
v, _ := query.Values(data)
s := v.Encode()
r := a.cli.R()
defer client.ReleaseRequest(r)
response, err := r.Get(NormalLowestPriceUrl + "?" + s)
if err != nil {
return LowestPriceResponse{}, errors.Wrapf(Err_Request_Err, "%s err:%v", NormalLowestPriceUrl, err)
}
defer client.ReleaseResponse(response)
var resp PublicResponse[LowestPriceResponse]
if err = response.JSON(&resp); err != nil {
return LowestPriceResponse{}, errors.Wrapf(Err_JSON_UNMarshal, "%s err:%v", NormalLowestPriceUrl, err)
}
if resp.Code != http.StatusOK {
return LowestPriceResponse{}, errors.Wrapf(Err_Request_Err, "%s resp.msg:%v", NormalLowestPriceUrl, resp.Msg)
}
return resp.Data, nil
}