module Webhooks::Outgoing::DeliveryAttemptSupport

def attempt

def attempt
  uri = URI.parse(delivery.endpoint_url)
  if BulletTrain::OutgoingWebhooks.advanced_hostname_security
    unless allowed_uri?(uri)
      self.response_code = 0
      self.error_message = "URI is not allowed: " + uri
      return false
    end
  end
  hostname = if BulletTrain::OutgoingWebhooks.advanced_hostname_security
    resolve_ip_from_authoritative(uri.hostname.downcase)
  else
    uri.hostname.downcase
  end
  # Net::HTTP will consider the url invalid (and not deliver the webhook) unless it ends with a '/'
  if uri.path == ""
    uri.path = "/"
  end
  http = Net::HTTP.new(hostname, uri.port)
  if uri.scheme == "https"
    http.use_ssl = true
    if BulletTrain::OutgoingWebhooks.http_verify_mode
      # Developers might need to set this to `OpenSSL::SSL::VERIFY_NONE` in some cases.
      http.verify_mode = BulletTrain::OutgoingWebhooks.http_verify_mode
    end
  end
  request = Net::HTTP::Post.new(uri.request_uri)
  request.add_field("Host", uri.host)
  request.add_field("Content-Type", "application/json")
  request.body = delivery.event.payload.to_json
  begin
    response = http.request(request)
    self.response_message = response.message
    self.response_code = response.code
    self.response_body = response.body
  rescue => exception
    self.response_code = 0
    self.error_message = exception.message
  end
  save
  successful?
end

def failed?

def failed?
  !(successful? || still_attempting?)
end

def label_string

def label_string
  "#{attempt_number.ordinalize} Attempt"
end

def still_attempting?

def still_attempting?
  error_message.nil? && response_code.nil?
end

def successful?

def successful?
  SUCCESS_RESPONSE_CODES.include?(response_code)
end