Enhance token caching with expiration check in MessageCenter
This commit is contained in:
parent
ca173e57c6
commit
b0fc0e3600
13
paramset.go
13
paramset.go
|
@ -7,6 +7,7 @@ import (
|
|||
"gitea.cdlsxd.cn/self-tools/l_msg_api/cache"
|
||||
"gitea.cdlsxd.cn/self-tools/l_msg_api/httpclient"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (m *MessageCenter) parseOACreateParam(formModel *FormsData) (out []byte) {
|
||||
|
@ -39,17 +40,19 @@ func (m *MessageCenter) parseSendBlackBoardParam(title, content string, receiver
|
|||
}
|
||||
|
||||
func (m *MessageCenter) getAccessToken() (string, error) {
|
||||
if tokenInterface, exist := cache.InstanceCacheMap().Get(m.ClientKey); exist {
|
||||
return tokenInterface.(string), nil
|
||||
}
|
||||
var data accessTokenResponse
|
||||
|
||||
if tokenInstance, exist := cache.InstanceCacheMap().Get(m.ClientKey); exist {
|
||||
data = tokenInstance.(accessTokenResponse)
|
||||
if int64(data.AccessExpire) >= time.Now().Unix() {
|
||||
return tokenInstance.(string), nil
|
||||
}
|
||||
}
|
||||
var authParam, _ = json.Marshal(map[string]string{"client_key": m.ClientKey, "client_secret": m.ClientSecret})
|
||||
err := m.accessPost(accessToken, authParam, &data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
cache.InstanceCacheMap().Put(m.ClientKey, data.AccessToken)
|
||||
cache.InstanceCacheMap().Put(m.ClientKey, data)
|
||||
return data.AccessToken, err
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,12 @@ func TestSms(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
func TestAccess(t *testing.T) {
|
||||
msg, _ := l_msg_api.NewMessageCenter(serverHost, ClientKey, ClientSecret, "sms", "sms")
|
||||
msg2, _ := l_msg_api.NewMessageCenter(serverHost, ClientKey, ClientSecret, "sms", "sms")
|
||||
fmt.Println(msg, msg2)
|
||||
}
|
||||
|
||||
func TestOaCreate(t *testing.T) {
|
||||
msg, err := l_msg_api.NewMessageCenter(local, ClientKey, ClientSecret, "sw_oa", "sw_oa_purchase")
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue