Module: ParallelReportPortal::HTTP

Included in:
ParallelReportPortal
Defined in:
lib/parallel_report_portal/http.rb

Overview

A collection of methods for communicating with the ReportPortal REST interface.

Constant Summary collapse

@@logger =

Creating class level logger and setting log level

Logger.new(STDOUT)

Instance Method Summary collapse

Instance Method Details

#authorization_headerString

Helper method for constructing the Bearer header



24
25
26
# File 'lib/parallel_report_portal/http.rb', line 24

def authorization_header
  "Bearer #{ParallelReportPortal.configuration.api_key}"
end

#http_connectionFaraday::Connection

Get a preconstructed Faraday HTTP connection which has the endpont and headers ready populated. This object is memoized.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/parallel_report_portal/http.rb', line 33

def http_connection
  @http_connection ||= Faraday.new(
    url: url,
    headers: {
      'Content-Type' => 'application/json',
      'Authorization' => authorization_header
    }
  ) do |f|
    f.adapter :net_http_persistent, pool_size: 5 do |http|
      http.idle_timeout = ParallelReportPortal.configuration.fetch(:idle_timeout, 100)
      http.open_timeout = ParallelReportPortal.configuration.fetch(:open_timeout, 60)
      http.read_timeout = ParallelReportPortal.configuration.fetch(:read_timeout, 60)
    end
  end
end

#http_multipart_connectionFaraday::Connection

Get a preconstructed Faraday HTTP multipart connection which has the endpont and headers ready populated. This object is memoized.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/parallel_report_portal/http.rb', line 54

def http_multipart_connection
  @http_multipart_connection ||= Faraday.new(
    url: url,
    headers: {
      'Authorization' => authorization_header
    }
  ) do |conn|
    conn.request :multipart
    conn.request :url_encoded
    conn.adapter :net_http_persistent, pool_size: 5 do |http|
      # yields Net::HTTP::Persistent
      http.idle_timeout = ParallelReportPortal.configuration.fetch(:idle_timeout, 100)
      http.open_timeout = ParallelReportPortal.configuration.fetch(:open_timeout, 60)
      http.read_timeout = ParallelReportPortal.configuration.fetch(:read_timeout, 60)
    end
  end
end

#req_feature_finished(feature_id, time) ⇒ Object

Send a request to Report Portal that a feature has completed.



147
148
149
150
151
# File 'lib/parallel_report_portal/http.rb', line 147

def req_feature_finished(feature_id, time)
  ParallelReportPortal.http_connection.put("item/#{feature_id}") do |req|
    req.body = { end_time: time }.to_json
  end
end

#req_feature_started(launch_id, parent_id, feature, time) ⇒ String

Send a request to ReportPortal to start a feature. It will bubble up any Faraday connection exceptions.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/parallel_report_portal/http.rb', line 105

def req_feature_started(launch_id, parent_id, feature, time)
    description = if feature.description
                    feature.description.split("\n").map(&:strip).join(' ')
                  else
                    feature.file
                  end

    req_hierarchy(launch_id,
                  "#{feature.keyword}: #{feature.name}",
                  parent_id,
                  'TEST',
                  feature.tags.map(&:name),
                  description,
                  time )
end

#req_hierarchy(launch_id, name, parent, type, tags, description, time) ⇒ String

Sends a request to Report Portal to add an item into its hierarchy.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/parallel_report_portal/http.rb', line 124

def req_hierarchy(launch_id, name, parent, type, tags, description, time )
  resource = 'item'
  resource += "/#{parent}" if parent
  resp = ParallelReportPortal.http_connection.post(resource) do |req|
    req.body = {
      start_time: time,
      name: name,
      type: type,
      launch_id: launch_id,
      tags: tags,
      description: description,
      attributes: tags
    }.to_json
  end

  if resp.success?
    JSON.parse(resp.body)['id']
  else
    @@logger.warn("Starting a heirarchy failed with response code #{resp.status} -- message #{resp.body}")
  end
end

#req_launch_finished(launch_id, time) ⇒ Object

Send a request to Report Portal to finish a launch. It will bubble up any Faraday connection exceptions.



95
96
97
98
99
# File 'lib/parallel_report_portal/http.rb', line 95

