lib/ringcentral_sdk/rest/client.rb in ringcentral_sdk-1.0.0 vs lib/ringcentral_sdk/rest/client.rb in ringcentral_sdk-1.1.0
- old
+ new
@@ -110,12 +110,12 @@
return opts
end
def authorize_password(username, extension='', password='', remember=false)
token = @oauth2client.password.get_token(username, password, {
- :extension => extension,
- :headers => { 'Authorization' => 'Basic ' + get_api_key() } })
+ extension: extension,
+ headers: { 'Authorization' => 'Basic ' + get_api_key() } })
set_token(token)
return token
end
def authorize_user(user, remember=false)
@@ -132,25 +132,25 @@
end
@token = token
@http = Faraday.new(:url => api_version_url()) do |conn|
- conn.request :oauth2_refresh, @token
- conn.request :json
- conn.request :url_encoded
+ 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$/
- conn.adapter Faraday.default_adapter
+ conn.adapter Faraday.default_adapter
end
end
def new_oauth2_client()
return OAuth2::Client.new(@app_config.key, @app_config.secret,
- :site => @app_config.server_url,
- :authorize_url => AUTHZ_ENDPOINT,
- :token_url => TOKEN_ENDPOINT)
+ site: @app_config.server_url,
+ authorize_url: AUTHZ_ENDPOINT,
+ token_url: TOKEN_ENDPOINT)
end
def set_oauth2_client(client=nil)
if client.nil?
@oauth2client = new_oauth2_client()
@@ -165,22 +165,47 @@
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]/,'') : ''
return api_key
end
- def send_request(request=nil)
- unless request.is_a?(RingCentralSdk::REST::Request::Base)
+ def send_request(request_sdk = nil)
+ unless request_sdk.is_a?(RingCentralSdk::REST::Request::Base)
fail 'Request is not a RingCentralSdk::REST::Request::Base'
end
- if request.method.downcase == 'post'
- resp = @http.post do |req|
- req.url request.url
- req.headers['Content-Type'] = request.content_type if request.content_type
- req.body = request.body if request.body
- end
- return resp
+ res = nil
+
+ method = request_sdk.method.downcase
+ 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
+ fail "#{method} not support"
end
+ return res
+ end
+
+ 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.length > 0
+ req_faraday.headers['Content-Type'] = ct.to_s
+ end
+ return req_faraday
end
def get_user_agent()
ua = "ringcentral-sdk-ruby/#{RingCentralSdk::VERSION} %s/%s %s" % [
(RUBY_ENGINE rescue nil or "ruby"),