class RallyAPI::RallyJsonConnection
Constants
- DEFAULT_PAGE_SIZE
Attributes
find_threads[R]
logger[R]
low_debug[RW]
rally_headers[RW]
rally_http_client[R]
Public Class Methods
new(headers, low_debug, proxy_info)
click to toggle source
# File lib/rally_api/rally_json_connection.rb, line 20 def initialize(headers, low_debug, proxy_info) @rally_headers = headers @low_debug = low_debug @logger = nil @rally_http_client = HTTPClient.new @rally_http_client.protocol_retry_count = 2 @rally_http_client.connect_timeout = 300 @rally_http_client.receive_timeout = 300 @rally_http_client.send_timeout = 300 @rally_http_client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE @rally_http_client.transparent_gzip_decompression = true #@rally_http_client.debug_dev = STDOUT #passed in proxy setup overrides env level proxy env_proxy = ENV["http_proxy"] #todo - this will go in the future env_proxy = ENV["rally_proxy"] if env_proxy.nil? if (!env_proxy.nil?) && (proxy_info.nil?) @rally_http_client.proxy = env_proxy end @rally_http_client.proxy = proxy_info unless proxy_info.nil? @find_threads = 4 end
Public Instance Methods
add_security_key(keyval)
click to toggle source
# File lib/rally_api/rally_json_connection.rb, line 69 def add_security_key(keyval) @security_token = keyval end
check_for_errors(result)
click to toggle source
# File lib/rally_api/rally_json_connection.rb, line 158 def check_for_errors(result) errors = [] warnings = [] if !result["OperationResult"].nil? errors = result["OperationResult"]["Errors"] || [] warnings = result["OperationResult"]["Warnings"] || [] elsif !result["QueryResult"].nil? errors = result["QueryResult"]["Errors"] || [] warnings = result["QueryResult"]["Warnings"] || [] elsif !result["CreateResult"].nil? errors = result["CreateResult"]["Errors"] || [] warnings = result["CreateResult"]["Warnings"] || [] end {:errors => errors, :warnings => warnings} end
get_all_json_results(url, args, query_params, limit = 99999)
click to toggle source
# File lib/rally_api/rally_json_connection.rb, line 91 def get_all_json_results(url, args, query_params, limit = 99999) all_results = [] args[:method] = :get params = {} params[:pagesize] = query_params[:pagesize] || DEFAULT_PAGE_SIZE params[:start] = 1 params = params.merge(query_params) query_result = send_request(url, args, params) all_results.concat(query_result["QueryResult"]["Results"]) totals = query_result["QueryResult"]["TotalResultCount"] limit < totals ? stop = limit : stop = totals page = params[:pagesize] + 1 page_num = 2 query_array = [] page.step(stop, params[:pagesize]) do |new_page| params[:start] = new_page query_array.push({:page_num => page_num, :url => url, :args => args, :params => params.dup}) page_num = page_num + 1 end all_res = [] all_res = run_threads(query_array) if query_array.length > 0 #stitch results back together in order all_res.each { |page_res| all_results.concat(page_res[:results]["QueryResult"]["Results"]) } query_result["QueryResult"]["Results"] = all_results query_result end
logger=(log_dev)
click to toggle source
# File lib/rally_api/rally_json_connection.rb, line 73 def logger=(log_dev) @logger = log_dev @rally_http_client.debug_dev = log_dev end
send_request(url, args, url_params = {})
click to toggle source
args should have :method
# File lib/rally_api/rally_json_connection.rb, line 123 def send_request(url, args, url_params = {}) method = args[:method] req_args = {} url_params = {} if url_params.nil? url_params[:key] = @security_token unless @security_token.nil? req_args[:query] = url_params if url_params.keys.length > 0 req_args[:header] = setup_request_headers(args[:method]) if (args[:method] == :post) || (args[:method] == :put) text_json = args[:payload].to_json req_args[:body] = text_json end begin log_info("Rally API calling #{method} - #{url} with #{req_args}\n With cookies: #{@rally_http_client.cookie_manager.cookies}") response = @rally_http_client.request(method, url, req_args) rescue Exception => ex msg = "RallyAPI: - rescued exception - #{ex.message} on request to #{url} with params #{url_params}" log_info(msg) raise StandardError, msg end log_info("RallyAPI response was - #{response.inspect}") if response.status_code != 200 msg = "RallyAPI - HTTP-#{response.status_code} on request - #{url}." msg << "\nResponse was: #{response.body}" raise StandardError, msg end json_obj = JSON.parse(response.body) #todo handle null post error errs = check_for_errors(json_obj) raise StandardError, "\nError on request - #{url} - \n#{errs}" if errs[:errors].length > 0 json_obj end
set_auth(auth_info)
click to toggle source
# File lib/rally_api/rally_json_connection.rb, line 45 def set_auth(auth_info) if auth_info[:api_key].nil? set_client_user(auth_info[:base_url], auth_info[:username], auth_info[:password]) else set_api_key(auth_info) end end
set_find_threads(num_threads = 2)
click to toggle source
you can have any number you want as long as it is between 1 and 4
# File lib/rally_api/rally_json_connection.rb, line 84 def set_find_threads(num_threads = 2) return if num_threads.class != Fixnum num_threads = 4 if num_threads > 4 num_threads = 1 if num_threads < 1 @find_threads = num_threads end
set_ssl_verify_mode(mode = OpenSSL::SSL::VERIFY_NONE)
click to toggle source
# File lib/rally_api/rally_json_connection.rb, line 53 def set_ssl_verify_mode(mode = OpenSSL::SSL::VERIFY_NONE) @rally_http_client.ssl_config.verify_mode = mode end
setup_security_token(security_url)
click to toggle source
[]todo - handle token expiration more gracefully - eg handle renewing
# File lib/rally_api/rally_json_connection.rb, line 58 def setup_security_token(security_url) reset_cookies begin json_response = send_request(security_url, { :method => :get }) @security_token = json_response[json_response.keys[0]]["SecurityToken"] rescue StandardError => ex raise unless (ex.message.include?("HTTP-404") || ex.message.include?("HTTP-500")) #for on-prem not on wsapi 2.x end true end
Private Instance Methods
log_info(message)
click to toggle source
# File lib/rally_api/rally_json_connection.rb, line 222 def log_info(message) return unless @low_debug puts message if @logger.nil? @logger.debug(message) unless @logger.nil? end
run_single_thread(request_array)
click to toggle source
# File lib/rally_api/rally_json_connection.rb, line 211 def run_single_thread(request_array) Thread.new do thread_results = [] request_array.each do |req| page_res = send_request(req[:url], req[:args], req[:params]) thread_results.push({:page_num => req[:page_num], :results => page_res}) end thread_results end end
run_threads(query_array)
click to toggle source
# File lib/rally_api/rally_json_connection.rb, line 195 def run_threads(query_array) num_threads = @find_threads thr_queries = [] (0...num_threads).each { |ind| thr_queries[ind] = [] } query_array.each { |query| thr_queries[query[:page_num] % num_threads].push(query) } thr_array = [] thr_queries.each { |thr_query_array| thr_array.push(run_single_thread(thr_query_array)) } all_results = [] thr_array.each do |thr| thr.value.each { |result_val| all_results.push(result_val) } end all_results.sort! { |resa, resb| resa[:page_num] <=> resb[:page_num] } end
set_api_key(auth_info)
click to toggle source
# File lib/rally_api/rally_json_connection.rb, line 191 def set_api_key(auth_info) @api_key = auth_info[:api_key] end
set_client_user(base_url, user, password)
click to toggle source
# File lib/rally_api/rally_json_connection.rb, line 186 def set_client_user(base_url, user, password) @rally_http_client.set_auth(base_url, user, password) @rally_http_client.www_auth.basic_auth.challenge(base_url) #force httpclient to put basic on first req to rally end
setup_request_headers(http_method)
click to toggle source
# File lib/rally_api/rally_json_connection.rb, line 176 def setup_request_headers(http_method) req_headers = @rally_headers.headers req_headers[:ZSESSIONID] = @api_key unless @api_key.nil? if (http_method == :post) || (http_method == :put) req_headers["Content-Type"] = "application/json" req_headers["Accept"] = "application/json" end req_headers end