lib/ringcentral_sdk/rest/client.rb in ringcentral_sdk-1.2.3 vs lib/ringcentral_sdk/rest/client.rb in ringcentral_sdk-1.3.0
- old
+ new
@@ -2,10 +2,12 @@
require 'faraday'
require 'faraday_middleware'
require 'faraday_middleware/oauth2_refresh'
require 'oauth2'
+require 'pp'
+
module RingCentralSdk::REST
class Client
ACCESS_TOKEN_TTL = 600 # 10 minutes
REFRESH_TOKEN_TTL = 36000 # 10 hours
REFRESH_TOKEN_TTL_REMEMBER = 604800 # 1 week
@@ -19,11 +21,10 @@
DEFAULT_LANGUAGE = 'en-us'
attr_reader :app_config
attr_reader :http
attr_reader :oauth2client
- attr_reader :token
attr_reader :user_agent
attr_reader :messages
attr_reader :instance_headers
@@ -46,11 +47,10 @@
@app_config = new_app_config
@oauth2client = new_oauth2_client()
end
def init_attributes()
- @token = nil
@http = nil
@user_agent = get_user_agent()
end
def api_version_url()
@@ -99,12 +99,12 @@
def authorize_url(opts = {})
@oauth2client.auth_code.authorize_url(_add_redirect_uri(opts))
end
- def authorize_code(code, opts = {})
- token = @oauth2client.auth_code.get_token(code, _add_redirect_uri(opts))
+ def authorize_code(code, params = {})
+ token = @oauth2client.auth_code.get_token(code, _add_redirect_uri(params))
set_token(token)
return token
end
def _add_redirect_uri(opts = {})
@@ -112,34 +112,36 @@
opts[:redirect_uri] = @app_config.redirect_url.to_s
end
return opts
end
- def authorize_password(username, extension = '', password = '', remember = false)
+ def authorize_password(username, extension = '', password = '', params = {})
token = @oauth2client.password.get_token(username, password, {
extension: extension,
- headers: {'Authorization' => 'Basic ' + get_api_key()}})
+ headers: {'Authorization' => 'Basic ' + get_api_key()}}.merge(params))
set_token(token)
return token
end
- def authorize_user(user, remember = false)
- authorize_password(user.username, user.extension, user.password)
+ def authorize_user(user, params = {})
+ authorize_password(user.username, user.extension, user.password, params)
end
+ def token
+ return @http ? @http.builder.app.oauth2_token : nil
+ end
+
def set_token(token)
if token.is_a? Hash
token = OAuth2::AccessToken::from_hash(@oauth2client, token)
end
unless token.is_a? OAuth2::AccessToken
raise "Token is not a OAuth2::AccessToken"
end
- @token = token
-
@http = Faraday.new(url: api_version_url()) do |conn|
- conn.request :oauth2_refresh, @token
+ conn.request :oauth2_refresh, token
conn.request :multipart
conn.request :url_encoded
conn.request :json
conn.headers['User-Agent'] = @user_agent
if @instance_headers.is_a? Hash