Commit b5f2d6d4 by mingyuan

new api acces in header

parent a48066c3
......@@ -19,5 +19,33 @@ module Dingtalk::Api
BASE
end
def get(path, params: {}, headers: {}, base: base_host, as: nil)
with_access_token(headers) do |with_token_headers|
@client.get path, headers: with_token_headers, params: params, base: base, as: as
end
end
def post(path, params: {}, headers: {}, base: base_host, **payload)
with_access_token(headers) do |with_token_headers|
@client.post path, payload.to_json, headers: with_token_headers, params: params, base: base
end
end
def post_file(path, file, params: {}, headers: {}, base: base_host)
with_access_token(headers) do |with_token_headers|
@client.post_file path, file, headers: with_token_headers, params: params, base: base
end
end
protected
def with_access_token(headers = {}, tries = 2)
app.refresh_access_token unless app.access_token_valid?
yield headers.merge!(Authorization: "Bearer #{app.access_token}")
rescue => e
Rails.logger.debug e.full_message
app.refresh_access_token
retry unless (tries -= 1).zero?
end
end
end
......@@ -70,11 +70,11 @@ module Dingtalk
# 40001, invalid credential, access_token is invalid or not latest hint
# 48001, api unauthorized hint, should not handle here # GH-230
when 42001, 40014, 40001, 41001
raise Wechat::AccessTokenExpiredError
raise Dingtalk::AccessTokenExpiredError
# 40029, invalid code for mp # GH-225
# 43004, require subscribe hint # GH-214
when 2
raise Wechat::ResponseError.new(data['errcode'], data['errmsg'])
raise Dingtalk::ResponseError.new(data['errcode'], data['errmsg'])
else
data
end
......
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