Class: PayPal::SDK::Core::API::REST
- Defined in:
- lib/paypal-sdk/core/api/rest.rb
Constant Summary collapse
- NVP_AUTH_HEADER =
{ :sandbox_email_address => "X-PAYPAL-SANDBOX-EMAIL-ADDRESS", :device_ipaddress => "X-PAYPAL-DEVICE-IPADDRESS" }
- DEFAULT_HTTP_HEADER =
{ "Content-Type" => "application/json" }
- DEFAULT_REST_END_POINTS =
{ :sandbox => "https://api.sandbox.paypal.com", :live => "https://api.paypal.com", :tls_test => "https://test-api.sandbox.paypal.com" }
- TOKEN_REQUEST_PARAMS =
"grant_type=client_credentials"
Constants inherited from Base
Base::API_MODES, Base::DEFAULT_API_MODE
Instance Attribute Summary collapse
-
#token_hash(auth_code = nil) ⇒ Object
Generate Oauth token or Get cached.
Attributes inherited from Base
Instance Method Summary collapse
-
#api_call(payload) ⇒ Object
Override the API call to handle Token Expire.
-
#format_request(payload) ⇒ Object
Format request payload === Argument * payload( uri, action, params, header) === Generate * payload( uri, body, header ).
-
#format_response(payload) ⇒ Object
Format response payload === Argument * payload( response ) === Generate * payload( data ).
-
#handle_response(response) ⇒ Object
Validate HTTP response.
-
#log_http_call(payload) ⇒ Object
Log PayPal-Request-Id header.
-
#service_endpoint ⇒ Object
Get REST service end point.
-
#set_config(*args) ⇒ Object
Clear cached values.
-
#token(auth_code = nil) ⇒ Object
Get access token.
-
#token=(new_token) ⇒ Object
token setter.
-
#token_endpoint ⇒ Object
Token endpoint.
-
#token_type ⇒ Object
Get access token type.
-
#token_uri ⇒ Object
URI object token endpoint.
-
#validate_token_hash ⇒ Object
Check token expired or not.
Methods inherited from Base
#api_mode, #default_http_header, #delete, #format_error, #get, #initialize, #patch, #post, #put, sdk_library_details, user_agent
Methods included from Util::HTTPHelper
#configure_ssl, #create_http_connection, #default_ca_file, #encode_www_form, #http_call, #map_header_value, #new_http, #url_join
Methods included from PayPal::SDK::Core::Authentication
#add_certificate, #base_credential, #base_credential_type, #credential, #third_party_credential
Methods included from Configuration
Methods included from Logging
#log_event, #logger, logger, logger=
Constructor Details
This class inherits a constructor from PayPal::SDK::Core::API::Base
Instance Attribute Details
#token_hash(auth_code = nil) ⇒ Object
Generate Oauth token or Get cached
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/paypal-sdk/core/api/rest.rb', line 49 def token_hash(auth_code=nil) validate_token_hash @token_hash ||= begin @token_request_at = Time.now basic_auth = ["#{config.client_id}:#{config.client_secret}"].pack('m').delete("\r\n") token_headers = default_http_header.merge({ "Content-Type" => "application/x-www-form-urlencoded", "Authorization" => "Basic #{basic_auth}" }) if auth_code != nil TOKEN_REQUEST_PARAMS.replace "grant_type=authorization_code&response_type=token&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&code=" TOKEN_REQUEST_PARAMS << auth_code end response = http_call( :method => :post, :uri => token_uri, :body => TOKEN_REQUEST_PARAMS, :header => token_headers ) MultiJson.load(response.body, :symbolize_keys => true) end end |
Instance Method Details
#api_call(payload) ⇒ Object
Override the API call to handle Token Expire
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/paypal-sdk/core/api/rest.rb', line 93 def api_call(payload) backup_payload = payload.dup begin response = super(payload) rescue UnauthorizedAccess => error if @token_hash and config.client_id # Reset cached token and Retry api request @token_hash = nil response = super(backup_payload) else raise error end end response end |
#format_request(payload) ⇒ Object
Format request payload
Argument
-
payload( uri, action, params, header)
Generate
-
payload( uri, body, header )
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/paypal-sdk/core/api/rest.rb', line 122 def format_request(payload) # Request URI payload[:uri].path = url_join(payload[:uri].path, payload[:action]) # HTTP Header credential_properties = credential(payload[:uri].to_s).properties header = map_header_value(NVP_AUTH_HEADER, credential_properties) payload[:header] = header.merge("Authorization" => "#{token_type} #{token}"). merge(DEFAULT_HTTP_HEADER).merge(payload[:header]) # Post Data payload[:body] = MultiJson.dump(payload[:params]) payload end |
#format_response(payload) ⇒ Object
Format response payload
Argument
-
payload( response )
Generate
-
payload( data )
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/paypal-sdk/core/api/rest.rb', line 140 def format_response(payload) response = payload[:response] payload[:data] = if response.code >= "200" and response.code <= "299" response.body && response.content_type == "application/json" ? MultiJson.load(response.body) : {} elsif response.content_type == "application/json" { "error" => MultiJson.load(response.body) } else { "error" => { "name" => response.code, "message" => response., "developer_msg" => response } } end payload end |
#handle_response(response) ⇒ Object
Validate HTTP response
110 111 112 113 114 115 |
# File 'lib/paypal-sdk/core/api/rest.rb', line 110 def handle_response(response) super rescue BadRequest => error # Catch BadRequest to get validation error message from the response. error.response end |
#log_http_call(payload) ⇒ Object
Log PayPal-Request-Id header
155 156 157 158 159 160 |
# File 'lib/paypal-sdk/core/api/rest.rb', line 155 def log_http_call(payload) if payload[:header] and payload[:header]["PayPal-Request-Id"] logger.info "PayPal-Request-Id: #{payload[:header]["PayPal-Request-Id"]}" end super end |
#service_endpoint ⇒ Object
Get REST service end point
22 23 24 |
# File 'lib/paypal-sdk/core/api/rest.rb', line 22 def service_endpoint config.rest_endpoint || super || DEFAULT_REST_END_POINTS[api_mode] end |
#set_config(*args) ⇒ Object
Clear cached values.
32 33 34 35 36 |
# File 'lib/paypal-sdk/core/api/rest.rb', line 32 def set_config(*args) @token_uri = nil @token_hash = nil super end |
#token(auth_code = nil) ⇒ Object
Get access token
69 70 71 |
# File 'lib/paypal-sdk/core/api/rest.rb', line 69 def token(auth_code=nil) token_hash(auth_code)[:access_token] end |
#token=(new_token) ⇒ Object
token setter
79 80 81 |
# File 'lib/paypal-sdk/core/api/rest.rb', line 79 def token=(new_token) @token_hash = { :access_token => new_token, :token_type => "Bearer" } end |
#token_endpoint ⇒ Object
Token endpoint
27 28 29 |
# File 'lib/paypal-sdk/core/api/rest.rb', line 27 def token_endpoint config.rest_token_endpoint || service_endpoint end |
#token_type ⇒ Object
Get access token type
74 75 76 |
# File 'lib/paypal-sdk/core/api/rest.rb', line 74 def token_type token_hash[:token_type] || "Bearer" end |
#token_uri ⇒ Object
URI object token endpoint
39 40 41 42 43 44 45 46 |
# File 'lib/paypal-sdk/core/api/rest.rb', line 39 def token_uri @token_uri ||= begin new_uri = URI.parse(token_endpoint) new_uri.path = "/v1/oauth2/token" if new_uri.path =~ /^\/?$/ new_uri end end |
#validate_token_hash ⇒ Object
Check token expired or not
84 85 86 87 88 89 90 |
# File 'lib/paypal-sdk/core/api/rest.rb', line 84 def validate_token_hash if @token_request_at and @token_hash and @token_hash[:expires_in] and (Time.now - @token_request_at) > @token_hash[:expires_in].to_i @token_hash = nil end end |