huginn_http_request_agent 1.0.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: aa4b89cbaf5fef82663f2845a176ff324843eb74
4
- data.tar.gz: 45bf7be76d8549e519571e0a4da22101e2167868
2
+ SHA256:
3
+ metadata.gz: efc2ffa3f090b8c268492b91a79a0914ee18bbbdd431b714160d65856f34be24
4
+ data.tar.gz: 276b80b72c60689cedb18d2f1e6e9fed117bbfe97229d06aa138e04a3cca8c0e
5
5
  SHA512:
6
- metadata.gz: 4c8cbcb0ef880d72ac64ac14ce67a1b2f611976b9ee9eef7a09f0ad94a13667806cf4cd6a21692d0b551d1286ac777018d155300dc01e60f53400c5e0daec184
7
- data.tar.gz: bec6693f64867873839b1b22be72faf25dfe754f810b79958fd294bb494dfadd0bf008666e67387353eb22a31f24c17ae620e9a86ddb880903bf2e5e1f8158f1
6
+ metadata.gz: 64f2207730c600ce39d38267057377af946164c50dbf15fe6b0a9108384fb16685511cef5105bffbee462ed59c493f60b78dd5bd2969061318be0cfd0eac169e
7
+ data.tar.gz: c2204699225a8867b3a4b5955724935e49964b6d1e65158413c8af42035a9dd4506c0e090977a96c216784b2fd93855e94b186967344aa4e2860d2e23e251ba5
@@ -34,6 +34,7 @@ module Agents
34
34
  - `no_merge` - Setting this value to `true` will result in the incoming event, but still send the interpolated payload
35
35
  - `output_mode` - Setting this value to `merge` will result in the emitted Event being merged into the original contents of the received Event. Setting it to `clean` will result in no merge.
36
36
  - `emit_events` - Setting this to `true` will result in the server response being emitted as an Event which can be subsequently consumed by another agent (ex. to a WebsiteAgent for parsing of the response body)
37
+ - `log_requests` - Setting this to `true` will log the contents of the interpolated request object sent by this event.
37
38
 
38
39
  ### Content Type's:
39
40
 
@@ -41,11 +42,18 @@ module Agents
41
42
 
42
43
  - `json` to send JSON instead
43
44
  - `xml` to send XML, where the name of the root element may be specified using `xml_root`
45
+ - All other `content_type`'s will be serialized as a string
44
46
 
45
47
  By default, non-GETs will be sent with form encoding (`application/json`).
46
48
 
47
- When `content_type` contains a [MIME](https://en.wikipedia.org/wiki/Media_type) type, and `payload` is a string, its interpolated value will be sent as a string in the HTTP request's body and the request's `Content-Type` HTTP header will be set to `content_type`.
49
+ The `content_type` field will default the `Content-Type` header if it is not explicitly set in the following manner:
48
50
 
51
+ - if `Content-Type` is set use value
52
+ - else if `content_type==json` then `Content-Type=application/json`
53
+ - else if `content_type==xml` then `Content-Type=application/xml`
54
+ - else `Content-Type=content_type`
55
+
56
+ This allows you fine grained control over the mime type being used.
49
57
 
50
58
  ### Response
51
59
 
@@ -93,7 +101,8 @@ module Agents
93
101
  'headers' => {},
94
102
  'emit_events' => 'true',
95
103
  'no_merge' => 'false',
96
- 'output_mode' => 'clean'
104
+ 'output_mode' => 'clean',
105
+ 'log_requests' => 'false'
97
106
  }
98
107
  end
99
108
 
@@ -138,6 +147,10 @@ module Agents
138
147
  errors.add(:base, "if provided, emit_events must be true or false")
139
148
  end
140
149
 
150
+ if options.has_key?('log_requests') && boolify(options['log_requests']).nil?
151
+ errors.add(:base, "If provided, log_requests must be true or false")
152
+ end
153
+
141
154
  validate_event_headers_options!
142
155
 
143
156
  unless %w[post get put delete patch].include?(method)
@@ -163,6 +176,7 @@ module Agents
163
176
  incoming_events.each do |event|
164
177
  interpolate_with(event) do
165
178
  outgoing = interpolated['payload'].presence || {}
179
+
166
180
  if boolify(interpolated['no_merge'])
167
181
  handle outgoing, event, headers(interpolated[:headers])
168
182
  else
@@ -187,7 +201,7 @@ module Agents
187
201
 
188
202
  case content_type
189
203
  when 'json'
190
- headers['Content-Type'] = 'application/json; charset=utf-8'
204
+ headers['Content-Type'] ||= 'application/json; charset=utf-8'
191
205
  url = faraday.build_url(url, data.compact)
192
206
  else
193
207
  params = data
@@ -209,13 +223,13 @@ module Agents
209
223
 
210
224
  case content_type
211
225
  when 'json'
212
- headers['Content-Type'] = 'application/json; charset=utf-8'
226
+ headers['Content-Type'] ||= 'application/json; charset=utf-8'
213
227
  body = data.to_json
214
228
  when 'xml'
