lib/ringcentral_sdk/rest/client.rb in ringcentral_sdk-1.1.1 vs lib/ringcentral_sdk/rest/client.rb in ringcentral_sdk-1.2.0

- old
+ new

@@ -4,44 +4,48 @@ require 'faraday_middleware/oauth2_refresh' require 'oauth2' module RingCentralSdk::REST class Client - ACCESS_TOKEN_TTL = 600 # 10 minutes REFRESH_TOKEN_TTL = 36000 # 10 hours REFRESH_TOKEN_TTL_REMEMBER = 604800 # 1 week ACCOUNT_PREFIX = '/account/' ACCOUNT_ID = '~' AUTHZ_ENDPOINT = '/restapi/oauth/authorize' TOKEN_ENDPOINT = '/restapi/oauth/token' REVOKE_ENDPOINT = '/restapi/oauth/revoke' API_VERSION = 'v1.0' URL_PREFIX = '/restapi' + 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 + def initialize(app_key='', app_secret='', server_url=RingCentralSdk::RC_SERVER_SANDBOX, opts={}) init_attributes() - app_config = RingCentralSdk::REST::ConfigApp.new(app_key, app_secret, server_url, opts) - app_config(app_config) + self.app_config = RingCentralSdk::REST::ConfigApp.new( + app_key, app_secret, server_url, opts) if opts.key?(:username) && opts.key?(:password) extension = opts.key?(:extension) ? opts[:extension] : '' authorize_password(opts[:username], extension, opts[:password]) end - @messages = RingCentralSdk::REST::Messages.new(self) + @instance_headers = opts[:headers] || {} + + @messages = RingCentralSdk::REST::Messages.new self end - def app_config(app_config) - @app_config = app_config + def app_config=(new_app_config) + @app_config = new_app_config @oauth2client = new_oauth2_client() end def init_attributes() @token = nil @@ -91,57 +95,64 @@ built_urls.push(create_url(url, add_server, add_method, add_token)) end return built_urls end - def authorize_url(opts={}) + def authorize_url(opts = {}) @oauth2client.auth_code.authorize_url(_add_redirect_uri(opts)) end - def authorize_code(code, opts={}) + def authorize_code(code, opts = {}) token = @oauth2client.auth_code.get_token(code, _add_redirect_uri(opts)) set_token(token) return token end - def _add_redirect_uri(opts={}) + def _add_redirect_uri(opts = {}) if !opts.key?(:redirect_uri) && @app_config.redirect_url.to_s.length > 0 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 = '', remember = false) token = @oauth2client.password.get_token(username, password, { extension: extension, - headers: { 'Authorization' => 'Basic ' + get_api_key() } }) + headers: {'Authorization' => 'Basic ' + get_api_key()}}) set_token(token) return token end - def authorize_user(user, remember=false) + def authorize_user(user, remember = false) authorize_password(user.username, user.extension, user.password) end def set_token(token) - if token.is_a?(Hash) + if token.is_a? Hash token = OAuth2::AccessToken::from_hash(@oauth2client, token) end - unless token.is_a?(OAuth2::AccessToken) + 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| + @http = Faraday.new(url: api_version_url()) do |conn| conn.request :oauth2_refresh, @token conn.request :json conn.request :url_encoded conn.headers['User-Agent'] = @user_agent - conn.headers['Rc-User-Agent'] = @user_agent - conn.response :json, :content_type => /\bjson$/ + if @instance_headers.is_a? Hash + @instance_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 conn.adapter Faraday.default_adapter end end def new_oauth2_client() @@ -152,32 +163,34 @@ end def set_oauth2_client(client=nil) if client.nil? @oauth2client = new_oauth2_client() - elsif client.is_a?(OAuth2::Client) + elsif client.is_a? OAuth2::Client @oauth2client = client else fail "client is not an OAuth2::Client" end end def get_api_key() api_key = (@app_config.key.is_a?(String) && @app_config.secret.is_a?(String)) \ - ? Base64.encode64("#{@app_config.key}:#{@app_config.secret}").gsub(/[\s\t\r\n]/,'') : '' + ? Base64.encode64("#{@app_config.key}:#{@app_config.secret}").gsub(/\s/,'') : '' return api_key end 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 fail 'Request is not a RingCentralSdk::REST::Request::Base' end + method = request_sdk.method.to_s.downcase + method = 'get' if method.empty? + res = nil - method = request_sdk.method.downcase case method when 'delete' res = @http.delete { |req| req = inflate_request(req, request_sdk) } when 'get' @@ -185,10 +198,10 @@ 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 - fail "#{method} not supported" + fail "method [#{method}] not supported" end return res end def inflate_request(req_faraday, req_sdk)