post_agent.rb 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. module Agents
  2. class PostAgent < Agent
  3. include EventHeadersConcern
  4. include WebRequestConcern
  5. include FileHandling
  6. consumes_file_pointer!
  7. MIME_RE = /\A\w+\/.+\z/
  8. can_dry_run!
  9. no_bulk_receive!
  10. default_schedule "never"
  11. description do
  12. <<~MD
  13. A Post Agent receives events from other agents (or runs periodically), merges those events with the [Liquid-interpolated](https://github.com/huginn/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`.
  14. The `post_url` field must specify where you would like to send requests. Please include the URI scheme (`http` or `https`).
  15. The `method` used can be any of `get`, `post`, `put`, `patch`, and `delete`.
  16. By default, non-GETs will be sent with form encoding (`application/x-www-form-urlencoded`).
  17. Change `content_type` to `json` to send JSON instead.
  18. Change `content_type` to `xml` to send XML, where the name of the root element may be specified using `xml_root`, defaulting to `post`.
  19. 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`. When `payload` is a string `no_merge` has to be set to `true`.
  20. If `emit_events` is set to `true`, the server response will be emitted as an Event. The "body" value of the Event is the response body. If the `parse_body` option is set to `true` and the content type of the response is JSON, it is parsed to a JSON object. Otherwise it is raw text. A raw HTML/XML text can be fed to a WebsiteAgent for parsing (using its `data_from_event` and `type` options).
  21. The Event will also have a "headers" hash and a "status" integer value.
  22. If `output_mode` is set to `merge`, the emitted Event will be merged into the original contents of the received Event.
  23. Set `event_headers` to a list of header names, either in an array of string or in a comma-separated string, to include only some of the header values.
  24. Set `event_headers_style` to one of the following values to normalize the keys of "headers" for downstream agents' convenience:
  25. * `capitalized` (default) - Header names are capitalized; e.g. "Content-Type"
  26. * `downcased` - Header names are downcased; e.g. "content-type"
  27. * `snakecased` - Header names are snakecased; e.g. "content_type"
  28. * `raw` - Backward compatibility option to leave them unmodified from what the underlying HTTP library returns.
  29. Other Options:
  30. * `headers` - When present, it should be a hash of headers to send with the request.
  31. * `basic_auth` - Specify HTTP basic auth parameters: `"username:password"`, or `["username", "password"]`.
  32. * `disable_ssl_verification` - Set to `true` to disable ssl verification.
  33. * `user_agent` - A custom User-Agent name (default: "Faraday v#{Faraday::VERSION}").
  34. #{receiving_file_handling_agent_description}
  35. When receiving a `file_pointer` the request will be sent with multipart encoding (`multipart/form-data`) and `content_type` is ignored. `upload_key` can be used to specify the parameter in which the file will be sent, it defaults to `file`.
  36. MD
  37. end
  38. event_description <<~MD
  39. Events look like this:
  40. {
  41. "status": 200,
  42. "headers": {
  43. "Content-Type": "text/html",
  44. ...
  45. },
  46. "body": "<html>Some data...</html>"
  47. }
  48. Original event contents will be merged when `output_mode` is set to `merge`.
  49. MD
  50. def default_options
  51. {
  52. 'post_url' => "http://www.example.com",
  53. 'expected_receive_period_in_days' => '1',
  54. 'content_type' => 'form',
  55. 'method' => 'post',
  56. 'payload' => {
  57. 'key' => 'value',
  58. 'something' => 'the event contained {{ somekey }}'
  59. },
  60. 'headers' => {},
  61. 'emit_events' => 'false',
  62. 'parse_body' => 'true',
  63. 'no_merge' => 'true',
  64. 'output_mode' => 'clean'
  65. }
  66. end
  67. def working?
  68. return false if recent_error_logs?
  69. if interpolated['expected_receive_period_in_days'].present?
  70. return false unless last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago
  71. end
  72. true
  73. end
  74. def method
  75. (interpolated['method'].presence || 'post').to_s.downcase
  76. end
  77. def validate_options
  78. unless options['post_url'].present?
  79. errors.add(:base, "post_url is a required field")
  80. end
  81. if options['payload'].present? && %w[get
  82. delete].include?(method) && !(options['payload'].is_a?(Hash) || options['payload'].is_a?(Array))
  83. errors.add(:base, "if provided, payload must be a hash or an array")
  84. end
  85. if options['payload'].present? && %w[post put patch].include?(method)
  86. if !(options['payload'].is_a?(Hash) || options['payload'].is_a?(Array)) && options['content_type'] !~ MIME_RE
  87. errors.add(:base, "if provided, payload must be a hash or an array")
  88. end
  89. end
  90. if options['content_type'] =~ MIME_RE && options['payload'].is_a?(String) && boolify(options['no_merge']) != true
  91. errors.add(:base, "when the payload is a string, `no_merge` has to be set to `true`")
  92. end
  93. if options['content_type'] == 'form' && options['payload'].present? && options['payload'].is_a?(Array)
  94. errors.add(:base, "when content_type is a form, if provided, payload must be a hash")
  95. end
  96. if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
  97. errors.add(:base, "if provided, emit_events must be true or false")
  98. end
  99. validate_event_headers_options!
  100. unless %w[post get put delete patch].include?(method)
  101. errors.add(:base, "method must be 'post', 'get', 'put', 'delete', or 'patch'")
  102. end
  103. if options['no_merge'].present? && !%(true false).include?(options['no_merge'].to_s)
  104. errors.add(:base, "if provided, no_merge must be 'true' or 'false'")
  105. end
  106. if options['output_mode'].present? && !options['output_mode'].to_s.include?('{') && !%(clean merge).include?(options['output_mode'].to_s)
  107. errors.add(:base, "if provided, output_mode must be 'clean' or 'merge'")
  108. end
  109. if options['parse_body'].present? && !/\A(?:true|false)\z|\{/.match?(options['parse_body'].to_s)
  110. errors.add(:base, "if provided, parse_body must be 'true' or 'false'")
  111. end
  112. unless headers.is_a?(Hash)
  113. errors.add(:base, "if provided, headers must be a hash")
  114. end
  115. validate_web_request_options!
  116. end
  117. def parse_body?
  118. boolify(interpolated['parse_body'])
  119. end
  120. def receive(incoming_events)
  121. incoming_events.each do |event|
  122. interpolate_with(event) do
  123. outgoing = interpolated['payload'].presence || {}
  124. if boolify(interpolated['no_merge'])
  125. handle outgoing, event, headers(interpolated[:headers])
  126. else
  127. handle outgoing.merge(event.payload), event, headers(interpolated[:headers])
  128. end
  129. end
  130. end
  131. end
  132. def check
  133. handle interpolated['payload'].presence || {}, headers
  134. end
  135. private
  136. def handle(data, event = Event.new, headers)
  137. url = interpolated(event.payload)[:post_url]
  138. case method
  139. when 'get', 'delete'
  140. params = data
  141. body = nil
  142. when 'post', 'put', 'patch'
  143. params = nil
  144. content_type =
  145. if has_file_pointer?(event)
  146. data[interpolated(event.payload)['upload_key'].presence || 'file'] = get_upload_io(event)
  147. nil
  148. else
  149. interpolated(event.payload)['content_type']
  150. end
  151. case content_type
  152. when 'json'
  153. headers['Content-Type'] = 'application/json; charset=utf-8'
  154. body = data.to_json
  155. when 'xml'
  156. headers['Content-Type'] = 'text/xml; charset=utf-8'
  157. body = data.to_xml(root: (interpolated(event.payload)[:xml_root] || 'post'))
  158. when MIME_RE
  159. headers['Content-Type'] = content_type
  160. body = data.to_s
  161. else
  162. body = data
  163. end
  164. else
  165. error "Invalid method '#{method}'"
  166. end
  167. response = faraday.run_request(method.to_sym, url, body, headers) { |request|
  168. request.params.update(params) if params
  169. }
  170. if boolify(interpolated['emit_events'])
  171. new_event = interpolated['output_mode'].to_s == 'merge' ? event.payload.dup : {}
  172. create_event payload: new_event.merge(
  173. body: response.body,
  174. status: response.status
  175. ).merge(
  176. event_headers_payload(response.headers)
  177. )
  178. end
  179. end
  180. def event_headers_key
  181. super || 'headers'
  182. end
  183. end
  184. end