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
-
#configure_ssl(http) ⇒ Object
Apply ssl configuration to http object.
-
#create_http_connection(uri) ⇒ Object
Create HTTP connection based on given service name or configured end point.
-
#default_ca_file ⇒ Object
Default ca file.
-
#default_cert_store ⇒ Object
Build a certificate store with system defaults and bundled PayPal certs.
- #encode_www_form(hash) ⇒ Object
-
#handle_response(response) ⇒ Object
Handles response and error codes from the remote service.
-
#http_call(payload) ⇒ Object
Make Http call * payload - Hash(:http, :method, :uri, :body, :header).
-
#log_http_call(payload) ⇒ Object
Log Http call * payload - Hash(:http, :method, :uri, :body, :header).
-
#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” }. -
#new_http(uri) ⇒ Object
New raw HTTP object.
-
#url_join(path, action) ⇒ Object
Join url.
Methods included from Authentication
#add_certificate, #base_credential, #base_credential_type, #credential, #set_config, #third_party_credential
Methods included from Configuration
Methods included from Logging
#log_event, #logger, logger, logger=
Instance Method Details
#configure_ssl(http) ⇒ Object
Apply ssl configuration to http object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 51 def configure_ssl(http) http.tap do |https| https.use_ssl = true https.cert_store = default_cert_store 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..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_file ⇒ Object
Default ca file
36 37 38 |
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 36 def default_ca_file File.("../../../../../data/paypal.crt", __FILE__) end |
#default_cert_store ⇒ Object
Build a certificate store with system defaults and bundled PayPal certs. This avoids pinning trust to only paypal.crt and works in environments where additional trusted roots are required.
43 44 45 46 47 48 |
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 43 def default_cert_store OpenSSL::X509::Store.new.tap do |store| store.set_default_paths store.add_file(default_ca_file) if File.exist?(default_ca_file) end end |
#encode_www_form(hash) ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 129 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.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 138 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)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 75 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)
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 93 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., 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” }
120 121 122 123 124 125 126 127 |
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 120 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
69 70 71 |
# File 'lib/paypal-sdk/core/util/http_helper.rb', line 69 def url_join(path, action) path.sub(/\/?$/, "/#{action}") end |