Commit a4e24b2a by Francis Zhou

add access_token cache hook define method

parent 8709e058
......@@ -5,9 +5,6 @@ require 'rspec/core/rake_task'
require 'rubocop/rake_task'
RSpec::Core::RakeTask.new(:spec)
RuboCop::RakeTask.new(:rubocop)
task default: :spec
RuboCop::RakeTask.new(:rubocop) do |task|
task.options = ['-A']
end
task default: :spec
\ No newline at end of file
......@@ -2,7 +2,7 @@
# frozen_string_literal: true
require 'bundler/setup'
require 'dingtalk'
require 'dingtalk_sdk'
# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
......
......@@ -15,5 +15,34 @@ module DingtalkSdk
request.add_const :appkey, ->(r) { r.app_key }, in: :query
request.add_const :appsecret, ->(r) { r.app_secret }, in: :query
end
# 获取缓存的 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
self.class.class_variable_get(method_name).call(agent_id, app_key, app_secret)
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) }
end
def unset_access_token_cache_method
remove_class_variable :@@ak_cache_method if class_variable_defined?(AK_CACHE_METHOD_NAME)
end
end
def self.included(base)
base.extend(ClassMethods)
end
end
end
......@@ -85,6 +85,8 @@ module DingtalkSdk
end
end
protected
def add_request(request_name, method, url)
builder = RequestBuilder.new
yield builder if block_given?
......
......@@ -23,7 +23,7 @@ RSpec.describe DingtalkSdk::AccessToken do
h[:access_token] = @mocked_access_token
end
stub_request(:get, /oapi.dingtalk.com/)
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
......@@ -33,4 +33,19 @@ RSpec.describe DingtalkSdk::AccessToken do
expect(response[:access_token]).to eq(@mocked_access_token)
end
it 'should invoke configured access_token_persist method' 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
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')
expect(request_hit.cached_access_token).to eq(ak1)
expect(request_miss.cached_access_token).to eq(ak2)
end
end
......@@ -47,7 +47,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 +56,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,
......
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