forked from xen0n/go-workwx
/
client.go
186 lines (149 loc) · 3.81 KB
/
client.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package workwx
import (
"bytes"
"encoding/json"
"fmt"
"mime/multipart"
"net/url"
"sync"
)
// Workwx 企业微信客户端
type Workwx struct {
opts options
// CorpID 企业 ID,必填
CorpID string
}
// WorkwxApp 企业微信客户端(分应用)
type WorkwxApp struct {
*Workwx
// CorpSecret 应用的凭证密钥,必填
CorpSecret string
// AgentID 应用 ID,必填
AgentID int64
accessToken *token
jsapiTicket *token
jsapiTicketAgentConfig *token
}
// New 构造一个 Workwx 客户端对象,需要提供企业 ID
func New(corpID string, opts ...CtorOption) *Workwx {
optionsObj := defaultOptions()
for _, o := range opts {
o.applyTo(&optionsObj)
}
return &Workwx{
opts: optionsObj,
CorpID: corpID,
}
}
// WithApp 构造本企业下某自建 app 的客户端
func (c *Workwx) WithApp(corpSecret string, agentID int64) *WorkwxApp {
app := WorkwxApp{
Workwx: c,
CorpSecret: corpSecret,
AgentID: agentID,
accessToken: &token{mutex: &sync.RWMutex{}},
jsapiTicket: &token{mutex: &sync.RWMutex{}},
jsapiTicketAgentConfig: &token{mutex: &sync.RWMutex{}},
}
app.accessToken.setGetTokenFunc(app.getAccessToken)
app.jsapiTicket.setGetTokenFunc(app.getJSAPITicket)
app.jsapiTicketAgentConfig.setGetTokenFunc(app.getJSAPITicketAgentConfig)
return &app
}
func (c *WorkwxApp) composeQyapiURL(path string, req interface{}) *url.URL {
values := url.Values{}
if valuer, ok := req.(urlValuer); ok {
values = valuer.intoURLValues()
}
// TODO: refactor
base, err := url.Parse(c.opts.QYAPIHost)
if err != nil {
// TODO: error_chain
panic(fmt.Sprintf("qyapiHost invalid: host=%s err=%+v", c.opts.QYAPIHost, err))
}
base.Path = path
base.RawQuery = values.Encode()
return base
}
func (c *WorkwxApp) composeQyapiURLWithToken(path string, req interface{}, withAccessToken bool) *url.URL {
url := c.composeQyapiURL(path, req)
if !withAccessToken {
return url
}
q := url.Query()
q.Set("access_token", c.accessToken.getToken())
url.RawQuery = q.Encode()
return url
}
func (c *WorkwxApp) executeQyapiGet(path string, req urlValuer, respObj interface{}, withAccessToken bool) error {
url := c.composeQyapiURLWithToken(path, req, withAccessToken)
urlStr := url.String()
resp, err := c.opts.HTTP.Get(urlStr)
if err != nil {
// TODO: error_chain
return err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(respObj)
if err != nil {
// TODO: error_chain
return err
}
return nil
}
func (c *WorkwxApp) executeQyapiJSONPost(path string, req bodyer, respObj interface{}, withAccessToken bool) error {
url := c.composeQyapiURLWithToken(path, req, withAccessToken)
urlStr := url.String()
body, err := req.intoBody()
if err != nil {
// TODO: error_chain
return err
}
resp, err := c.opts.HTTP.Post(urlStr, "application/json", bytes.NewReader(body))
if err != nil {
// TODO: error_chain
return err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(respObj)
if err != nil {
// TODO: error_chain
return err
}
return nil
}
func (c *WorkwxApp) executeQyapiMediaUpload(
path string,
req mediaUploader,
respObj interface{},
withAccessToken bool,
) error {
url := c.composeQyapiURLWithToken(path, req, withAccessToken)
urlStr := url.String()
m := req.getMedia()
// FIXME: use streaming upload to conserve memory!
buf := bytes.Buffer{}
mw := multipart.NewWriter(&buf)
err := m.writeTo(mw)
if err != nil {
return err
}
err = mw.Close()
if err != nil {
return err
}
resp, err := c.opts.HTTP.Post(urlStr, mw.FormDataContentType(), &buf)
if err != nil {
// TODO: error_chain
return err
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(respObj)
if err != nil {
// TODO: error_chain
return err
}
return nil
}