Project

General

Profile

« Previous | Next » 

Revision 258a439a

Added by Marc Dequènes about 13 years ago

  • ID 258a439a59e2be7ce7dcb2e68acf3b744e01f91f

[evol] Botnet: propagate API calls environment (user token and language prefs at the moment), propagate session a bit further too in order to implement a more dynamic API tree

View differences:

lib/cyborghood/cyborg/botnet/interface.rb
node '?', :hidden => true do
on_request do |request|
if request.args.empty?
request.reply.results = lookup_node.__send__(:visible_nodes_names)
request.reply.results = lookup_node.__send__(:visible_nodes_names, request)
else
request.reply.results = list.nil? ? search_among_children(request, lookup_node) :
search_in_list(request, lookup_node, list)
......
@request_cb = cb
end
def _is_node?(session, node_path)
node = __send__(:find_node, session, node_path)
def _is_node?(session, env, node_path)
node = __send__(:find_node, session, env, node_path)
not node.nil?
end
def _call(session, node_path, args = nil, &send_result_cb)
def _call(session, env, node_path, args = nil, &send_result_cb)
args ||= []
raise CyberError.new(:unrecoverable, 'api/cyborghood', "wrong format for arguments when calling node '#{node_path}'") unless args.is_a? Array
node = find_node(session, node_path)
node = find_node(session, env, node_path)
raise CyberError.new(:unrecoverable, 'api/cyborghood', "unknown node '#{node_path}'") if node.nil?
logger.debug "[Server API] Node '#{node_path}' found"
node.__send__(:request, session, args, &send_result_cb)
# TODO: validate environment and setup things (choosen locale for example)
node.__send__(:request, session, env, args, &send_result_cb)
end
protected
......
end
end
def find_node(session, node_path)
def find_node(session, env, node_path)
# node_path is a string argument when interface root node is called, but is a list of node elements later on
if root?
logger.debug "[Server API] Looking for node '#{node_path}'"
......
if node_element.nil?
return self
else
next_node = find_child_node(node_element)
next_node = find_child_node(session, env, node_element)
if next_node
next_node.__send__(:load, node_element)
return next_node.__send__(:find_node, session, node_path)
return next_node.__send__(:find_node, session, env, node_path)
else
return
end
end
end
def find_child_node(child_node)
def find_child_node(session, env, child_node)
return @nodes[child_node] if @nodes.has_key? child_node
@nodes.each_pair do |match, node|
......
elsif match.is_a? Regexp
child_node =~ Regexp.new(match)
elsif match.is_a? Proc
match.call.include? child_node
match.call(session, env).include? child_node
end
return node if found
end
......
end
class Request
attr_reader :session, :args, :reply
attr_reader :session, :env, :args, :reply
def initialize(session, args = [], &send_result_cb)
def initialize(session, env, args = [], &send_result_cb)
@session = session
@env = env.to_ostruct
@args = args
@send_result_cb = send_result_cb
......
end
end
def request(session, args = [], &send_result_cb)
request = Request.new(session, args, &send_result_cb)
def request(session, env, args = [], &send_result_cb)
request = Request.new(session, env, args, &send_result_cb)
if @request_cb
begin
......
raise CyberError.new(:unrecoverable, 'api/cyborghood', "call failed on node '#{node_path}': " + $!)
end
else
request.reply.results = visible_nodes_names
request.reply.results = visible_nodes_names(request)
request.send_reply
end
end
......
Hash[@nodes.select{|match, node| not node.hidden? }]
end
def node_match_to_name(match)
def node_match_to_name(request, match)
if match.is_a? String
match
elsif match.is_a? Regexp
'/' + match.to_s + '/'
elsif match.is_a? Proc
match.call
match.call(request.session, request.env)
end
end
def visible_nodes_names
def visible_nodes_names(request)
visible_nodes.keys.collect do |match|
node_match_to_name(match)
node_match_to_name(request, match)
end.compact.flatten
end
......
if match.is_a? String
match_list = [match]
elsif match.is_a? Proc
match_list = match.call
match_list = match.call(request.session, request.env)
else
next
end
# TODO: filter by auth token
match_list.each do |child_node_name|
node.__send__(:load, child_node_name)
result = node.__send__(:request, request.session)
result = node.__send__(:request, request.session, request.env)
next unless result.respond_to? :to_hash
child_node_attrs = result.to_hash
......
return {} unless criterias.is_a? Hash
if list.is_a? Proc
data = list.call(criterias)
data = list.call(request.session, request.env, criterias)
return data[:list] unless data[:post_filter]
end

Also available in: Unified diff