Class: Request

Inherits:
Object
  • Object
show all
Defined in:
lib/trail_marker/request.rb

Overview

Calls testrail API History: 2/26/18 Removed retry for failed calls

2/27/18 Combined post/get methods DRY
        Change retry to skip only if TC not found.

Defined Under Namespace

Classes: APIError

Constant Summary collapse

RETRIES =
1

Instance Method Summary collapse

Constructor Details

#initialize(client, debug_mode = false) ⇒ Request

Returns a new instance of Request.



11
12
13
14
15
# File 'lib/trail_marker/request.rb', line 11

def initialize(client, debug_mode = false)
  @client = client
  @debug_mode = get_debug_value(debug_mode)
  @debug_mode = true
end

Instance Method Details

#call_api(req, rtype, data = nil) ⇒ Object

Executes a TestRail API call but catches any exception raised to prevent script from crashing. Used by exec_get to do retries.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/trail_marker/request.rb', line 58

def call_api(req, rtype, data=nil)
  msg("#{rtype} REQ: #{req}")
  res_hash = {:status => true, :response => ""}
  begin
    if rtype == 'POST'
      get_response = @client.send_post(req, data)
    else
      get_response = @client.send_get(req)
    end
  rescue Exception => e
    puts "Raised Exception: #{e.message}."
    res_hash[:status] = false
    res_hash[:response] = e.message
  else
    res_hash[:status] = true
    res_hash[:response] = get_response
  end
  msg("RESPONSE: #{res_hash}")
  return res_hash
end

#exec_api_call(rest_type, req, data = nil, exit_on_fail = false) ⇒ Object

Retries an API call max number of times if an exception is raised. Sleeps 4, 8, 12 .… seconds for each retry.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/trail_marker/request.rb', line 21

def exec_api_call(rest_type, req, data = nil, exit_on_fail = false)
  max_retry = RETRIES
  get_hash = nil
  attempts = 0
  is_good = false
  while !is_good && attempts < max_retry
    attempts += 1
    get_hash = call_api(req, rest_type, data)
    is_good = get_hash[:status]
    unless is_good
      exit_script() if exit_on_fail
      puts "Got Error making API call - #{req} "
      if attempts < max_retry
        puts "Retrying #{attempts}"
        sleep(4 * attempts)
      end
    end
  end
  get_hash
end

#exec_get(req, exit_on_fail = false) ⇒ Object



48
49
50
51
52
# File 'lib/trail_marker/request.rb', line 48

def exec_get(req, exit_on_fail = false)
  response_hash = exec_api_call('GET', req, nil, exit_on_fail)
  check_authentication_fail(response_hash) unless response_hash[:status]
  response_hash[:response]
end

#exec_post(req, data, exit_on_fail = false) ⇒ Object

TODO: DRY with exec_get



43
44
45
46
# File 'lib/trail_marker/request.rb', line 43

def exec_post(req, data, exit_on_fail = false)
  response_hash = exec_api_call('POST', req, data, exit_on_fail)
  response_hash[:response]
end