71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package kuaishou_mini
|
|
|
|
import (
|
|
"fmt"
|
|
"git.rosy.net.cn/baseapi/platformapi"
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
const (
|
|
// KuaiShouBashUrl 基础域名
|
|
KuaiShouBashUrl = "https://open.kuaishou.com" // 域名
|
|
|
|
// 获取授权信息
|
|
KuaiShouAuthLogin = KuaiShouBashUrl + "/oauth2/mp/code2session" // 授权登录
|
|
)
|
|
|
|
type API struct {
|
|
appSecret string // 应用唯一标识对应的密钥
|
|
appId string // 应用唯一标识
|
|
client *http.Client
|
|
config *platformapi.APIConfig
|
|
locker sync.RWMutex
|
|
msgToken string // accessToken
|
|
expiresIn int64 // 过期时间
|
|
}
|
|
|
|
func (a *API) GetAppID() string {
|
|
return a.appId
|
|
}
|
|
|
|
func (a *API) GetSecret() string {
|
|
return a.appSecret
|
|
}
|
|
|
|
// New 初始化
|
|
func New(appSecret, appId string, config ...*platformapi.APIConfig) *API {
|
|
curConfig := platformapi.DefAPIConfig
|
|
if len(config) > 0 {
|
|
curConfig = *config[0]
|
|
}
|
|
return &API{
|
|
appSecret: appSecret,
|
|
appId: appId,
|
|
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
|
config: &curConfig,
|
|
}
|
|
}
|
|
|
|
// AccessAPI2 发送请求
|
|
func (a *API) AccessAPI2(url string, params map[string]interface{}) (retVal map[string]interface{}, err error) {
|
|
|
|
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
|
func() *http.Request {
|
|
request, _ := http.NewRequest(http.MethodPost, url, strings.NewReader(utils.Map2URLValues(params).Encode()))
|
|
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
return request
|
|
},
|
|
a.config,
|
|
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
|
if jsonResult1 == nil {
|
|
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
|
}
|
|
retVal = jsonResult1
|
|
return platformapi.ErrLevelSuccess, nil
|
|
})
|
|
return retVal, err
|
|
}
|