post_agent.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. module Agents
  2. class PostAgent < Agent
  3. include WebRequestConcern
  4. cannot_create_events!
  5. default_schedule "never"
  6. description <<-MD
  7. A Post Agent receives events from other agents (or runs periodically), merges those events with the [Liquid-interpolated](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) contents of `payload`, and sends the results as POST (or GET) requests to a specified url. To skip merging in the incoming event, but still send the interpolated payload, set `no_merge` to `true`.
  8. The `post_url` field must specify where you would like to send requests. Please include the URI scheme (`http` or `https`).
  9. The `method` used can be any of `get`, `post`, `put`, `patch`, and `delete`.
  10. By default, non-GETs will be sent with form encoding (`application/x-www-form-urlencoded`). Change `content_type` to `json` to send JSON instead. Change `content_type` to `xml` to send XML, where the name of the root element may be specified using `xml_root`, defaulting to `post`.
  11. Other Options:
  12. * `headers` - When present, it should be a hash of headers to send with the request.
  13. * `basic_auth` - Specify HTTP basic auth parameters: `"username:password"`, or `["username", "password"]`.
  14. * `disable_ssl_verification` - Set to `true` to disable ssl verification.
  15. * `user_agent` - A custom User-Agent name (default: "Faraday v#{Faraday::VERSION}").
  16. MD
  17. event_description "Does not produce events."
  18. def default_options
  19. {
  20. 'post_url' => "http://www.example.com",
  21. 'expected_receive_period_in_days' => '1',
  22. 'content_type' => 'form',
  23. 'method' => 'post',
  24. 'payload' => {
  25. 'key' => 'value',
  26. 'something' => 'the event contained {{ somekey }}'
  27. },
  28. 'headers' => {}
  29. }
  30. end
  31. def working?
  32. last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  33. end
  34. def method
  35. (interpolated['method'].presence || 'post').to_s.downcase
  36. end
  37. def validate_options
  38. unless options['post_url'].present? && options['expected_receive_period_in_days'].present?
  39. errors.add(:base, "post_url and expected_receive_period_in_days are required fields")
  40. end
  41. if options['payload'].present? && !options['payload'].is_a?(Hash)
  42. errors.add(:base, "if provided, payload must be a hash")
  43. end
  44. unless %w[post get put delete patch].include?(method)
  45. errors.add(:base, "method must be 'post', 'get', 'put', 'delete', or 'patch'")
  46. end
  47. if options['no_merge'].present? && !%[true false].include?(options['no_merge'].to_s)
  48. errors.add(:base, "if provided, no_merge must be 'true' or 'false'")
  49. end
  50. unless headers.is_a?(Hash)
  51. errors.add(:base, "if provided, headers must be a hash")
  52. end
  53. validate_web_request_options!
  54. end
  55. def receive(incoming_events)
  56. incoming_events.each do |event|
  57. outgoing = interpolated(event)['payload'].presence || {}
  58. if boolify(interpolated['no_merge'])
  59. handle outgoing, event.payload
  60. else
  61. handle outgoing.merge(event.payload), event.payload
  62. end
  63. end
  64. end
  65. def check
  66. handle interpolated['payload'].presence || {}
  67. end
  68. private
  69. def handle(data, payload = {})
  70. url = interpolated(payload)[:post_url]
  71. headers = headers()
  72. case method
  73. when 'get', 'delete'
  74. params, body = data, nil
  75. when 'post', 'put', 'patch'
  76. params = nil
  77. case interpolated(payload)['content_type']
  78. when 'json'
  79. headers['Content-Type'] = 'application/json; charset=utf-8'
  80. body = data.to_json
  81. when 'xml'
  82. headers['Content-Type'] = 'text/xml; charset=utf-8'
  83. body = data.to_xml(root: (interpolated(payload)[:xml_root] || 'post'))
  84. else
  85. body = data
  86. end
  87. else
  88. error "Invalid method '#{method}'"
  89. end
  90. faraday.run_request(method.to_sym, url, body, headers) { |request|
  91. request.params.update(params) if params
  92. }
  93. end
  94. end
  95. end