/
ai.go
77 lines (69 loc) · 1.66 KB
/
ai.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package ecloud
import (
"github.com/guonaihong/gout"
"github.com/guonaihong/gout/dataflow"
"github.com/pkg/errors"
"net/http"
"strconv"
"time"
)
type (
AiService struct {
g *dataflow.Gout
opt *AiOptions
expireTime time.Time
accessToken string
}
AiOptions struct {
AccessKey string
SecretKey string
Client *http.Client
}
authMiddleware struct {
as *AiService
}
)
func NewAi(opt *AiOptions) (*AiService, error) {
s := &AiService{
g: gout.New(opt.Client),
opt: opt,
}
if err := s.RefreshAccessToken(); err != nil {
return nil, errors.Wrap(err, "request access token error")
}
return s, nil
}
func (as *AiService) RefreshAccessToken() error {
var rsp gout.H
if err := as.g.GET("https://smartlib-api-changsha-1.cmecloud.cn:8444/ecloud/ai/oauth/getToken").SetQuery(gout.H{
"grant_type": "client_credentials",
"client_id": as.opt.AccessKey,
"client_secret": as.opt.SecretKey,
}).BindJSON(&rsp).Do(); err != nil {
return errors.Wrap(err, "send request error")
}
if m, ok := rsp["errorCode"]; ok {
return errors.New(m.(string))
}
ei, _ := strconv.ParseInt(rsp["expires_in"].(string), 10, 64)
as.expireTime = time.Now().Add(time.Second * time.Duration(ei))
as.accessToken = rsp["access_token"].(string)
return nil
}
func (as *AiService) auth() *authMiddleware {
return &authMiddleware{
as: as,
}
}
func (a *authMiddleware) ModifyRequest(req *http.Request) error {
if time.Now().After(a.as.expireTime) {
if err := a.as.RefreshAccessToken(); err != nil {
return err
}
}
if req.URL.RawQuery != "" {
req.URL.RawQuery += "&"
}
req.URL.RawQuery += "access_token=" + a.as.accessToken
return nil
}