Class: PayPal::SDK::Core::API::Base

Inherits:
Object
  • Object
show all
Includes:
Util::HTTPHelper
Defined in:
lib/paypal-sdk/core/api/base.rb

Overview

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

Example

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

Direct Known Subclasses

Merchant, Platform, REST, OpenIDConnect::API

Constant Summary collapse

DEFAULT_API_MODE =
:sandbox
API_MODES =
[ :live, :sandbox ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::HTTPHelper

#configure_ssl, #create_http_connection, #default_ca_file, #encode_www_form, #handle_response, #http_call, #log_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

#config

Methods included from Logging

#log_event, #logger, logger, logger=

Constructor Details

#initialize(service_name = "", environment = nil, options = {}) ⇒ Base

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


26
27
28
29
30
31
32
# 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

Instance Attribute Details

#httpObject

Returns the value of attribute http.



12
13
14
# File 'lib/paypal-sdk/core/api/base.rb', line 12

def http
  @http
end

#service_nameObject

Returns the value of attribute service_name.



12
13
14
# File 'lib/paypal-sdk/core/api/base.rb', line 12

def service_name
  @service_name
end

#uriObject

Returns the value of attribute uri.



12
13
14
# File 'lib/paypal-sdk/core/api/base.rb', line 12

def uri
  @uri
end

Class Method Details

.sdk_library_detailsObject



152
153
154
# 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_agentObject



156
157
158
# File 'lib/paypal-sdk/core/api/base.rb', line 156

def user_agent
  @user_agent ||= "PayPalSDK/sdk-core-ruby #{VERSION} (#{sdk_library_details})"
end

Instance Method Details

#api_call(payload) ⇒ Object

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



78
79
80
81
82
83
84
85
86
87
# 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_modeObject

Get configured API mode( sandbox or live)



54
55
56
57
58
59
60
# 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_headerObject

Default Http header



68
69
70
# 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 = {}) ⇒ Object



115
116
117
118
# 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) ⇒ Object

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

Arguments

  • exception – Exception object.

  • message – Readable error message.



147
148
149
# File 'lib/paypal-sdk/core/api/base.rb', line 147

def format_error(exception, message)
  raise exception
end

#format_request(payload) ⇒ Object

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



128
129
130
131
132
# 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) ⇒ Object

Format Response object. It will be override by child class

Argument

  • action – Request action

  • response – HTTP response object



138
139
140
141
# 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 = {}) ⇒ Object



100
101
102
103
# 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 = {}) ⇒ Object



105
106
107
108
# 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 = {}) ⇒ Object Also known as: request

Generate HTTP request for given action and parameters

Arguments

  • action – Action to perform

  • params – (Optional) Parameters for the action

  • initheader – (Optional) HTTP header



94
95
96
97
# 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

#put(action, params = {}, header = {}) ⇒ Object



110
111
112
113
# 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

#service_endpointObject

Get service end point



63
64
65
# File 'lib/paypal-sdk/core/api/base.rb', line 63

def service_endpoint
  config.endpoint
end

#set_config(*args) ⇒ Object

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



48
49
50
51
# File 'lib/paypal-sdk/core/api/base.rb', line 48

def set_config(*args)
  @http = @uri = nil
  super
end