Skip to content

Commit

Permalink
Merge pull request #26 from CyJaySong/master
Browse files Browse the repository at this point in the history
优化AccessToken 获取机制,临近过期才会刷新AccessToken
  • Loading branch information
cyjaysong authored Jun 25, 2021
2 parents e20b539 + b72a39d commit 959f6ed
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 24 deletions.
8 changes: 7 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package gosdk

import (
"errors"
"strconv"
"time"

"github.com/imroc/req"
)
Expand All @@ -19,6 +21,7 @@ type Client struct {
ClientID string
ClientSecret string
AccessToken string
expireAt time.Time
Authorizer Authorizer
}

Expand Down Expand Up @@ -53,11 +56,14 @@ func (da DefaultAuthorizer) Authorize(client *Client) error {
}

client.AccessToken = authresponse.AccessToken
expireIn, _ := strconv.Atoi(authresponse.ExpireIn)
// 记录AccessToken 过期时间,过期时间前60秒就触发刷新
client.expireAt = time.Now().Add(time.Second * time.Duration(expireIn-60))
return nil
}

func (client *Client) Auth() error {
if client.AccessToken != "" {
if client.AccessToken != "" && time.Now().Before(client.expireAt) {
return nil
}

Expand Down
1 change: 0 additions & 1 deletion vision/face/v2/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type FaceResponse struct {
}

func (fc *FaceClient) DetectAndAnalysis(image *vision.Image, options map[string]interface{}) (*FaceResponse, error) {

if err := fc.Auth(); err != nil {
return nil, err
}
Expand Down
6 changes: 2 additions & 4 deletions vision/face/v2/person_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ func (fc FaceClient) PersonVerify(image, idCardNumber, name string, options map[
return nil, errors.New("image length is invalid")
}

if fc.AccessToken == "" {
if err := fc.Auth(); err != nil {
return nil, err
}
if err := fc.Auth(); err != nil {
return nil, err
}
options["image"] = image
options["id_card_number"] = idCardNumber
Expand Down
20 changes: 6 additions & 14 deletions vision/face/v2/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ type IdentifyResponse struct {
}

func (fc FaceClient) Identify(img *vision.Image, groupID []string, options map[string]interface{}) (*IdentifyResponse, error) {
if fc.AccessToken == "" {
if err := fc.Auth(); err != nil {
return nil, err
}
if err := fc.Auth(); err != nil {
return nil, err
}

encodedImg, err := img.Base64Encode()
if err != nil {
return nil, err
Expand All @@ -50,12 +47,9 @@ type VerifyResponse struct {
}

func (fc FaceClient) Verify(img *vision.Image, uid string, groupID string, options map[string]interface{}) (*VerifyResponse, error) {
if fc.AccessToken == "" {
if err := fc.Auth(); err != nil {
return nil, err
}
if err := fc.Auth(); err != nil {
return nil, err
}

encodedImg, err := img.Base64Encode()
if err != nil {
return nil, err
Expand All @@ -80,10 +74,8 @@ type MultiVerifyResponse struct {
}

func (fc FaceClient) MultiVerify(image vision.Image, groupIDs []string, options map[string]interface{}) (*MultiVerifyResponse, error) {
if fc.AccessToken == "" {
if err := fc.Auth(); err != nil {
return nil, err
}
if err := fc.Auth(); err != nil {
return nil, err
}

encodedImage, err := image.Base64Encode()
Expand Down
6 changes: 2 additions & 4 deletions vision/face/v3/person_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ func (fc FaceClient) PersonVerify(image, imageType, idCardNumber, name string, o
if imageType == "BASE64" && len(image) > 2<<(20-1)*2 {
return nil, errors.New("image length is invalid")
}
if fc.AccessToken == "" {
if err := fc.Auth(); err != nil {
return nil, err
}
if err := fc.Auth(); err != nil {
return nil, err
}
options["image"] = image
options["image_type"] = imageType
Expand Down

0 comments on commit 959f6ed

Please sign in to comment.