Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
dingtalk-sdk
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
dingtalk-sdk
Commits
a4e24b2a
Commit
a4e24b2a
authored
Sep 05, 2020
by
Francis Zhou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add access_token cache hook define method
parent
8709e058
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
53 additions
and
9 deletions
+53
-9
Rakefile
Rakefile
+3
-5
console
bin/console
+1
-1
access_token.rb
lib/dingtalk_sdk/access_token.rb
+29
-0
core.rb
lib/dingtalk_sdk/core.rb
+2
-0
access_token_spec.rb
spec/dingtalk_sdk/access_token_spec.rb
+16
-1
auth_spec.rb
spec/dingtalk_sdk/auth_spec.rb
+2
-2
No files found.
Rakefile
View file @
a4e24b2a
...
...
@@ -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
bin/console
View file @
a4e24b2a
...
...
@@ -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.
...
...
lib/dingtalk_sdk/access_token.rb
View file @
a4e24b2a
...
...
@@ -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
lib/dingtalk_sdk/core.rb
View file @
a4e24b2a
...
...
@@ -85,6 +85,8 @@ module DingtalkSdk
end
end
protected
def
add_request
(
request_name
,
method
,
url
)
builder
=
RequestBuilder
.
new
yield
builder
if
block_given?
...
...
spec/dingtalk_sdk/access_token_spec.rb
View file @
a4e24b2a
...
...
@@ -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
spec/dingtalk_sdk/auth_spec.rb
View file @
a4e24b2a
...
...
@@ -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
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment