Class: Resat::ApiRequest

Inherits:
Object
  • Object
show all
Includes:
Kwalify::Util::HashLike
Defined in:
lib/api_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#failuresObject (readonly)

Returns the value of attribute failures.



14
15
16
# File 'lib/api_request.rb', line 14

def failures
  @failures
end

#requestObject (readonly)

Returns the value of attribute request.



14
15
16
# File 'lib/api_request.rb', line 14

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



14
15
16
# File 'lib/api_request.rb', line 14

def response
  @response
end

#send_countObject (readonly)

Returns the value of attribute send_count.



14
15
16
# File 'lib/api_request.rb', line 14

def send_count
  @send_count
end

Instance Method Details

#get_response_field(field, target) ⇒ Object

Get value of response header or body field



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/api_request.rb', line 127

def get_response_field(field, target)
  return unless @response
  return @response[field] if target == 'header'
  return @response.body if field.nil? || field.empty?
  json = JSON.load(@response.body) rescue nil
  res = nil
  if json
    res = json_field(json, field)
  else
    doc = REXML::Document.new(@response.body)
    elem = doc.elements[field]
    res = elem.get_text.to_s if elem
  end
  res
end

#has_response_field?(field, target) ⇒ Boolean

Does response include given header or body field?

Returns:

  • (Boolean)


122
123
124
# File 'lib/api_request.rb', line 122

def has_response_field?(field, target)
  !!get_response_field(field, target)
end

#prepare(variables, config) ⇒ Object

Prepare request so ‘send’ can be called



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/api_request.rb', line 17

def prepare(variables, config)
 @format ||= 'xml'
 @failures = []
 @send_count = 0
 @config_delay = config.delay
 @config_use_ssl = config.use_ssl

 # 1. Normalize call fields
 @headers ||= []
 @params ||= []
 # Clone config values so we don't mess with them when expanding variables
 config.headers.each do |h|
   value = @headers.detect { |header| header['name'] == h['name'] }
   @headers << { 'name' => h['name'].dup, 'value' => h['value'].dup } unless value
 end if config.headers
 config.params.each do |p|
   value = @params.detect { |header| header['name'] == h['name'] }
   @params << { 'name' => h['name'].dup, 'value' => h['value'].dup } unless value
 end if config.params && request_class.REQUEST_HAS_BODY
 variables.substitute!(@params)
 variables.substitute!(@headers)

 # 2. Build URI
 variables.substitute!(@id) if @id
 uri_class = (@use_ssl || @use_ssl.nil? && config.use_ssl) ? URI::HTTPS : URI::HTTP
 port = @port || config.port || uri_class::DEFAULT_PORT
 variables.substitute!(port)
 host = @host || config.host
 variables.substitute!(host)
 @uri = uri_class.build( :host => host, :port => port )
 base_url = "/#{@base_url || config.base_url}/".squeeze('/')
 variables.substitute!(base_url)
 path = @path
 unless path
   path = "#{base_url}#{@resource}"
   path = "#{path}/#{@id}" if @id
   path = "#{path}.#{@format}" if @format && !@format.empty? && !@custom
   path = "#{path}#{@custom.separator}#{@custom.name}" if @custom
 end
 variables.substitute!(path)
 @uri.merge!(path)

 # 3. Build request
 case @operation
   when 'index', 'show' then request_class = Net::HTTP::Get
   when 'create'        then request_class = Net::HTTP::Post
   when 'update'        then request_class = Net::HTTP::Put
   when 'destroy'       then request_class = Net::HTTP::Delete
 else
   if type = (@type || @custom && @custom.type)
     case type
       when 'get'    then request_class = Net::HTTP::Get
       when 'post'   then request_class = Net::HTTP::Post
       when 'put'    then request_class = Net::HTTP::Put
       when 'delete' then request_class = Net::HTTP::Delete
     end
   else
     @failures << "Missing request type for request on '#{@resource}'."
     return
   end
 end
 @request = request_class.new(@uri.to_s)
 username = @username || config.username
 variables.substitute!(username) if username
 password = @password || config.password
 variables.substitute!(password) if password
 if username && password
   @request.basic_auth(username, password)
 end
 form_data = Hash.new
 @headers.each { |header| @request[header['name']] = header['value'] }
 @params.each do |param|
   form_data[param['name']] ||= []
   form_data[param['name']] << param['value']
 end
 set_form_data(@request, form_data) unless form_data.empty?
 Log.request(@request)

 # 4. Send request and check response code
 @oks = @valid_codes.map { |r| r.to_s } if @valid_codes
 @oks ||= %w{200 201 202 203 204 205 206}
end

#sendObject

Actually send the request



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/api_request.rb', line 101

def send
  sleep(delay_seconds) # Delay request if needed
  http = Net::HTTP.new(@uri.host, @uri.port)
  http.use_ssl = @config_use_ssl
  begin
    res = http.start { |http| @response = http.request(@request) }
  rescue Exception => e
    @failures << "Exception raised while making request: #{e.message}"
  end
  if @failures.size == 0
    @send_count += 1
    if @oks.include?(res.code)
      Log.response(@response)
    else
      Log.response(@response, false)
      @failures << "Request returned #{res.code}"
    end
  end
end