Module: PayPal::SDK::Core::Util::HTTPHelper

Includes:
Authentication, Configuration, Exceptions, Logging
Included in:
API::Base, API::IPN::Message
Defined in:
lib/paypal-sdk/core/util/http_helper.rb

Instance Method Summary collapse

Methods included from Authentication

#add_certificate, #base_credential, #base_credential_type, #credential, #set_config, #third_party_credential

Methods included from Configuration

#config, #set_config

Methods included from Logging

#log_event, #logger, logger, logger=

Instance Method Details

#configure_ssl(http) ⇒ Object

Apply ssl configuration to http object



41
42
43
44
45
46
47
48
49
50
51
# 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
    config.ssl_options.each do |key, value|
      http.send("#{key}=", value)
    end
    add_certificate(https)
  end
end

#create_http_connection(uri) ⇒ Object

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



15
16
17
18
19
20
21
22
23
# 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_fileObject

Default ca file



36
37
38
# 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) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 104

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) ⇒ Object

Handles response and error codes from the remote service.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 113

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) ⇒ Object

Make Http call

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 60

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) ⇒ Object

Log Http call

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



77
78
79
80
81
82
83
84
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 77

def log_http_call(payload)
  logger.info "Request[#{payload[:method]}]: #{payload[:uri].to_s}"
  start_time = Time.now
  response = yield
  logger.info sprintf("Response[%s]: %s, Duration: %.3fs", response.code,
    response.message, Time.now - start_time)
  response
end

#map_header_value(header_keys, properties) ⇒ Object

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” }



95
96
97
98
99
100
101
102
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 95

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) ⇒ Object

New raw HTTP object



26
27
28
29
30
31
32
33
# 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) ⇒ Object

Join url



54
55
56
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 54

def url_join(path, action)
  path.sub(/\/?$/, "/#{action}")
end