module PayPal::SDK::Core::Util::HTTPHelper

Public Instance Methods

configure_ssl(http) click to toggle source

Apply ssl configuration to http object

# File lib/paypal-sdk/core/util/http_helper.rb, line 41
def configure_ssl(http)
  http.tap do |https|
    https.use_ssl = true
    https.ca_file = default_ca_file
    https.verify_mode = OpenSSL::SSL::VERIFY_PEER
    begin
      https.ssl_version = :TLSv1_2
    rescue => error
      logger.warn("WARNING: Your system does not support TLSv1.2. Per PCI Security Council mandate (https://github.com/paypal/TLS-update), you MUST update to latest security library.")
    end
    config.ssl_options.each do |key, value|
      http.send("#{key}=", value)
    end
    add_certificate(https)
  end
end
create_http_connection(uri) click to toggle source

Create HTTP connection based on given service name or configured end point

# File lib/paypal-sdk/core/util/http_helper.rb, line 15
def create_http_connection(uri)
  new_http(uri).tap do |http|
    if config.http_timeout
      http.open_timeout = config.http_timeout
      http.read_timeout = config.http_timeout
    end
    configure_ssl(http) if uri.scheme == "https"
  end
end
default_ca_file() click to toggle source

Default ca file

# File lib/paypal-sdk/core/util/http_helper.rb, line 36
def default_ca_file
  File.expand_path("../../../../../data/paypal.crt", __FILE__)
end
encode_www_form(hash) click to toggle source
# File lib/paypal-sdk/core/util/http_helper.rb, line 119
def encode_www_form(hash)
  if defined? URI.encode_www_form
    URI.encode_www_form(hash)
  else
    hash.map{|key, value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}" }.join("&")
  end
end
handle_response(response) click to toggle source

Handles response and error codes from the remote service.

# File lib/paypal-sdk/core/util/http_helper.rb, line 128
def handle_response(response)
  case response.code.to_i
    when 301, 302, 303, 307
      raise(Redirection.new(response))
    when 200...400
      response
    when 400
      raise(BadRequest.new(response))
    when 401
      raise(UnauthorizedAccess.new(response))
    when 403
      raise(ForbiddenAccess.new(response))
    when 404
      raise(ResourceNotFound.new(response))
    when 405
      raise(MethodNotAllowed.new(response))
    when 409
      raise(ResourceConflict.new(response))
    when 410
      raise(ResourceGone.new(response))
    when 422
      raise(ResourceInvalid.new(response))
    when 401...500
      raise(ClientError.new(response))
    when 500...600
      raise(ServerError.new(response))
    else
      raise(ConnectionError.new(response, "Unknown response code: #{response.code}"))
  end
end
http_call(payload) click to toggle source

Make Http call

  • payload - Hash(:http, :method, :uri, :body, :header)

# File lib/paypal-sdk/core/util/http_helper.rb, line 65
def http_call(payload)
  response =
    log_http_call(payload) do
      http = payload[:http] || create_http_connection(payload[:uri])
      http.start do |session|
        if [ :get, :delete, :head ].include? payload[:method]
          session.send(payload[:method], payload[:uri].request_uri, payload[:header])
        else
          session.send(payload[:method], payload[:uri].request_uri, payload[:body], payload[:header])
        end
      end
    end

  handle_response(response)
end
log_http_call(payload) { || ... } click to toggle source

Log Http call

  • payload - Hash(:http, :method, :uri, :body, :header)

# File lib/paypal-sdk/core/util/http_helper.rb, line 83
def log_http_call(payload)
  logger.info "Request[#{payload[:method]}]: #{payload[:uri].to_s}"

  logger.debug "Request.body=#{payload[:body]}\trequest.header=#{payload[:header]}"

  start_time = Time.now
  response = yield
  logger.info sprintf("Response[%s]: %s, Duration: %.3fs", response.code,
    response.message, Time.now - start_time)

  logger.add(
    response_details_log_level(response),
    "Response.body=#{response.body}\tResponse.header=#{response.to_hash}"
  )

  response
end
map_header_value(header_keys, properties) click to toggle source

Generate header based on given header keys and properties

Arguments

  • header_keys – List of Header keys for the properties

  • properties – properties

Return

Hash with header as key property as value

Example

map_header_value( { :username => “X-PAYPAL-USERNAME”}, { :username => “guest” }) # Return: { “X-PAYPAL-USERNAME” => “guest” }

# File lib/paypal-sdk/core/util/http_helper.rb, line 110
def map_header_value(header_keys, properties)
  header = {}
  properties.each do |key, value|
    key = header_keys[key]
    header[key] = value.to_s if key and value
  end
  header
end
new_http(uri) click to toggle source

New raw HTTP object

# File lib/paypal-sdk/core/util/http_helper.rb, line 26
def new_http(uri)
  if config.http_proxy
    proxy = URI.parse(config.http_proxy)
    Net::HTTP.new(uri.host, uri.port, proxy.host, proxy.port, proxy.user, proxy.password)
  else
    Net::HTTP.new(uri.host, uri.port)
  end
end
url_join(path, action) click to toggle source

Join url

# File lib/paypal-sdk/core/util/http_helper.rb, line 59
def url_join(path, action)
  path.sub(/\/?$/, "/#{action}")
end

Private Instance Methods

response_details_log_level(response) click to toggle source
# File lib/paypal-sdk/core/util/http_helper.rb, line 161
def response_details_log_level(response)
  if (400...600).cover?(response.code.to_i)
    Logger::WARN
  else
    Logger::DEBUG
  end
end