Нет описания http://j1x-huginn.herokuapp.com

post_agent.rb 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. module Agents
  2. class PostAgent < Agent
  3. include WebRequestConcern
  4. MIME_RE = /\A\w+\/.+\z/
  5. can_dry_run!
  6. no_bulk_receive!
  7. default_schedule "never"
  8. description <<-MD
  9. 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`.
  10. The `post_url` field must specify where you would like to send requests. Please include the URI scheme (`http` or `https`).
  11. The `method` used can be any of `get`, `post`, `put`, `patch`, and `delete`.
  12. By default, non-GETs will be sent with form encoding (`application/x-www-form-urlencoded`).
  13. Change `content_type` to `json` to send JSON instead.
  14. Change `content_type` to `xml` to send XML, where the name of the root element may be specified using `xml_root`, defaulting to `post`.
  15. 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`.
  16. If `emit_events` is set to `true`, the server response will be emitted as an Event and can be fed to a WebsiteAgent for parsing (using its `data_from_event` and `type` options). No data processing
  17. will be attempted by this Agent, so the Event's "body" value will always be raw text.
  18. Other Options:
  19. * `headers` - When present, it should be a hash of headers to send with the request.
  20. * `basic_auth` - Specify HTTP basic auth parameters: `"username:password"`, or `["username", "password"]`.
  21. * `disable_ssl_verification` - Set to `true` to disable ssl verification.
  22. * `user_agent` - A custom User-Agent name (default: "Faraday v#{Faraday::VERSION}").
  23. MD
  24. event_description <<-MD
  25. Events look like this:
  26. {
  27. "status": 200,
  28. "headers": {
  29. "Content-Type": "text/html",
  30. ...
  31. },
  32. "body": "<html>Some data...</html>"
  33. }
  34. MD
  35. def default_options
  36. {
  37. 'post_url' => "http://www.example.com",
  38. 'expected_receive_period_in_days' => '1',
  39. 'content_type' => 'form',
  40. 'method' => 'post',
  41. 'payload' => {
  42. 'key' => 'value',
  43. 'something' => 'the event contained {{ somekey }}'
  44. },
  45. 'headers' => {},
  46. 'emit_events' => 'false',
  47. 'no_merge' => 'false'
  48. }
  49. end
  50. def working?
  51. last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  52. end
  53. def method
  54. (interpolated['method'].presence || 'post').to_s.downcase
  55. end
  56. def validate_options
  57. unless options['post_url'].present? && options['expected_receive_period_in_days'].present?
  58. errors.add(:base, "post_url and expected_receive_period_in_days are required fields")
  59. end
  60. if options['payload'].present? && %w[get delete].include?(method) && !options['payload'].is_a?(Hash)
  61. errors.add(:base, "if provided, payload must be a hash")
  62. end
  63. if options['payload'].present? && %w[post put patch].include?(method)
  64. if !options['payload'].is_a?(Hash) && options['content_type'] !~ MIME_RE
  65. errors.add(:base, "if provided, payload must be a hash")
  66. end
  67. if options['content_type'] =~ MIME_RE && options['payload'].is_a?(String) && boolify(options['no_merge']) != true
  68. errors.add(:base, "when the payload is a string, `no_merge` has to be set to `true`")
  69. end
  70. end
  71. if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
  72. errors.add(:base, "if provided, emit_events must be true or false")
  73. end
  74. unless %w[post get put delete patch].include?(method)
  75. errors.add(:base, "method must be 'post', 'get', 'put', 'delete', or 'patch'")
  76. end
  77. if options['no_merge'].present? && !%[true false].include?(options['no_merge'].to_s)
  78. errors.add(:base, "if provided, no_merge must be 'true' or 'false'")
  79. end
  80. unless headers.is_a?(Hash)
  81. errors.add(:base, "if provided, headers must be a hash")
  82. end
  83. validate_web_request_options!
  84. end
  85. def receive(incoming_events)
  86. incoming_events.each do |event|
  87. outgoing = interpolated(event)['payload'].presence || {}
  88. if boolify(interpolated['no_merge'])
  89. handle outgoing, event.payload
  90. else
  91. handle outgoing.merge(event.payload), event.payload
  92. end
  93. end
  94. end
  95. def check
  96. handle interpolated['payload'].presence || {}
  97. end
  98. private
  99. def handle(data, payload = {})
  100. url = interpolated(payload)[:post_url]
  101. headers = headers()
  102. case method
  103. when 'get', 'delete'
  104. params, body = data, nil
  105. when 'post', 'put', 'patch'
  106. params = nil
  107. case (content_type = interpolated(payload)['content_type'])
  108. when 'json'
  109. headers['Content-Type'] = 'application/json; charset=utf-8'
  110. body = data.to_json
  111. when 'xml'
  112. headers['Content-Type'] = 'text/xml; charset=utf-8'
  113. body = data.to_xml(root: (interpolated(payload)[:xml_root] || 'post'))
  114. when MIME_RE
  115. headers['Content-Type'] = content_type
  116. body = data.to_s
  117. else
  118. body = data
  119. end
  120. else
  121. error "Invalid method '#{method}'"
  122. end
  123. response = faraday.run_request(method.to_sym, url, body, headers) { |request|
  124. request.params.update(params) if params
  125. }
  126. if boolify(interpolated['emit_events'])
  127. create_event payload: { body: response.body, headers: response.headers, status: response.status }
  128. end
  129. end
  130. end
  131. end