-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
95 lines (81 loc) · 1.8 KB
/
client_test.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package dingtalk
import (
"fmt"
"net/http"
"net/url"
"os"
"testing"
"time"
)
func NewDingtalkClientWithEnv() (*Client, error) {
var appKey string
if appKey = os.Getenv("DINGTALK_APP_KEY"); appKey == "" {
return nil, fmt.Errorf("not DINGTALK_APP_KEY environment variable")
}
var appSecret string
if appSecret = os.Getenv("DINGTALK_APP_SECRET"); appSecret == "" {
return nil, fmt.Errorf("not DINGTALK_APP_SECRET environment variable")
}
auth := &CorpInternalAuth{
AppKey: appKey,
AppSecret: appSecret,
}
token, err := auth.GetToken()
if err != nil {
return nil, err
}
if token.Errcode != 0 {
return nil, fmt.Errorf(token.Errmsg)
}
return &Client{
AccessToken: token.AccessToken,
accessTokenExpired: time.Now().Unix() + token.ExpiresIn,
auth: auth,
baseUrl: &url.URL{
Scheme: "https",
Host: "oapi.dingtalk.com",
},
http: &http.Client{},
}, nil
}
func NewDingtalkClientWithParams(appKey, appSecret string) (*Client, error) {
auth := &CorpInternalAuth{
AppKey: appKey,
AppSecret: appSecret,
}
token, err := auth.GetToken()
if err != nil {
return nil, err
}
if token.Errcode != 0 {
return nil, fmt.Errorf(token.Errmsg)
}
return &Client{
AccessToken: token.AccessToken,
accessTokenExpired: time.Now().Unix() + token.ExpiresIn,
auth: auth,
baseUrl: &url.URL{
Scheme: "https",
Host: "oapi.dingtalk.com",
},
http: &http.Client{},
}, nil
}
func TestCorpClient(t *testing.T) {
client, err := NewDingtalkClientWithEnv()
if err != nil {
t.Error(err)
}
fmt.Println(client.AccessToken)
}
func TestNewToken(t *testing.T) {
client, err := NewDingtalkClientWithParams("", "")
if err != nil {
t.Error(err)
}
token, err := client.GetToken()
if err != nil {
return
}
fmt.Println("token", token)
}