class RingCentralSdk::REST::Client

Client is the RingCentral REST API client class which handles HTTP requests with built-in OAuth handling

Constants

ACCESS_TOKEN_TTL
ACCOUNT_ID
ACCOUNT_PREFIX
API_VERSION
AUTHZ_ENDPOINT
DEFAULT_LANGUAGE
REFRESH_TOKEN_TTL
REFRESH_TOKEN_TTL_REMEMBER
REVOKE_ENDPOINT
TOKEN_ENDPOINT
URL_PREFIX

Attributes

config[R]
http[R]
logger[R]
messages[R]
oauth2client[R]
user_agent[R]

Public Class Methods

new() { |config| ... } click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 33
def initialize
  init_attributes

  raise ArgumentError, 'Config block not given' unless block_given?
  @config = RingCentralSdk::REST::Configuration.new
  yield config
  @config.inflate

  @oauth2client = new_oauth2_client

  unless @config.username.to_s.empty?
    authorize_password @config.username, @config.extension, @config.password
  end

  @messages = RingCentralSdk::REST::Messages.new self
end

Public Instance Methods

_add_redirect_uri(opts = {}) click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 105
def _add_redirect_uri(opts = {})
  if !opts.key?(:redirect_uri) && !@config.redirect_url.to_s.empty?
    opts[:redirect_uri] = @config.redirect_url.to_s
  end
  opts
end
api_key() click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 179
def api_key
  Base64.encode64("#{@config.app_key}:#{@config.app_secret}").gsub(/\s/, '')
end
authorize(username, extension = '', password = '', params = {})
Alias for: authorize_password
authorize_code(code, params = {}) click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 99
def authorize_code(code, params = {})
  token = @oauth2client.auth_code.get_token(code, _add_redirect_uri(params))
  set_token(token)
  token
end
authorize_password(username, extension = '', password = '', params = {}) click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 112
def authorize_password(username, extension = '', password = '', params = {})
  token = @oauth2client.password.get_token(username, password, {
    extension: extension,
    headers: { 'Authorization' => 'Basic ' + api_key }
  }.merge(params))
  set_token token
  token
end
Also aliased as: authorize, login
authorize_url(opts = {}) click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 95
def authorize_url(opts = {})
  @oauth2client.auth_code.authorize_url(_add_redirect_uri(opts))
end
build_user_agent() click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 228
def build_user_agent
  ua = "ringcentral-sdk-ruby/#{RingCentralSdk::VERSION} %s/%s %s" % [
    (RUBY_ENGINE rescue nil || 'ruby'),
    RUBY_VERSION,
    RUBY_PLATFORM
  ]
  ua.strip
end
create_subscription() click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 237
def create_subscription
  RingCentralSdk::REST::Subscription.new self
end
create_url(url, add_server = false, add_method = nil, add_token = false) click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 59
def create_url(url, add_server = false, add_method = nil, add_token = false)
  built_url = ''
  has_http = !url.index('http://').nil? && !url.index('https://').nil?

  built_url += @config.server_url if add_server && !has_http

  if url.index(URL_PREFIX).nil? && !has_http
    built_url += URL_PREFIX + '/' + API_VERSION + '/'
  end

  if url.index('/') == 0
    if built_url =~ %r{/$}
      built_url += url.gsub(%r{^/+}, '')
    else
      built_url += url
    end
  else # no /
    if built_url =~ %r{/$}
      built_url += url
    else
      built_url += '/' << url
    end
  end

  built_url
end
create_urls(urls, add_server = false, add_method = nil, add_token = false) click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 86
def create_urls(urls, add_server = false, add_method = nil, add_token = false)
  raise(ArgumentError, 'URLs is not an array') unless urls.is_a? Array
  built_urls = []
  urls.each do |url|
    built_urls.push(create_url(url, add_server, add_method, add_token))
  end
  built_urls
end
inflate_request(req_faraday, req_sdk) click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 211
def inflate_request(req_faraday, req_sdk)
  req_faraday.url req_sdk.url
  req_faraday.body = req_sdk.body if req_sdk.body
  if req_sdk.params.is_a? Hash
    req_sdk.params.each { |k, v| req_faraday.params[k] = v }
  end
  if req_sdk.headers.is_a? Hash
    req_sdk.headers.each { |k, v| req_faraday.headers[k] = v }
  end

  ct = req_sdk.content_type
  if !ct.nil? && !ct.to_s.strip.empty?
    req_faraday.headers['Content-Type'] = ct.to_s
  end
  req_faraday
end
init_attributes() click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 50
def init_attributes
  @http = nil
  @user_agent = build_user_agent
end
login(username, extension = '', password = '', params = {})
Alias for: authorize_password
new_oauth2_client() click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 159
def new_oauth2_client
  OAuth2::Client.new(
    @config.app_key,
    @config.app_secret,
    site: @config.server_url,
    authorize_url: @config.authorize_url,
    token_url: TOKEN_ENDPOINT
  )
end
send_request(request_sdk = {}) click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 183
def send_request(request_sdk = {})
  if request_sdk.is_a? Hash
    request_sdk = RingCentralSdk::REST::Request::Simple.new request_sdk
  elsif !request_sdk.is_a? RingCentralSdk::REST::Request::Base
    raise ArgumentError, 'Request is not a RingCentralSdk::REST::Request::Base'
  end

  method = request_sdk.method.to_s.downcase
  method = 'get' if method.empty?

  res = nil

  case method
  when 'delete'
    res = @http.delete { |req| req = inflate_request(req, request_sdk) }
  when 'get'
    res = @http.get { |req| req = inflate_request(req, request_sdk) }
  when 'post'
    res = @http.post { |req| req = inflate_request(req, request_sdk) }
  when 'put'
    res = @http.put { |req| req = inflate_request(req, request_sdk) }
  else
    raise "method [#{method}] not supported"
  end

  res
end
set_oauth2_client(client = nil) click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 169
def set_oauth2_client(client = nil)
  if client.nil?
    @oauth2client = new_oauth2_client
  elsif client.is_a? OAuth2::Client
    @oauth2client = client
  else
    raise ArgumentError, 'client is not an OAuth2::Client'
  end
end
set_token(token) click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 125
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

  @http = Faraday.new(url: api_version_url) do |conn|
    conn.request :oauth2_refresh, token
    conn.request :multipart
    conn.request :url_encoded
    conn.request :json
    conn.headers['User-Agent'] = @user_agent
    if @config.headers.is_a? Hash
      @config.headers.each do |k, v|
        conn.headers[k] = v
      end
    end
    conn.headers['RC-User-Agent'] = @user_agent
    conn.headers['SDK-User-Agent'] = @user_agent
    conn.response :json, content_type: /\bjson$/
    conn.response :logger, @config.logger
    if @config.retry
      conn.use FaradayMiddleware::Request::Retry, @config.retry_options
    end
    conn.adapter Faraday.default_adapter
  end

  token_string = MultiJson.encode token.to_hash
  @config.logger.info("SET_TOKEN: #{token_string}")
end
token() click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 121
def token
  @http ? @http.builder.app.oauth2_token : nil
end

Private Instance Methods

api_version_url() click to toggle source
# File lib/ringcentral_sdk/rest/client.rb, line 55
def api_version_url
  @config.server_url + URL_PREFIX + '/' + API_VERSION
end