111 lines
2.7 KiB
Go
111 lines
2.7 KiB
Go
package coach_client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"time"
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/proxy"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
const (
|
|
BaseUrl_CA_Coach = "https://ca.coach.com"
|
|
BaseUrl_CA_CoachOutlet = "https://ca.coachoutlet.com"
|
|
)
|
|
|
|
func LoadCAClientType() string {
|
|
clientType := os.Getenv(Client_Type_Env_Key)
|
|
if clientType == "" {
|
|
logrus.Fatal("加载coach客户端类型失败")
|
|
}
|
|
if clientType != USClient_Type_Coach && clientType != USClient_Type_Coach_Outlet {
|
|
logrus.Fatal("加载coach客户端类型不正确")
|
|
}
|
|
return clientType
|
|
}
|
|
|
|
type CAClientBuilder struct {
|
|
pool *proxy.Pool
|
|
}
|
|
|
|
func CA(pool *proxy.Pool) *CAClientBuilder {
|
|
return &CAClientBuilder{
|
|
pool: pool,
|
|
}
|
|
}
|
|
|
|
func (u *CAClientBuilder) Coach() USCAClient {
|
|
return &ca{
|
|
pool: u.pool,
|
|
defaultTimeOut: 10 * time.Minute,
|
|
baseUrl: BaseUrl_CA_Coach,
|
|
}
|
|
}
|
|
|
|
func (u *CAClientBuilder) CoachOutlet() USCAClient {
|
|
return &ca{
|
|
pool: u.pool,
|
|
defaultTimeOut: 10 * time.Minute,
|
|
baseUrl: BaseUrl_CA_CoachOutlet,
|
|
}
|
|
}
|
|
|
|
func (u *CAClientBuilder) CreateByType(clientType string) USCAClient {
|
|
if clientType == USClient_Type_Coach {
|
|
return u.Coach()
|
|
} else if clientType == USClient_Type_Coach_Outlet {
|
|
return u.CoachOutlet()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type ca struct {
|
|
pool *proxy.Pool
|
|
defaultTimeOut time.Duration
|
|
baseUrl string
|
|
}
|
|
|
|
func (c *ca) BaseUrl() string {
|
|
return c.baseUrl
|
|
}
|
|
|
|
func (c *ca) RequestInventory(ctx context.Context, pid string) (inv Inventory, err error) {
|
|
sctx, cancel := context.WithTimeout(ctx, c.defaultTimeOut)
|
|
defer cancel()
|
|
var resp InventoryResponse
|
|
urlPath := fmt.Sprintf("/api/inventory?vgId=%s&includeVariantData=false", url.QueryEscape(pid))
|
|
err = tryRequest(sctx, c.baseUrl, urlPath, &resp, c.pool.RandomIterator())
|
|
inv = resp.Inventory.InventoryInfo
|
|
return
|
|
}
|
|
|
|
func (c *ca) RequestProductDetail(ctx context.Context, pid string) (data ProductData, err error) {
|
|
sctx, cancel := context.WithTimeout(ctx, c.defaultTimeOut)
|
|
defer cancel()
|
|
var resp ProductDataResponse
|
|
urlPath := fmt.Sprintf("/api/get-products?ids=%s&includeInventory=false", url.QueryEscape(pid))
|
|
err = tryRequest(sctx, c.baseUrl, urlPath, &resp, c.pool.RandomIterator())
|
|
if len(resp.ProductData) == 0 && err != nil {
|
|
err = fmt.Errorf("获取详情信息为空")
|
|
return
|
|
}
|
|
if len(resp.ProductData) > 0 {
|
|
data = *resp.ProductData[0]
|
|
}
|
|
return
|
|
}
|
|
|
|
func (c *ca) ViewAllBags(ctx context.Context, page int) (resp PageDataResponse, err error) {
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
sctx, cancel := context.WithTimeout(ctx, c.defaultTimeOut)
|
|
defer cancel()
|
|
urlPath := fmt.Sprintf("/api/shop/bags/view-all?locale=en&page=%d", page)
|
|
err = tryRequest(sctx, c.baseUrl, urlPath, &resp, c.pool.RandomIterator())
|
|
return
|
|
}
|