Class: PayPal::PayPalHttpClient

Inherits:
PayPalHttp::HttpClient
  • Object
show all
Defined in:
lib/core/paypal_http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, refresh_token = nil) ⇒ PayPalHttpClient

Returns a new instance of PayPalHttpClient.


9
10
11
12
13
14
15
# File 'lib/core/paypal_http_client.rb', line 9

def initialize(environment, refresh_token = nil)
  super(environment)
  @refresh_token = refresh_token

  add_injector(&method(:_sign_request))
  add_injector(&method(:_add_headers))
end

Instance Attribute Details

#refresh_tokenObject

Returns the value of attribute refresh_token


7
8
9
# File 'lib/core/paypal_http_client.rb', line 7

def refresh_token
  @refresh_token
end

Instance Method Details

#_add_headers(request) ⇒ Object


39
40
41
42
43
44
45
# File 'lib/core/paypal_http_client.rb', line 39

def _add_headers(request)
  request.headers["Accept-Encoding"] = "gzip"
  request.headers["sdk_name"] = "Checkout SDK"
  request.headers["sdk_version"] = VERSION
  request.headers["sdk_tech_stack"] = "Ruby" + RUBY_VERSION
  request.headers["api_integration_type"] = "PAYPALSDK"
end

#_has_auth_header(request) ⇒ Object


52
53
54
# File 'lib/core/paypal_http_client.rb', line 52

def _has_auth_header(request)
  request.headers.key?("Authorization")
end

#_is_auth_request(request) ⇒ Object


47
48
49
50
# File 'lib/core/paypal_http_client.rb', line 47

def _is_auth_request(request)
  request.path == '/v1/oauth2/token' ||
    request.path == '/v1/identity/openidconnect/tokenservice'
end

#_sign_request(request) ⇒ Object


28
29
30
31
32
33
34
35
36
37
# File 'lib/core/paypal_http_client.rb', line 28

def _sign_request(request)
  if (!_has_auth_header(request) && !_is_auth_request(request))
    if (!@access_token || @access_token.isExpired)
      accessTokenRequest = PayPal::AccessTokenRequest.new(@environment, @refresh_token)
      tokenResponse = execute(accessTokenRequest)
      @access_token = PayPal::AccessToken.new(tokenResponse.result)
    end
    request.headers["Authorization"] = @access_token.authorizationString()
  end
end

#user_agentObject


17
18
19
20
21
22
23
24
25
26
# File 'lib/core/paypal_http_client.rb', line 17

def user_agent
  library_details ||= "paypal-checkout-sdk #{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

  "PayPalSDK/rest-sdk-ruby #{VERSION} (#{library_details})"
end