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

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

28
29
30
31
32
33
34
# File 'lib/paypal-sdk/core/api/base.rb', line 28

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


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

def http
  @http
end

#service_nameObject

Returns the value of attribute service_name


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

def service_name
  @service_name
end

#uriObject

Returns the value of attribute uri


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

def uri
  @uri
end

Class Method Details

.sdk_library_detailsObject


154
155
156
157
158
159
160
161
# File 'lib/paypal-sdk/core/api/base.rb', line 154

def sdk_library_details
  @library_details ||= "paypal-sdk-core #{PayPal::SDK::REST::VERSION}; ruby #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}-#{RUBY_PLATFORM}"
  begin
    @library_details << ";#{OpenSSL::OPENSSL_LIBRARY_VERSION}"
  rescue NameError
    @library_details << ";OpenSSL #{OpenSSL::OPENSSL_VERSION}"
  end
end

.user_agentObject


163
164
165
# File 'lib/paypal-sdk/core/api/base.rb', line 163

def user_agent
  @user_agent ||= "PayPalSDK/rest-sdk-ruby #{PayPal::SDK::REST::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


80
81
82
83
84
85
86
87
88
89
# File 'lib/paypal-sdk/core/api/base.rb', line 80

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)


56
57
58
59
60
61
62
# File 'lib/paypal-sdk/core/api/base.rb', line 56

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


70
71
72
# File 'lib/paypal-sdk/core/api/base.rb', line 70

def default_http_header
  { "User-Agent" => self.class.user_agent }
end

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


117
118
119
120
# File 'lib/paypal-sdk/core/api/base.rb', line 117

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.


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

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


130
131
132
133
134
# File 'lib/paypal-sdk/core/api/base.rb', line 130

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


140
141
142
143
# File 'lib/paypal-sdk/core/api/base.rb', line 140

def format_response(payload)
  payload[:data] = payload[:response].body
  payload
end

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


102
103
104
105
# File 'lib/paypal-sdk/core/api/base.rb', line 102

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


107
108
109
110
# File 'lib/paypal-sdk/core/api/base.rb', line 107

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 = {}, query = {}) ⇒ 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


96
97
98
99
# File 'lib/paypal-sdk/core/api/base.rb', line 96

def post(action, params = {}, header = {}, query = {})
  action, params, header = "", action, params if action.is_a? Hash
  api_call(:method => :post, :action => action, :query => query, :params => params, :header => header)
end

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


112
113
114
115
# File 'lib/paypal-sdk/core/api/base.rb', line 112

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


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

def service_endpoint
  config.endpoint
end

#set_config(*args) ⇒ Object

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


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

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