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
52
53
54
55
56
# 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) ⇒ 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


119
120
121
122
123
124
125
# 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) ⇒ Object

Handles response and error codes from the remote service.


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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) ⇒ Object

Make Http call

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


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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) ⇒ Object

Log Http call

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


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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) ⇒ 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” }


110
111
112
113
114
115
116
117
# 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) ⇒ 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


59
60
61
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 59

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