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
f1d62302
Commit
f1d62302
authored
Sep 06, 2020
by
Francis Zhou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
will call get_access_token if no cache method
parent
a4e24b2a
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
42 additions
and
33 deletions
+42
-33
.rubocop.yml
.rubocop.yml
+3
-0
Rakefile
Rakefile
+0
-0
dingtalk_sdk.rb
lib/dingtalk_sdk.rb
+2
-2
access_token.rb
lib/dingtalk_sdk/access_token.rb
+5
-6
auth.rb
lib/dingtalk_sdk/auth.rb
+2
-2
corp_conversation.rb
lib/dingtalk_sdk/corp_conversation.rb
+3
-6
department.rb
lib/dingtalk_sdk/department.rb
+2
-2
storage.rb
lib/dingtalk_sdk/storage.rb
+2
-1
user.rb
lib/dingtalk_sdk/user.rb
+3
-3
access_token_spec.rb
spec/dingtalk_sdk/access_token_spec.rb
+13
-5
auth_spec.rb
spec/dingtalk_sdk/auth_spec.rb
+7
-6
No files found.
.rubocop.yml
View file @
f1d62302
Style/AsciiComments
:
Enabled
:
false
Style/Documentation
:
Enabled
:
false
\ No newline at end of file
Rakefile
View file @
f1d62302
lib/dingtalk_sdk.rb
View file @
f1d62302
...
...
@@ -24,8 +24,8 @@ module DingtalkSdk
end
class
Signature
def
initialize
(
s
)
@signature
=
s
def
initialize
(
s
ignature
)
@signature
=
s
ignature
end
def
to_s
...
...
lib/dingtalk_sdk/access_token.rb
View file @
f1d62302
...
...
@@ -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
...
...
lib/dingtalk_sdk/auth.rb
View file @
f1d62302
...
...
@@ -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
# 生成扫码登录跳转地址
...
...
lib/dingtalk_sdk/corp_conversation.rb
View file @
f1d62302
...
...
@@ -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
lib/dingtalk_sdk/department.rb
View file @
f1d62302
...
...
@@ -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
lib/dingtalk_sdk/storage.rb
View file @
f1d62302
...
...
@@ -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
lib/dingtalk_sdk/user.rb
View file @
f1d62302
...
...
@@ -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
spec/dingtalk_sdk/access_token_spec.rb
View file @
f1d62302
...
...
@@ -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_secre
t
|
app_key
==
@app_key
&&
app_secret
==
@app_secret
?
ak1
:
ak2
DingtalkSdk
::
Request
.
set_access_token_cache_method
do
|
reques
t
|
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
)
...
...
spec/dingtalk_sdk/auth_spec.rb
View file @
f1d62302
...
...
@@ -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
)
...
...
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