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 92
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
flat_hash(h) click to toggle source
# File lib/paypal-sdk/core/api/rest.rb, line 155
def flat_hash(h)
  new_hash = {}
  h.each_pair do |key, val|
    if val.is_a?(Hash)
      new_hash.merge!(flat_hash(val))
    else
      new_hash[key] = val
    end
  end
  new_hash
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 121
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(payload[:header])} #{token(nil, payload[:header])}").
    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 139
def format_response(payload)
  response = payload[:response]
  payload[:data] =
    if response.body && response.body.strip == ""
      {}
    elsif 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" => flat_hash(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

# File lib/paypal-sdk/core/api/rest.rb, line 109
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

# File lib/paypal-sdk/core/api/rest.rb, line 168
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

# File lib/paypal-sdk/core/api/rest.rb, line 21
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 31
def set_config(*args)
  @token_uri = nil
  @token_hash = nil
  super
end
token(auth_code=nil, headers={}) click to toggle source

Get access token

# File lib/paypal-sdk/core/api/rest.rb, line 68
def token(auth_code=nil, headers={})
  token_hash(auth_code, headers)[:access_token]
end
token=(new_token) click to toggle source

token setter

# File lib/paypal-sdk/core/api/rest.rb, line 78
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 26
def token_endpoint
  config.rest_token_endpoint || service_endpoint
end
token_hash(auth_code=nil, headers=nil) click to toggle source

Generate Oauth token or Get cached

# File lib/paypal-sdk/core/api/rest.rb, line 48
def token_hash(auth_code=nil, headers=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}" }).merge(headers)
      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(headers={}) click to toggle source

Get access token type

# File lib/paypal-sdk/core/api/rest.rb, line 73
def token_type(headers={})
  token_hash(nil, headers)[:token_type] || "Bearer"
end
token_uri() click to toggle source

URI object token endpoint

# File lib/paypal-sdk/core/api/rest.rb, line 38
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 83
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