class PayPal::SDK::Core::API::REST
Constants
- DEFAULT_HTTP_HEADER
- DEFAULT_REST_END_POINTS
- NVP_AUTH_HEADER
- TOKEN_REQUEST_PARAMS
Attributes
token_hash[W]
Public Instance Methods
api_call(payload)
click to toggle source
Override the API
call to handle Token Expire
Calls superclass method
PayPal::SDK::Core::API::Base#api_call
# 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)
click to toggle source
Format request payload
Argument¶ ↑
-
payload( uri, action, params, header)
Generate¶ ↑
-
payload( uri, body, header )
# 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)
click to toggle source
Format response payload
Argument¶ ↑
-
payload( response )
Generate¶ ↑
-
payload( data )
# 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.message, "developer_msg" => response } } end payload end
handle_response(response)
click to toggle source
Validate HTTP response
Calls superclass method
PayPal::SDK::Core::Util::HTTPHelper#handle_response
# 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)
click to toggle source
Log PayPal-Request-Id header
Calls superclass method
PayPal::SDK::Core::Util::HTTPHelper#log_http_call
# 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()
click to toggle source
Get REST
service end point
Calls superclass method
PayPal::SDK::Core::API::Base#service_endpoint
# 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)
click to toggle source
Clear cached values.
Calls superclass method
PayPal::SDK::Core::API::Base#set_config
# 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)
click to toggle source
Get access token
# 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)
click to toggle source
token setter
# 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()
click to toggle source
Token endpoint
# File lib/paypal-sdk/core/api/rest.rb, line 27 def token_endpoint config.rest_token_endpoint || service_endpoint end
token_hash(auth_code=nil)
click to toggle source
Generate Oauth token or Get cached
# 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
token_type()
click to toggle source
Get access token type
# File lib/paypal-sdk/core/api/rest.rb, line 74 def token_type token_hash[:token_type] || "Bearer" end
token_uri()
click to toggle source
URI object token endpoint
# 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()
click to toggle source
Check token expired or not
# 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