lib/gooddata/rest/client.rb in gooddata-0.6.11 vs lib/gooddata/rest/client.rb in gooddata-0.6.12

- old
+ new

@@ -2,11 +2,11 @@ require 'rest-client' require_relative '../helpers/auth_helpers' -require_relative 'connections/connections' +require_relative 'connection' require_relative 'object_factory' require_relative '../mixins/inspector' module GoodData @@ -17,11 +17,11 @@ # SHOULD provide way to use - HEAD, Bulk GET ... class Client ################################# # Constants ################################# - DEFAULT_CONNECTION_IMPLEMENTATION = Connections::RestClientConnection + DEFAULT_CONNECTION_IMPLEMENTATION = GoodData::Rest::Connection ################################# # Class variables ################################# @@instance = nil # rubocop:disable ClassVars @@ -58,10 +58,15 @@ # # @param username [String] Username to be used for authentication # @param password [String] Password to be used for authentication # @return [GoodData::Rest::Client] Client def connect(username, password, opts = { :verify_ssl => true }) + if username.nil? && password.nil? + username = ENV['GD_GEM_USER'] + password = ENV['GD_GEM_PASSWORD'] + end + new_opts = opts.dup if username.is_a?(Hash) && username.key?(:sst_token) new_opts[:sst_token] = username[:sst_token] elsif username.is_a? Hash new_opts[:username] = username[:login] || username[:user] || username[:username] @@ -81,11 +86,11 @@ end client = Client.new(new_opts) if client at_exit do - # puts client.connection.stats_table if client && client.connection + puts client.connection.stats_table if client && client.connection && (GoodData.stats_on? || client.stats_on?) end end # HACK: This line assigns class instance # if not done yet @@instance = client # rubocop:disable ClassVars @@ -102,22 +107,12 @@ def connection @@instance # rubocop:disable ClassVars end # Retry block if exception thrown - def retryable(options = {}, &_block) - opts = { :tries => 1, :on => Exception }.merge(options) - - retry_exception, retries = opts[:on], opts[:tries] - - begin - return yield - rescue retry_exception - retry if (retries -= 1) > 0 - end - - yield + def retryable(options = {}, &block) + GoodData::Rest::Connection.retryable(options, &block) end alias_method :client, :connection end @@ -143,11 +138,11 @@ # Create factory bound to previously created connection @factory = ObjectFactory.new(self) end def create_project(options = { title: 'Project', auth_token: ENV['GD_PROJECT_TOKEN'] }) - GoodData::Project.create(options.merge(client: self)) + GoodData::Project.create({ client: self }.merge(options)) end def create_project_from_blueprint(blueprint, options = {}) GoodData::Model::ProjectCreator.migrate(spec: blueprint, token: options[:auth_token], client: self) end @@ -190,14 +185,30 @@ def resource(res_name) puts "Getting resource '#{res_name}'" nil end - def user - create(GoodData::Profile, @connection.user) + def user(id = nil) + if id + create(GoodData::Profile, get(id)) + else + create(GoodData::Profile, @connection.user) + end end + def stats_off + @stats = false + end + + def stats_on + @stats = true + end + + def stats_on? # rubocop:disable Style/TrivialAccessors + @stats + end + ####################### # Rest ####################### # HTTP DELETE # @@ -251,11 +262,11 @@ sleep_interval = options[:sleep_interval] || DEFAULT_SLEEP_INTERVAL response = get(link, :process => false) while response.code == code sleep sleep_interval - GoodData::Rest::Client.retryable(:tries => 3, :on => RestClient::InternalServerError) do + GoodData::Rest::Client.retryable(:tries => 3, :refresh_token => proc { connection.refresh_token }) do sleep sleep_interval response = get(link, :process => false) end end if options[:process] == false @@ -277,11 +288,11 @@ def poll_on_response(link, options = {}, &bl) sleep_interval = options[:sleep_interval] || DEFAULT_SLEEP_INTERVAL response = get(link) while bl.call(response) sleep sleep_interval - GoodData::Rest::Client.retryable(:tries => 3, :on => RestClient::InternalServerError) do + GoodData::Rest::Client.retryable(:tries => 3, :refresh_token => proc { connection.refresh_token }) do sleep sleep_interval response = get(link) end end response @@ -316,10 +327,10 @@ # @param options [Hash] must contain :staging_url key (file will be downloaded from :staging_url + source_relative_path) def download(source_relative_path, target_file_path, options = {}) @connection.download source_relative_path, target_file_path, options end - def download_from_user_webdav(source_relative_path, target_file_path, options = {}) + def download_from_user_webdav(source_relative_path, target_file_path, options = { :client => GoodData.client, :project => project }) download(source_relative_path, target_file_path, options.merge( :directory => options[:directory], :staging_url => get_user_webdav_url(options) )) end