Commit d1bf8bdb by Francis Zhou

add get_access_token, get_int_user_id method

parent 6195bcf7
...@@ -10,7 +10,7 @@ Gem::Specification.new do |spec| ...@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
spec.email = ["francis.tm@gmail.com"] spec.email = ["francis.tm@gmail.com"]
spec.summary = %q{Dingtalk integration} spec.summary = %q{Dingtalk integration}
spec.description = %q{Dingtalk server api SKD} spec.description = %q{Dingtalk ruby SKD}
spec.homepage = "http://github.com" spec.homepage = "http://github.com"
spec.license = "MIT" spec.license = "MIT"
...@@ -36,6 +36,10 @@ Gem::Specification.new do |spec| ...@@ -36,6 +36,10 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"] spec.require_paths = ["lib"]
spec.add_runtime_dependency "httparty", "~> 0.18"
spec.add_runtime_dependency "activesupport", "~> 5.0"
spec.add_development_dependency "pry", "~> 0.13"
spec.add_development_dependency "webmock", "~> 3.8" spec.add_development_dependency "webmock", "~> 3.8"
spec.add_development_dependency "bundler", "~> 1.17" spec.add_development_dependency "bundler", "~> 1.17"
spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rake", "~> 10.0"
......
require "dingtalk/core"
require "dingtalk/request_url"
module Dingtalk
module AccessToken
class << self
extend Dingtalk::Core
# 获取 access_token
# @url https://ding-doc.dingtalk.com/doc#/serverapi2/eev437
add_request :get_access_token, :get, Dingtalk::RequestUrl::ACCESS_TOKEN do |request|
request.with_key_and_secret!
end
end
end
end
\ No newline at end of file
require "dingtalk/core"
require "dingtalk/request_url"
module Dingtalk
module Auth
class << self
extend Dingtalk::Core
# 企业内部应用免登
# @url https://ding-doc.dingtalk.com/doc#/serverapi2/clotub
add_request :get_int_user_id, :get, Dingtalk::RequestUrl::GET_USER_INFO do |request|
request.add_arg :code, required: true, in: :query
request.add_arg :access_token, required: true, in: :query
end
end
end
end
\ No newline at end of file
require "json"
require "httparty"
require "active_support"
require "active_support/core_ext/hash/keys"
require "active_support/core_ext/hash/deep_merge"
require "dingtalk"
require "dingtalk/config"
module Dingtalk
module Core
class RequestBuilder
attr_reader :query_args, :payload_args, :required_args
def initialize
@query_args = []
@payload_args = []
@required_args = []
@with_key_and_secret = false
end
def with_key_and_secret!
@with_key_and_secret = true
end
def with_key_and_secret?
@with_key_and_secret
end
def is_arg_required?(arg_name)
@required_args.include? arg_name
end
def add_arg(arg_name, option)
case option[:in]
when :query
@query_args << arg_name
when :payload
@payload_args << arg_name
else
raise Dingtalk::Error.new("unknown argument position")
end
@required_args << arg_name if option[:required]
end
end
# options
# with_key_secret 是否携带 appkey, appsecret 在请求 query 中
def add_request(request_name, method, url)
builder = RequestBuilder.new
yield builder if block_given?
define_method request_name do |method_args = {}|
default_options = {
query: {}
}
request_options = default_options.tap do |h|
if builder.with_key_and_secret?
h[:query].merge!({
appkey: Dingtalk::Config.app_key,
appsecret: Dingtalk::Config.app_secret,
})
end
builder.required_args.each do |arg|
raise Dingtalk::Error.new("missing required argument '#{arg}' when invoke request #{request_name}") \
if method_args[arg].nil?
end
builder.query_args.each do |arg|
h[:query][arg] = method_args[arg] unless method_args[arg].nil?
end
builder.payload_args.each do |arg|
end
end
response = HTTParty.send(method, url, **request_options)
JSON.parse(response.body).deep_symbolize_keys
end
end
end
end
\ No newline at end of file
module Dingtalk
module RequestUrl
# 获取 AccessToken
ACCESS_TOKEN = "https://oapi.dingtalk.com/gettoken".freeze
# 企业内应用免登录获取用户ID
GET_USER_INFO = "https://oapi.dingtalk.com/user/getuserinfo".freeze
end
end
\ No newline at end of file
require "json"
require "dingtalk/access_token"
RSpec.describe Dingtalk::AccessToken do
before :each do
@app_key = "mocked_app_key"
@app_secret = "mocked_app_secret"
@mocked_access_token = "mocked_access_token"
Dingtalk::Config.app_key = @app_key
Dingtalk::Config.app_secret = @app_secret
response_body = {}.tap do |h|
h[:errcode] = 0
h[:errmsg] = 0
h[:access_token] = @mocked_access_token
end
stub_request(:get, /oapi.dingtalk.com/)
.with(query: { appkey: @app_key, appsecret: @app_secret })
.to_return(status: 200, body: response_body.to_json )
end
it "should return correct access_token from request" do
response = Dingtalk::AccessToken.get_access_token
expect(response[:access_token]).to eq(@mocked_access_token)
end
end
\ No newline at end of file
require "json"
require "dingtalk/auth"
RSpec.describe Dingtalk::Auth do
before :each do
@int_user_id = 100
@access_token = "mocked_access_token"
@int_login_code = "mocked_int_login_code"
response_body = {}.tap do |h|
h[:userid] = @int_user_id
h[:is_sys] = false
h[:sys_level] = 0
end
stub_request(:get, /oapi.dingtalk.com\/user\/getuserinfo/)
.with(query: { code: @int_login_code, access_token: @access_token })
.to_return(status: 200, body: response_body.to_json)
end
it 'should get internal app user id' do
response = Dingtalk::Auth.get_int_user_id code: @int_login_code, access_token: @access_token
expect(response[:userid]).to eq(@int_user_id)
expect(response[:is_sys]).to eq(false)
expect(response[:sys_level]).to eq(0)
end
end
\ No newline at end of file
require "dingtalk/config"
RSpec.describe Dingtalk::Config do RSpec.describe Dingtalk::Config do
before :each do before :each do
@app_key = "ci_dingtalk_app_key" @app_key = "ci_dingtalk_app_key"
......
require "pry"
require "bundler/setup" require "bundler/setup"
require "webmock/rspec"
require "dingtalk" require "dingtalk"
require "webmock"
RSpec.configure do |config| RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure # Enable flags like --only-failures and --next-failure
......
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