Class: RallyAPI::RallyJsonConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/rally_api/rally_json_connection.rb

Constant Summary collapse

DEFAULT_PAGE_SIZE =
200

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, low_debug, proxy_info) ⇒ RallyJsonConnection

Returns a new instance of RallyJsonConnection.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#find_threadsObject (readonly)

Returns the value of attribute find_threads.



18
19
20
# File 'lib/rally_api/rally_json_connection.rb', line 18

def find_threads
  @find_threads
end

#loggerObject

Returns the value of attribute logger.



18
19
20
# File 'lib/rally_api/rally_json_connection.rb', line 18

def logger
  @logger
end

#low_debugObject

Returns the value of attribute low_debug.



17
18
19
# File 'lib/rally_api/rally_json_connection.rb', line 17

def low_debug
  @low_debug
end

#rally_headersObject

Returns the value of attribute rally_headers.



17
18
19
# File 'lib/rally_api/rally_json_connection.rb', line 17

def rally_headers
  @rally_headers
end

#rally_http_clientObject (readonly)

Returns the value of attribute rally_http_client.



18
19
20
# File 'lib/rally_api/rally_json_connection.rb', line 18

def rally_http_client
  @rally_http_client
end

Instance Method Details

#add_security_key(keyval) ⇒ Object



69
70
71
# File 'lib/rally_api/rally_json_connection.rb', line 69

def add_security_key(keyval)
  @security_token = keyval
end

#check_for_errors(result) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# 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) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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

#reset_cookiesObject

may be needed for session issues



79
80
81
# File 'lib/rally_api/rally_json_connection.rb', line 79

def reset_cookies
  @rally_http_client.cookie_manager.cookies = []
end

#send_request(url, args, url_params = {}) ⇒ Object

args should have :method

Raises:

  • (StandardError)


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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) ⇒ Object



45
46
47
48
49
50
51
# 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) ⇒ Object

you can have any number you want as long as it is between 1 and 4



84
85
86
87
88
89
# 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) ⇒ Object



53
54
55
# 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) ⇒ Object

[]todo - handle token expiration more gracefully - eg handle renewing



58
59
60
61
62
63
64
65
66
67
# 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