class PayPal::SDK::Core::API::Base

API class provide default functionality for accessing the API web services.

Example

api      = API::Base.new("AdaptivePayments")
response = api.request("GetPaymentOptions", "")

Constants

API_MODES
DEFAULT_API_MODE

Attributes

http[RW]
service_name[RW]
uri[RW]

Public Class Methods

new(service_name = "", environment = nil, options = {}) click to toggle source

Initialize API object

Argument

  • service_name – (Optional) Service name

  • environment – (Optional) Configuration environment to load

  • options – (Optional) Override configuration.

Example

new("AdaptivePayments")
new("AdaptivePayments", :development)
new(:wsdl_service)       # It load wsdl_service configuration
# File lib/paypal-sdk/core/api/base.rb, line 26
def initialize(service_name = "", environment = nil, options = {})
  unless service_name.is_a? String
    environment, options, service_name = service_name, environment || {}, ""
  end
  @service_name = service_name
  set_config(environment, options)
end
sdk_library_details() click to toggle source
# File lib/paypal-sdk/core/api/base.rb, line 152
def sdk_library_details
  @library_details ||= "paypal-sdk-core #{PayPal::SDK::Core::VERSION}; ruby #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}-#{RUBY_PLATFORM}"
end
user_agent() click to toggle source
# File lib/paypal-sdk/core/api/base.rb, line 156
def user_agent
  @user_agent ||= "PayPalSDK/sdk-core-ruby #{VERSION} (#{sdk_library_details})"
end

Public Instance Methods

api_call(payload) click to toggle source

Generate HTTP request for given action and parameters

Arguments

  • http_method – HTTP method(get/put/post/delete/patch)

  • action – Action to perform

  • params – (Optional) Parameters for the action

  • initheader – (Optional) HTTP header

# File lib/paypal-sdk/core/api/base.rb, line 78
def api_call(payload)
  payload[:header] = default_http_header.merge(payload[:header])
  payload[:uri]   ||= uri.dup
  payload[:http]  ||= http.dup
  payload[:uri].query = encode_www_form(payload[:query]) if payload[:query] and payload[:query].any?
  format_request(payload)
  payload[:response] = http_call(payload)
  format_response(payload)
  payload[:data]
end
api_mode() click to toggle source

Get configured API mode( sandbox or live)

# File lib/paypal-sdk/core/api/base.rb, line 54
def api_mode
  if config.mode and API_MODES.include? config.mode.to_sym
    config.mode.to_sym
  else
    DEFAULT_API_MODE
  end
end
default_http_header() click to toggle source

Default Http header

# File lib/paypal-sdk/core/api/base.rb, line 68
def default_http_header
  { "User-Agent" => self.class.user_agent }
end
delete(action, params = {}, header = {}) click to toggle source
# File lib/paypal-sdk/core/api/base.rb, line 115
def delete(action, params = {}, header = {})
  action, params, header = "", action, params if action.is_a? Hash
  api_call(:method => :delete, :action => action, :params => params, :header => header)
end
format_error(exception, message) click to toggle source

Format Error object. It will be override by child class.

Arguments

  • exception – Exception object.

  • message – Readable error message.

# File lib/paypal-sdk/core/api/base.rb, line 147
def format_error(exception, message)
  raise exception
end
format_request(payload) click to toggle source

Format Request data. It will be override by child class

Arguments

  • action – Request action

  • params – Request parameters

Return

  • path – Formated request uri object

  • params – Formated request Parameters

  • header – HTTP Header

# File lib/paypal-sdk/core/api/base.rb, line 128
def format_request(payload)
  payload[:uri].path = url_join(payload[:uri].path, payload[:action])
  payload[:body] = payload[:params].to_s
  payload
end
format_response(payload) click to toggle source

Format Response object. It will be override by child class

Argument

  • action – Request action

  • response – HTTP response object

# File lib/paypal-sdk/core/api/base.rb, line 138
def format_response(payload)
  payload[:data] = payload[:response].body
  payload
end
get(action, params = {}, header = {}) click to toggle source
# File lib/paypal-sdk/core/api/base.rb, line 100
def get(action, params = {}, header = {})
  action, params, header = "", action, params if action.is_a? Hash
  api_call(:method => :get, :action => action, :query => params, :params => nil, :header => header)
end
patch(action, params = {}, header = {}) click to toggle source
# File lib/paypal-sdk/core/api/base.rb, line 105
def patch(action, params = {}, header = {})
  action, params, header = "", action, params if action.is_a? Hash
  api_call(:method => :patch, :action => action, :params => params, :header => header)
end
post(action, params = {}, header = {}) click to toggle source

Generate HTTP request for given action and parameters

Arguments

  • action – Action to perform

  • params – (Optional) Parameters for the action

  • initheader – (Optional) HTTP header

# File lib/paypal-sdk/core/api/base.rb, line 94
def post(action, params = {}, header = {})
  action, params, header = "", action, params if action.is_a? Hash
  api_call(:method => :post, :action => action, :params => params, :header => header)
end
Also aliased as: request
put(action, params = {}, header = {}) click to toggle source
# File lib/paypal-sdk/core/api/base.rb, line 110
def put(action, params = {}, header = {})
  action, params, header = "", action, params if action.is_a? Hash
  api_call(:method => :put, :action => action, :params => params, :header => header)
end
request(action, params = {}, header = {})
Alias for: post
service_endpoint() click to toggle source

Get service end point

# File lib/paypal-sdk/core/api/base.rb, line 63
def service_endpoint
  config.endpoint
end
set_config(*args) click to toggle source

Override set_config method to create http connection on changing the configuration.

Calls superclass method
# File lib/paypal-sdk/core/api/base.rb, line 48
def set_config(*args)
  @http = @uri = nil
  super
end