From 226a4c284fb7721fc05d5d25c38eb32d4d15cd50 Mon Sep 17 00:00:00 2001 From: JiangYongKang Date: Mon, 14 Sep 2020 13:28:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B4=A6=E5=8F=B7=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=92=8C=E8=BF=90=E8=90=A5=E7=AE=A1=E7=90=86=E7=9A=84?= =?UTF-8?q?API=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tim_sdk/api.rb | 102 ++++++++++++++++++++++++++++++++++++++++++- spec/tim_sdk_spec.rb | 53 ++++++++++++++++++++++ 2 files changed, 154 insertions(+), 1 deletion(-) diff --git a/lib/tim_sdk/api.rb b/lib/tim_sdk/api.rb index a7a5b6c..5db0e7f 100644 --- a/lib/tim_sdk/api.rb +++ b/lib/tim_sdk/api.rb @@ -16,8 +16,108 @@ def self.connection end end + # 导入单个帐号 + def self.invoke_account_import(identifier, nick = nil, face_url = nil) + response = connection.post('/v4/im_open_login_svc/account_import') do |request| + request_body = { :Identifier => identifier.to_s } + request_body.merge!(:Nick => nick) if nick + request_body.merge!(:FaceUrl => face_url) if face_url + request.body = request_body.to_json + end + raise TimServerError, "Response Status: #{response.status}" unless response.success? + JSON.parse(response.body, symbolize_names: true) if response.success? + end + + # 导入多个账号 + def self.invoke_multi_account_import(accounts) + response = connection.post('/v4/im_open_login_svc/multiaccount_import') do |request| + request.body = { + :Accounts => accounts.map(&:to_s) + }.to_json + end + raise TimServerError, "Response Status: #{response.status}" unless response.success? + JSON.parse(response.body, symbolize_names: true) if response.success? + end + + # 删除账号 + def self.invoke_account_delete(accounts) + response = connection.post('/v4/im_open_login_svc/account_delete') do |request| + request.body = { + :DeleteItem => accounts.map do |account| + { + :UserID => account.to_s + } + end + }.to_json + end + raise TimServerError, "Response Status: #{response.status}" unless response.success? + JSON.parse(response.body, symbolize_names: true) if response.success? + end + + # 查询账号 + def self.invoke_account_check(accounts) + response = connection.post('/v4/im_open_login_svc/account_check') do |request| + request.body = { + :CheckItem => accounts.map do |account| + { + :UserID => account.to_s + } + end + }.to_json + end + raise TimServerError, "Response Status: #{response.status}" unless response.success? + JSON.parse(response.body, symbolize_names: true) if response.success? + end + + # 失效帐号登录态 + def self.invoke_kick(identifier) + response = connection.post('/v4/im_open_login_svc/kick') do |request| + request.body = { + :Identifier => identifier.to_s + }.to_json + end + raise TimServerError, "Response Status: #{response.status}" unless response.success? + JSON.parse(response.body, symbolize_names: true) if response.success? + end + + # 查询帐号在线状态 + def self.invoke_query_state(accounts, is_need_detail = 0) + response = connection.post('/v4/openim/querystate') do |request| + request.body = { + :To_Account => accounts.map(&:to_s), + :IsNeedDetail => is_need_detail + }.to_json + end + raise TimServerError, "Response Status: #{response.status}" unless response.success? + JSON.parse(response.body, symbolize_names: true) if response.success? + end + + # 拉取运营数据 + def self.invoke_fetch_app_info(fields = []) + response = connection.post('/v4/openconfigsvr/getappinfo') do |request| + request.body = { + :RequestField => fields + }.to_json + end + raise TimServerError, "Response Status: #{response.status}" unless response.success? + JSON.parse(response.body, symbolize_names: true) if response.success? + end + + + # 下载消息记录 + def self.invoke_fetch_history(chat_type, msg_time) + response = connection.post('/v4/open_msg_svc/get_history') do |request| + request.body = { + :ChatType => chat_type, + :MsgTime => msg_time, + }.to_json + end + raise TimServerError, "Response Status: #{response.status}" unless response.success? + JSON.parse(response.body, symbolize_names: true) if response.success? + end + + # 获取服务器 IP 地址 - # 基于安全等考虑,您可能需要获知服务器的 IP 地址列表,以便进行相关限制。App 管理员可以通过该接口获得 SDK、第三方回调所使用到的服务器 IP 地址列表或 IP 网段信息。 def self.invoke_fetch_ip_list response = connection.post('/v4/ConfigSvc/GetIPList') do |request| request.body = {}.to_json diff --git a/spec/tim_sdk_spec.rb b/spec/tim_sdk_spec.rb index f7fd8bb..67fef22 100644 --- a/spec/tim_sdk_spec.rb +++ b/spec/tim_sdk_spec.rb @@ -25,6 +25,59 @@ expect(signature.size).to be > 0 end + it 'should be imported successfully' do + response = TimSdk::Api.invoke_account_import('foo') + expect(response[:ActionStatus]).to eq('OK') + expect(response[:ErrorCode]).to eq(0) + end + + it 'should be imported multi account successfully' do + response = TimSdk::Api.invoke_multi_account_import(%w[foo bar]) + expect(response[:ActionStatus]).to eq('OK') + expect(response[:ErrorCode]).to eq(0) + end + + it 'should be kick account successfully' do + response = TimSdk::Api.invoke_kick('foo') + expect(response[:ActionStatus]).to eq('OK') + expect(response[:ErrorCode]).to eq(0) + end + + it 'should be check account successfully' do + response = TimSdk::Api.invoke_account_check(%w[foo bar]) + expect(response[:ActionStatus]).to eq('OK') + expect(response[:ErrorCode]).to eq(0) + end + + it 'should be query account state successfully' do + response = TimSdk::Api.invoke_query_state(%w[foo bar]) + expect(response[:ActionStatus]).to eq('OK') + expect(response[:ErrorCode]).to eq(0) + response = TimSdk::Api.invoke_query_state(%w[foo bar], 1) + expect(response[:ActionStatus]).to eq('OK') + expect(response[:ErrorCode]).to eq(0) + end + + it 'should be delete account successfully' do + response = TimSdk::Api.invoke_account_delete(%w[foo bar]) + expect(response[:ActionStatus]).to eq('OK') + expect(response[:ErrorCode]).to eq(0) + end + + it 'should return app info' do + response = TimSdk::Api.invoke_fetch_app_info + expect(response[:ErrorInfo]).to eq('OK') + expect(response[:ErrorCode]).to eq(0) + expect(response).to have_key(:Result) + end + + it 'should return message histories' do + response = TimSdk::Api.invoke_fetch_history('C2C', '2020091116') + expect(response).to have_key(:ActionStatus) + expect(response).to have_key(:ErrorInfo) + expect(response).to have_key(:ErrorCode) + end + it 'should return a list of IP addresses' do response = TimSdk::Api.invoke_fetch_ip_list expect(response[:ActionStatus]).to eq('OK')