Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rails_dingtalk
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
other
rails_dingtalk
Commits
5471c943
Commit
5471c943
authored
Sep 12, 2021
by
mingyuan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
common params
parent
80285ebe
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
15 additions
and
19 deletions
+15
-19
base.rb
app/apis/dingtalk/api/base.rb
+5
-5
new.rb
app/apis/dingtalk/api/new.rb
+1
-1
saas.rb
app/apis/dingtalk/api/saas.rb
+7
-11
http_client.rb
app/apis/dingtalk/http_client.rb
+2
-2
No files found.
app/apis/dingtalk/api/base.rb
View file @
5471c943
...
...
@@ -21,25 +21,25 @@ module Dingtalk::Api
end
def
get
(
path
,
params:
{},
headers:
{},
base:
base_host
,
as:
nil
)
with_access_token
(
'get'
,
path
,
params
,
headers
)
do
|
processed_params
,
processed_headers
|
with_access_token
(
'get'
,
path
,
params
,
headers
,
nil
)
do
|
processed_params
,
processed_headers
|
@client
.
get
path
,
headers:
processed_headers
,
params:
processed_params
,
base:
base
,
as:
as
end
end
def
post
(
path
,
params:
{},
headers:
{},
base:
base_host
,
**
payload
)
with_access_token
(
'post'
,
path
,
params
,
headers
)
do
|
processed_params
,
processed_headers
|
@client
.
post
path
,
payload
.
to_json
,
headers:
processed_headers
,
params:
processed_params
,
base:
base
with_access_token
(
'post'
,
path
,
params
,
headers
,
payload
)
do
|
processed_params
,
processed_headers
|
@client
.
post
path
,
payload
,
headers:
processed_headers
,
params:
processed_params
,
base:
base
end
end
def
post_file
(
path
,
file
,
params:
{},
headers:
{},
base:
base_host
)
with_access_token
(
'post'
,
path
,
params
,
headers
)
do
|
processed_params
,
processed_headers
|
with_access_token
(
'post'
,
path
,
params
,
headers
,
nil
)
do
|
processed_params
,
processed_headers
|
@client
.
post_file
path
,
file
,
headers:
processed_headers
,
params:
processed_params
,
base:
base
end
end
protected
def
with_access_token
(
method
,
path
,
params
=
{},
headers
=
{},
tries
=
2
)
def
with_access_token
(
method
,
path
,
params
=
{},
headers
=
{},
payload
=
{},
tries
=
2
)
app
.
refresh_access_token
unless
app
.
access_token_valid?
yield
params
.
merge!
(
access_token:
app
.
access_token
),
headers
rescue
=>
e
...
...
app/apis/dingtalk/api/new.rb
View file @
5471c943
...
...
@@ -20,7 +20,7 @@ module Dingtalk::Api
end
protected
def
with_access_token
(
method
,
path
,
params
=
{},
headers
=
{},
tries
=
2
)
def
with_access_token
(
method
,
path
,
params
=
{},
headers
=
{},
payload
=
{},
tries
=
2
)
app
.
refresh_access_token
unless
app
.
access_token_valid?
yield
params
,
headers
.
merge!
(
Authorization
:
"Bearer
#{
app
.
access_token
}
"
,
'x-acs-dingtalk-access-token'
:
app
.
access_token
)
rescue
=>
e
...
...
app/apis/dingtalk/api/saas.rb
View file @
5471c943
...
...
@@ -5,30 +5,26 @@ module Dingtalk::Api
def
token
headers
=
sign_header
(
'POST'
,
'/gettoken.json'
)
r
=
@client
.
post
'gettoken.json'
,
nil
,
headers:
headers
,
base:
BASE
r
=
@client
.
post
'gettoken.json'
,
headers:
headers
,
base:
BASE
{
'access_token'
=>
r
.
dig
(
'content'
,
'data'
,
'accessToken'
),
'expires_in'
=>
r
.
dig
(
'content'
,
'data'
,
'expiresIn'
)
}
end
def
sign_assess_header
(
method
,
path
,
params
=
{})
sign_header
(
method
,
path
,
params
=
{})
end
protected
def
with_access_token
(
method
,
path
,
params
=
{},
headers
=
{},
tries
=
2
)
def
with_access_token
(
method
,
path
,
params
=
{},
headers
=
{},
payload
=
{},
tries
=
2
)
app
.
refresh_access_token
unless
app
.
access_token_valid?
processed_headers
=
sign_header
(
method
,
path
,
params
=
{})
yield
params
.
merge!
(
access_token:
app
.
access_token
),
processed_headers
processed_params
=
params
.
merge!
(
access_token:
app
.
access_token
)
processed_headers
=
sign_header
(
method
,
path
,
processed_params
,
payload
)
yield
processed_params
,
processed_headers
rescue
=>
e
Rails
.
logger
.
debug
e
.
full_message
app
.
refresh_access_token
retry
unless
(
tries
-=
1
).
zero?
end
def
sign_header
(
method
,
path
,
params
=
{})
def
sign_header
(
method
,
path
,
params
=
{}
,
payload
=
{}
)
headers
=
{
apiKey:
app
.
app_key
,
'X-Hmac-Auth-Timestamp'
:
Time
.
now
.
strftime
(
'%Y-%m-%dT%H:%M:%S.%3N%:z'
),
...
...
@@ -44,7 +40,7 @@ module Dingtalk::Api
headers
[
:'X-Hmac-Auth-Nonce'
],
path
]
result
<<
params
.
to_query
if
params
.
present?
result
<<
params
.
merge
(
payload
).
to_query
if
params
.
present?
signature
=
OpenSSL
::
HMAC
.
digest
(
'SHA256'
,
app
.
app_secret
,
result
.
join
(
"
\n
"
))
headers
.
merge!
'X-Hmac-Auth-Signature'
:
Base64
.
strict_encode64
(
signature
)
end
...
...
app/apis/dingtalk/http_client.rb
View file @
5471c943
...
...
@@ -17,7 +17,7 @@ module Dingtalk
parse_response
(
response
,
options
[
:as
])
end
def
post
(
path
,
payload
,
headers:
{},
params:
{},
base:
nil
,
**
options
)
def
post
(
path
,
payload
=
{}
,
headers:
{},
params:
{},
base:
nil
,
**
options
)
headers
[
'Accept'
]
||=
'application/json'
headers
[
'Content-Type'
]
||=
'application/json'
url
=
base
+
path
...
...
@@ -25,7 +25,7 @@ module Dingtalk
opts
=
{
params:
params
}
opts
.
merge!
(
body:
payload
)
if
payload
.
present?
opts
.
merge!
(
body:
payload
.
to_json
)
if
payload
.
present?
response
=
@http
.
with_headers
(
headers
).
post
(
url
,
**
opts
)
parse_response
(
response
,
options
[
:as
])
...
...
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