Commit 99b020e6beffced0a59541b7d28b14c370b6414f

Authored by Jeff Lai
1 parent c67b60df
Exists in master

Make suite_key, suite_secret configurable

lib/dingtalk/api/base.rb
@@ -4,12 +4,17 @@ module Dingtalk @@ -4,12 +4,17 @@ module Dingtalk
4 attr_accessor :corp_id 4 attr_accessor :corp_id
5 ACCESS_TOKEN = "access_token" 5 ACCESS_TOKEN = "access_token"
6 6
7 - def initialize(corp_id = nil) 7 + def initialize(corp_id = nil, permanent_code = nil)
8 @corp_id = corp_id 8 @corp_id = corp_id
  9 + @permanent_code = permanent_code
9 end 10 end
10 11
11 def access_token 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 end 18 end
14 19
15 private 20 private
lib/dingtalk/api/suite.rb
@@ -5,10 +5,9 @@ module Dingtalk @@ -5,10 +5,9 @@ module Dingtalk
5 SUITE_ACCESS_TOKEN = "suite_access_token" 5 SUITE_ACCESS_TOKEN = "suite_access_token"
6 EXPIRATION = 7200 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 end 11 end
13 12
14 def get_permanent_code(tmp_auth_code) 13 def get_permanent_code(tmp_auth_code)
@@ -29,13 +28,25 @@ module Dingtalk @@ -29,13 +28,25 @@ module Dingtalk
29 suite_secret: @suite_secret, 28 suite_secret: @suite_secret,
30 suite_ticket: suite_ticket 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 redis.set(SUITE_ACCESS_TOKEN, res['suite_access_token']) 33 redis.set(SUITE_ACCESS_TOKEN, res['suite_access_token'])
35 redis.expire(SUITE_ACCESS_TOKEN, EXPIRATION) 34 redis.expire(SUITE_ACCESS_TOKEN, EXPIRATION)
36 redis.get(SUITE_ACCESS_TOKEN) 35 redis.get(SUITE_ACCESS_TOKEN)
37 end 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 def suite_ticket 50 def suite_ticket
40 redis.get(SUITE_TICKET) 51 redis.get(SUITE_TICKET)
41 end 52 end
lib/dingtalk/config.rb
@@ -10,9 +10,17 @@ module Dingtalk @@ -10,9 +10,17 @@ module Dingtalk
10 return nil unless self.config 10 return nil unless self.config
11 @redis ||= config.redis 11 @redis ||= config.redis
12 end 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 end 21 end
14 22
15 class Config 23 class Config
16 - attr_accessor :redis 24 + attr_accessor :redis, :redis_options, :suite_key, :suite_secret
17 end 25 end
18 end 26 end