class MagicPipe::Transports::Https

Attributes

conn[R]

Public Class Methods

new(config, metrics) click to toggle source
Calls superclass method MagicPipe::Transports::Base::new
# File lib/magic_pipe/transports/https.rb, line 10
def initialize(config, metrics)
  super(config, metrics)
  @options = @config.https_transport_options
  @conn = build_connection
  @path_builder = @options[:dynamic_path_builder]
end

Public Instance Methods

submit!(payload, metadata) click to toggle source

TODO: should this raise an error on failure? So that it can be retried?

# File lib/magic_pipe/transports/https.rb, line 23
def submit!(payload, metadata)
  username, password = basic_auth(metadata[:topic])
  @conn.basic_auth(username, password || "x")
  resp = @conn.post do |r|
    path = dynamic_path(metadata[:topic])
    r.url(path) if path

    r.body = payload
    r.headers["X-MagicPipe-Sent-At"] = metadata[:time]
    r.headers["X-MagicPipe-Topic"] = metadata[:topic]
    r.headers["X-MagicPipe-Producer"] = metadata[:producer]
  end

  unless resp.success?
    msg = %Q{HTTP response: status=#{resp.status} body="#{resp.body}"}
    raise SubmitFailedError.new(self.class, msg)
  end
end

Private Instance Methods

basic_auth(topic) click to toggle source
# File lib/magic_pipe/transports/https.rb, line 55
def basic_auth(topic)
  user_auth = @options.fetch(:basic_auth)
  credentials = if user_auth.respond_to?(:call)
    user_auth.call(topic)
  else
    user_auth
  end
  credentials.split(':')
end
build_connection() click to toggle source

For a single backend, can't this be cached as a read only global?

# File lib/magic_pipe/transports/https.rb, line 87
def build_connection
  Faraday.new(url) do |f|
    f.request :retry, max: 2, interval: 0.1, backoff_factor: 2

    f.headers['Content-Type'] = content_type
    f.headers['User-Agent'] = user_agent

    f.options.timeout = timeout
    f.options.open_timeout = open_timeout

    f.adapter :typhoeus
  end
end
content_type() click to toggle source
# File lib/magic_pipe/transports/https.rb, line 73
def content_type
  MagicPipe::Codecs.lookup(@config.codec)::TYPE
end
dynamic_path(topic) click to toggle source
# File lib/magic_pipe/transports/https.rb, line 46
def dynamic_path(topic)
  return nil unless !!@path_builder
  @path_builder.call(topic)
end
open_timeout() click to toggle source
# File lib/magic_pipe/transports/https.rb, line 69
def open_timeout
  @options.fetch(:open_timeout)
end
timeout() click to toggle source
# File lib/magic_pipe/transports/https.rb, line 65
def timeout
  @options.fetch(:timeout)
end
url() click to toggle source
# File lib/magic_pipe/transports/https.rb, line 51
def url
  @options.fetch(:url)
end
user_agent() click to toggle source
# File lib/magic_pipe/transports/https.rb, line 77
def user_agent
  "MagicPipe v%s (Faraday v%s, Typhoeus v%s)" % [
    MagicPipe::VERSION,
    Faraday::VERSION,
    Typhoeus::VERSION
  ]
end