Commit f1d62302 by Francis Zhou

will call get_access_token if no cache method

parent a4e24b2a
Style/AsciiComments:
Enabled: false
Style/Documentation:
Enabled: false
\ No newline at end of file
......@@ -7,4 +7,4 @@ require 'rubocop/rake_task'
RSpec::Core::RakeTask.new(:spec)
RuboCop::RakeTask.new(:rubocop)
task default: :spec
\ No newline at end of file
task default: :spec
......@@ -24,8 +24,8 @@ module DingtalkSdk
end
class Signature
def initialize(s)
@signature = s
def initialize(signature)
@signature = signature
end
def to_s
......
......@@ -17,27 +17,26 @@ module DingtalkSdk
end
# 获取缓存的 access_token
# 如果未设置 会自动调用 get_access_token 获取一个新的 access_token
# 通过 Request.set_access_token_cache_method 定义缓存方法
# @return [Hash]
def cached_access_token
method_name = :@@ak_cache_method
unless self.class.class_variable_defined? method_name
raise DingtalkSdk::Error, 'access_token cache method not found'
end
return get_access_token unless self.class.class_variable_defined? method_name
self.class.class_variable_get(method_name).call(agent_id, app_key, app_secret)
self.class.class_variable_get(method_name).call(self)
end
module ClassMethods
def set_access_token_cache_method
raise ArgumentError, 'invalid access_token cache method' unless block_given?
class_variable_set :@@ak_cache_method, ->(agent_id, app_key, app_secret) { yield(agent_id, app_key, app_secret) }
class_variable_set :@@ak_cache_method, ->(request) { yield request }
end
def unset_access_token_cache_method
remove_class_variable :@@ak_cache_method if class_variable_defined?(AK_CACHE_METHOD_NAME)
remove_class_variable :@@ak_cache_method if class_variable_defined?(@@ak_cache_method)
end
end
......
......@@ -10,11 +10,11 @@ module DingtalkSdk
# 企业内部应用免登录 用户ID获取
# {https://ding-doc.dingtalk.com/doc#/serverapi2/clotub}
# @!method get_int_login_free_user_id(code:, access_token:)
# @!method get_int_login_free_user_id(code:)
# @return [Hash]
add_request :get_int_login_free_user_id, :get, DingtalkSdk::RequestUrl::INT_LOGIN_FREE_GET_USER_INFO do |request|
request.add_arg :code, required: true, in: :query
request.add_arg :access_token, required: true, in: :query
request.add_const :access_token, ->(r) { r.cached_access_token }, in: :query
end
# 生成扫码登录跳转地址
......
......@@ -29,9 +29,8 @@ module DingtalkSdk
add_request :get_corp_conversation_progress, :post, DingtalkSdk::RequestUrl::CORP_CONVERSATION_SEND_PROGRESS do |request|
request.add_arg :task_id, in: :body, required: true
request.add_arg :access_token, in: :query, required: true
request.add_const :agent_id, ->(r) { r.agent_id }, in: :body
request.add_const :access_token, ->(r) { r.cached_access_token }, in: :query
end
# 查询工作通知消息的发送结果
......@@ -41,9 +40,8 @@ module DingtalkSdk
add_request :get_corp_conversation_result, :post, DingtalkSdk::RequestUrl::CORP_CONVERSATION_SEND_RESULT do |request|
request.add_arg :task_id, in: :body, required: true
request.add_arg :access_token, in: :query, required: true
request.add_const :agent_id, ->(r) { r.agent_id }, in: :body
request.add_const :access_token, ->(r) { r.cached_access_token }, in: :query
end
# 工作通知消息撤回
......@@ -53,9 +51,8 @@ module DingtalkSdk
add_request :recall_corp_conversation, :post, DingtalkSdk::RequestUrl::RECALL_CORP_CONVERSATION do |request|
request.add_arg :msg_task_id, in: :body, required: true
request.add_arg :access_token, in: :query, required: true
request.add_const :agent_id, ->(r) { r.agent_id }, in: :body
request.add_const :access_token, ->(r) { r.cached_access_token }, in: :query
end
end
end
......@@ -14,7 +14,7 @@ module DingtalkSdk
add_request :get_department_list, :get, DingtalkSdk::RequestUrl::GET_DEPARTMENT_LIST do |request|
request.add_arg :id, in: :query # 父部门id(如果不传,默认部门为根部门,根部门ID为1)
request.add_arg :fetch_child, in: :query # 是否递归部门的全部子部门
request.add_arg :access_token, required: true, in: :query
request.add_const :access_token, ->(r) { r.cached_access_token }, in: :query
end
# 获取部门详情
......@@ -23,7 +23,7 @@ module DingtalkSdk
# @return [Hash]
add_request :get_department_profile, :get, DingtalkSdk::RequestUrl::GET_DEPARTMENT_PROFILE do |request|
request.add_arg :id, in: :query, required: true
request.add_arg :access_token, in: :query, required: true
request.add_const :access_token, ->(r) { r.cached_access_token }, in: :query
end
end
end
......@@ -13,8 +13,9 @@ module DingtalkSdk
# @return [Hash]
add_request :upload_media, :post, DingtalkSdk::RequestUrl::UPLOAD_MEDIA_FILE do |request|
request.add_arg :type, in: query, required: true
request.add_arg :access_token, in: :query, required: true
request.add_arg :media, in: :body, required: true
request.add_const :access_token, ->(r) { r.cached_access_token }, in: :query
end
end
end
......@@ -13,7 +13,7 @@ module DingtalkSdk
# @return [Hash]
add_request :get_user_profile, :get, DingtalkSdk::RequestUrl::GET_USER_PROFILE do |request|
request.add_arg :userid, required: true, in: :query
request.add_arg :access_token, required: true, in: :query
request.add_const :access_token, ->(r) { r.cached_access_token }, in: :query
end
# 根据 unionId 获取 userId
......@@ -22,7 +22,7 @@ module DingtalkSdk
# @return [Hash]
add_request :get_userid_by_unionid, :get, DingtalkSdk::RequestUrl::GET_USERID_FROM_UNIONID do |request|
request.add_arg :unionid, required: true, in: :query
request.add_arg :access_token, required: true, in: :query
request.add_const :access_token, ->(r) { r.cached_access_token }, in: :query
end
# 根据手机号获取 userId
......@@ -31,7 +31,7 @@ module DingtalkSdk
# @return [Hash]
add_request :get_userid_by_mobile, :get, DingtalkSdk::RequestUrl::GET_USERID_FROM_MOBILE do |request|
request.add_arg :mobile, required: true, in: :query
request.add_arg :access_token, required: true, in: :query
request.add_const :access_token, ->(r) { r.cached_access_token }, in: :query
end
end
end
......@@ -23,7 +23,7 @@ RSpec.describe DingtalkSdk::AccessToken do
h[:access_token] = @mocked_access_token
end
stub_request(:get, %r{oapi\.dingtalk\.com/gettoken})
stub_request(:get, %r{oapi.dingtalk.com/gettoken})
.with(query: { appkey: @app_key, appsecret: @app_secret })
.to_return(status: 200, body: response_body.to_json)
end
......@@ -38,12 +38,20 @@ RSpec.describe DingtalkSdk::AccessToken do
ak1 = @mocked_access_token
ak2 = 'another_mocked_access_token'
DingtalkSdk::Request.set_access_token_cache_method do |_agent_id, app_key, app_secret|
app_key == @app_key && app_secret == @app_secret ? ak1 : ak2
DingtalkSdk::Request.set_access_token_cache_method do |request|
request.app_key == @app_key && request.app_secret == @app_secret ? ak1 : ak2
end
request_hit = DingtalkSdk::Request.new(agent_id: @agent_id, app_key: @app_key, app_secret: @app_secret)
request_miss = DingtalkSdk::Request.new(agent_id: @agent_id, app_key: 'another_app_key', app_secret: 'another_app_secret')
request_hit = DingtalkSdk::Request.new(
agent_id: @agent_id,
app_key: @app_key,
app_secret: @app_secret
)
request_miss = DingtalkSdk::Request.new(
agent_id: @agent_id,
app_key: 'another_app_key',
app_secret: 'another_app_secret'
)
expect(request_hit.cached_access_token).to eq(ak1)
expect(request_miss.cached_access_token).to eq(ak2)
......
......@@ -19,6 +19,10 @@ RSpec.describe DingtalkSdk::Auth do
@int_login_code = 'mocked_int_login_code'
@timestamp = (Time.now.localtime('+08:00').to_f * 1000).to_i
DingtalkSdk::Request.set_access_token_cache_method do
@access_token
end
@signature = DingtalkSdk.login_free_signature(@app_secret, timestamp: @timestamp)
@dingtalk_request = DingtalkSdk::Request.new(
......@@ -47,7 +51,7 @@ RSpec.describe DingtalkSdk::Auth do
}
end
stub_request(:get, %r{oapi\.dingtalk\.com/user/getuserinfo})
stub_request(:get, %r{oapi.dingtalk.com/user/getuserinfo})
.with(
query: {
code: @int_login_code,
......@@ -56,7 +60,7 @@ RSpec.describe DingtalkSdk::Auth do
)
.to_return(status: 200, body: get_user_info_response_body.to_json)
stub_request(:post, %r{oapi\.dingtalk\.com/sns/getuserinfo_bycode})
stub_request(:post, %r{oapi.dingtalk.com/sns/getuserinfo_bycode})
.with(
query: {
accessKey: @app_key,
......@@ -70,10 +74,7 @@ RSpec.describe DingtalkSdk::Auth do
end
it 'should get user id in internal app login-free scenario' do
response = @dingtalk_request.get_int_login_free_user_id(
code: @int_login_code,
access_token: @access_token
)
response = @dingtalk_request.get_int_login_free_user_id(code: @int_login_code)
expect(response[:userid]).to eq(@int_user_id)
expect(response[:is_sys]).to eq(false)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment