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
Public Class Methods
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 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
# 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
# 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
Public Instance Methods
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 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
Get configured API
mode( sandbox or live)
# 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 header
# File lib/paypal-sdk/core/api/base.rb, line 70 def default_http_header { "User-Agent" => self.class.user_agent } end
# 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 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 130 def format_request(payload) payload[:uri].path = url_join(payload[:uri].path, payload[:action]) payload[:body] = payload[:params].to_s payload end
# 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
# 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
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 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
# 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
Get service end point
# File lib/paypal-sdk/core/api/base.rb, line 65 def service_endpoint config.endpoint end
Override set_config
method to create http connection on changing the configuration.
# File lib/paypal-sdk/core/api/base.rb, line 50 def set_config(*args) @http = @uri = nil super end