215
- headers['Content-Type'] = 'text/xml; charset=utf-8'
229
+ headers['Content-Type'] ||= 'text/xml; charset=utf-8'
216
230
  body = data.to_xml(root: (interpolated(event.payload)[:xml_root] || 'post'))
217
231
  when MIME_RE
218
- headers['Content-Type'] = content_type
232
+ headers['Content-Type'] ||= content_type
219
233
  body = data.to_s
220
234
  else
221
235
  body = data
@@ -224,34 +238,72 @@ module Agents
224
238
  error "Invalid method '#{method}'"
225
239
  end
226
240
 
227
- response = faraday.run_request(method.to_sym, url, body, headers) { |request|
241
+ if boolify(interpolated['log_requests'])
242
+ log({ method: method, url: url, body: body, headers: headers })
243
+ end
228
244
 
229
- # open/read timeout in seconds
230
- if interpolated['timeout'].to_i
231
- request.options.timeout = interpolated['timeout'].to_i
232
- end
245
+ output_event = interpolated['output_mode'].to_s == 'merge' ? event.payload.dup : {}
233
246
 
234
- # connection open timeout in seconds
235
- if interpolated['open_timeout'].to_i
236
- request.options.open_timeout = interpolated['open_timeout'].to_i
237
- end
247
+ begin
238
248
 
239
- request.params.update(params) if params
240
- }
249
+ response = faraday.run_request(method.to_sym, url, body, headers) { |request|
241
250
 
242
- if boolify(interpolated['emit_events'])
243
- new_event = interpolated['output_mode'].to_s == 'merge' ? event.payload.dup : {}
244
- create_event payload: new_event.merge(
245
- body: response.body,
246
- status: response.status
247
- ).merge(
248
- event_headers_payload(response.headers)
249
- )
251
+ # open/read timeout in seconds
252
+ if interpolated['timeout'].to_i
253
+ request.options.timeout = interpolated['timeout'].to_i
254
+ end
255
+
256
+ # connection open timeout in seconds
257
+ if interpolated['open_timeout'].to_i
258
+ request.options.open_timeout = interpolated['open_timeout'].to_i
259
+ end
260
+
261
+ request.params.update(params) if params
262
+ }
263
+
264
+ if boolify(interpolated['emit_events'])
265
+ create_event payload: output_event.merge(
266
+ body: response.body,
267
+ status: response.status
268
+ ).merge(
269
+ event_headers_payload(response.headers)
270
+ )
271
+ end
272
+ rescue => e
273
+ handle_req_error(e, output_event, url)
250
274
  end
251
275
  end
252
276
 
253
277
  def event_headers_key
254
278
  super || 'headers'
255
279
  end
256
- end
280
+
281
+ def handle_req_error( error, output_payload, endpoint )
282
+
283
+ error_status = defined?(error.response_status) && !error.response_status.nil? ? error.response_status : 500
284
+
285
+ # NOTE: `options['payload']`` below is intentionally _NOT_ interpolated.
286
+ # The primary reason for this is that it may contain sensitive values
287
+ # By passing the raw option, we will see liquid placeholders instead.
288
+ # This wiill assist with debugging while also not exposing secrets.
289
+
290
+ log({
291
+ error_message: error.message,
292
+ status_code: error_status,
293
+ endpoint: endpoint,
294
+ payload_options: options['payload'],
295
+ })
296
+
297
+ if boolify(interpolated['emit_events'])
298
+ create_event payload: output_payload.merge(
299
+ status: error_status,
300
+ error_message: error.message,
301
+ endpoint: endpoint,
302
+ payload_options: options['payload'],
303
+ )
304
+ end
305
+
306
+ end
307
+
308
+ end # <-- End of class
257
309
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: huginn_http_request_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Spizziri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-02 00:00:00.000000000 Z
11
+ date: 2022-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,7 +52,8 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: Write a longer description or delete this line.
55
+ description: The Http Request Agent is intended to be an abstract agent that allows
56
+ for the interaction of any http service.
56
57
  email:
57
58
  - jspizziri@weare5stones.com
58
59
  executables: []
@@ -63,7 +64,7 @@ files:
63
64
  - lib/huginn_http_request_agent.rb
64
65
  - lib/huginn_http_request_agent/http_request_agent.rb
65
66
  - spec/http_request_agent_spec.rb
66
- homepage: https://github.com/[my-github-username]/huginn_http_request_agent
67
+ homepage: https://github.com/5-stones/huginn_http_request_agent
67
68
  licenses:
68
69
  - MIT
69
70
  metadata: {}
@@ -82,10 +83,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
83
  - !ruby/object:Gem::Version
83
84
  version: '0'
84
85
  requirements: []
85
- rubyforge_project:
86
- rubygems_version: 2.6.10
86
+ rubygems_version: 3.0.3
87
87
  signing_key:
88
88
  specification_version: 4
89
- summary: Write a short summary, because Rubygems requires one.
89
+ summary: The Http Request Agent is intended to be an abstract agent that allows for
90
+ the interaction of any http service.
90
91
  test_files:
91
92
  - spec/http_request_agent_spec.rb