Commit 99b020e6beffced0a59541b7d28b14c370b6414f
1 parent
c67b60df
Exists in
master
Make suite_key, suite_secret configurable
Showing
3 changed files
with
33 additions
and
9 deletions
Show diff stats
lib/dingtalk/api/base.rb
... | ... | @@ -4,12 +4,17 @@ module Dingtalk |
4 | 4 | attr_accessor :corp_id |
5 | 5 | ACCESS_TOKEN = "access_token" |
6 | 6 | |
7 | - def initialize(corp_id = nil) | |
7 | + def initialize(corp_id = nil, permanent_code = nil) | |
8 | 8 | @corp_id = corp_id |
9 | + @permanent_code = permanent_code | |
9 | 10 | end |
10 | 11 | |
11 | 12 | def access_token |
12 | - "#{corp_id}_#{ACCESS_TOKEN}" | |
13 | + redis.get("#{corp_id}_#{ACCESS_TOKEN}") || set_access_token | |
14 | + end | |
15 | + | |
16 | + def set_access_token | |
17 | + Suite.new.set_corp_access_token(@corp_id, @permanent_code) | |
13 | 18 | end |
14 | 19 | |
15 | 20 | private | ... | ... |
lib/dingtalk/api/suite.rb
... | ... | @@ -5,10 +5,9 @@ module Dingtalk |
5 | 5 | SUITE_ACCESS_TOKEN = "suite_access_token" |
6 | 6 | EXPIRATION = 7200 |
7 | 7 | |
8 | - def initialize(suite_key, suite_secret) | |
9 | - # put them into config file? | |
10 | - @suite_key = suite_key | |
11 | - @suite_secret = suite_secret | |
8 | + def initialize | |
9 | + @suite_key = Dingtalk.suite_key | |
10 | + @suite_secret = Dingtalk.suite_secret | |
12 | 11 | end |
13 | 12 | |
14 | 13 | def get_permanent_code(tmp_auth_code) |
... | ... | @@ -29,13 +28,25 @@ module Dingtalk |
29 | 28 | suite_secret: @suite_secret, |
30 | 29 | suite_ticket: suite_ticket |
31 | 30 | } |
32 | - http_post('get_suite_token', params) | |
33 | - # TODO check response values | |
31 | + res = http_post('get_suite_token', params) | |
32 | + # TODO globally check response values | |
34 | 33 | redis.set(SUITE_ACCESS_TOKEN, res['suite_access_token']) |
35 | 34 | redis.expire(SUITE_ACCESS_TOKEN, EXPIRATION) |
36 | 35 | redis.get(SUITE_ACCESS_TOKEN) |
37 | 36 | end |
38 | 37 | |
38 | + def set_corp_access_token(corp_id, permanent_code) | |
39 | + params = { | |
40 | + suite_access_token: suite_access_token, | |
41 | + permanent_code: permanent_code, | |
42 | + auth_corpid: corp_id | |
43 | + } | |
44 | + res = http_post('get_corp_token', params) | |
45 | + redis.set("#{corp_id}_#{ACCESS_TOKEN}", res['access_token']) | |
46 | + redis.expire("#{corp_id}_#{ACCESS_TOKEN}", EXPIRATION) | |
47 | + redis.get("#{corp_id}_#{ACCESS_TOKEN}") | |
48 | + end | |
49 | + | |
39 | 50 | def suite_ticket |
40 | 51 | redis.get(SUITE_TICKET) |
41 | 52 | end | ... | ... |
lib/dingtalk/config.rb
... | ... | @@ -10,9 +10,17 @@ module Dingtalk |
10 | 10 | return nil unless self.config |
11 | 11 | @redis ||= config.redis |
12 | 12 | end |
13 | + | |
14 | + def suite_key | |
15 | + @suite_key ||= config.suite_key | |
16 | + end | |
17 | + | |
18 | + def suite_secret | |
19 | + @suite_secret ||= config.suite_secret | |
20 | + end | |
13 | 21 | end |
14 | 22 | |
15 | 23 | class Config |
16 | - attr_accessor :redis | |
24 | + attr_accessor :redis, :redis_options, :suite_key, :suite_secret | |
17 | 25 | end |
18 | 26 | end | ... | ... |