forked from Nigel/forgeplus
66 lines
1.7 KiB
Ruby
66 lines
1.7 KiB
Ruby
class ApplicationQuery
|
|
include Callable
|
|
|
|
private
|
|
|
|
def strip_param(key)
|
|
params[key].to_s.strip.presence
|
|
end
|
|
|
|
|
|
# author: zxh
|
|
# add blockchain related functions in application_query
|
|
|
|
def invoke_blockchain_api(uri, params)
|
|
begin
|
|
uri = URI.parse(URI.encode(uri.strip))
|
|
res = Net::HTTP.start(uri.host, uri.port) do |http|
|
|
req = Net::HTTP::Post.new(uri)
|
|
req['Content-Type'] = 'application/json'
|
|
req.body = params
|
|
http.request(req)
|
|
end
|
|
if res.code.to_i != 200
|
|
puts '区块链接口请求失败.'
|
|
return false
|
|
else
|
|
res_body = JSON.parse(res.body)
|
|
if res_body.has_key?("status") && res_body["status"] == 0
|
|
else
|
|
puts '区块链接口请求出错.'
|
|
return false
|
|
end
|
|
end
|
|
|
|
return res_body
|
|
rescue Exception => e
|
|
puts '区块链接口请求失败.'
|
|
return false
|
|
end
|
|
end
|
|
|
|
|
|
# find all the repos that a user has tokens
|
|
def find_repo_with_token(user_id)
|
|
param = {
|
|
"request-type": "query user balance of all repos",
|
|
"username": user_id.to_s
|
|
}.to_json
|
|
resp_body = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
|
|
token_list = resp_body['UserBalanceList'].nil? ? [] : resp_body['UserBalanceList']
|
|
return token_list
|
|
end
|
|
|
|
|
|
# find one repo that a user has tokens
|
|
def find_one_balance(user_id, project_id)
|
|
param = {
|
|
"request-type": "query user balance of single repo",
|
|
"username": user_id.to_s,
|
|
"token_name": project_id.to_s
|
|
}.to_json
|
|
resp_body = invoke_blockchain_api(Blockchain.blockchain_config[:api_url], param)
|
|
return resp_body
|
|
end
|
|
|
|
end |