def req_launch_finished(launch_id, time)
  ParallelReportPortal.http_connection.put("launch/#{launch_id}/finish") do |req|
    req.body = { end_time: time }.to_json
  end
end

#req_launch_started(time) ⇒ Object

Send a request to ReportPortal to start a launch. It will bubble up any Faraday connection exceptions.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/parallel_report_portal/http.rb', line 74

def req_launch_started(time)
  resp = http_connection.post('launch') do |req|
          req.body = {
            name: ParallelReportPortal.configuration.launch,
            start_time: time,
            tags: ParallelReportPortal.configuration.tags,
            description: ParallelReportPortal.configuration.description,
            mode: (ParallelReportPortal.configuration.debug ? 'DEBUG' : 'DEFAULT' ),
            attributes: ParallelReportPortal.configuration.attributes
          }.to_json
  end

  if resp.success?
    JSON.parse(resp.body)['id']
  else
    @@logger.error("Launch failed with response code #{resp.status} -- message #{resp.body}")
  end
end

#req_log(test_case_id, detail, level, time) ⇒ Object

Request that Report Portal records a log record



193
194
195
196
197
198
199
200
201
202
# File 'lib/parallel_report_portal/http.rb', line 193

def req_log(test_case_id, detail, level, time)
  resp = ParallelReportPortal.http_connection.post('log') do |req|
    req.body = {
      item_id: test_case_id,
      message: detail,
      level: level,
      time: time,
    }.to_json
  end
end

#req_test_case_finished(test_case_id, status, time) ⇒ Object

Request that the test case be finished



182
183
184
185
186
187
188
189
# File 'lib/parallel_report_portal/http.rb', line 182

def req_test_case_finished(test_case_id, status, time)
  resp = ParallelReportPortal.http_connection.put("item/#{test_case_id}") do |req|
    req.body = {
      end_time: time,
      status: status
    }.to_json
  end
end

#req_test_case_started(launch_id, feature_id, test_case, time) ⇒ String

Send a request to ReportPortal to start a test case.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/parallel_report_portal/http.rb', line 156

def req_test_case_started(launch_id, feature_id, test_case, time)
  resp = ParallelReportPortal.http_connection.post("item/#{feature_id}") do |req|

    keyword = if test_case.respond_to?(:feature)
                test_case.feature.keyword
              else
                test_case.keyword
              end
    req.body = {
      start_time: time,
      tags: test_case.tags.map(&:name),
      name: "#{keyword}: #{test_case.name}",
      type: 'STEP',
      launch_id: launch_id,
      description: test_case.description,
      attributes: test_case.tags.map(&:name)
    }.to_json
  end
  if resp.success?
    @test_case_id = JSON.parse(resp.body)['id'] if resp.success?
  else
    @@logger.warn("Starting a test case failed with response code #{resp.status} -- message #{resp.body}")
  end
end

#send_file(status, path, label = nil, time = ParallelReportPortal.clock, mime_type = 'image/png', scenario_id = nil) ⇒ Object

Request that Report Portal attach a file to the test case.



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/parallel_report_portal/http.rb', line 212

def send_file(
  status,
  path,
  label = nil,
  time = ParallelReportPortal.clock,
  mime_type = 'image/png',
  scenario_id = nil
)
  File.open(File.realpath(path), 'rb') do |file|
    label ||= File.basename(file)

    # where did @test_case_id come from? ok, I know where it came from but this
    # really should be factored out of here and state handled better
    json = { level: status, message: label, item_id: scenario_id || @test_case_id, time: time, file: { name: File.basename(file) } }

    json_file = Tempfile.new
    json_file << [json].to_json
    json_file.rewind

    resp = http_multipart_connection.post("log") do |req|
      req.body = {
        json_request_part: Faraday::UploadIO.new(json_file, 'application/json'),
        file: Faraday::UploadIO.new(file, mime_type)
      }
    end
  end
end

#urlString

Construct the Report Portal project URL (as a string) based on the config settings.



17
18
19
# File 'lib/parallel_report_portal/http.rb', line 17

def url
  "#{ParallelReportPortal.configuration.endpoint}/#{ParallelReportPortal.configuration.project}"
end