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

- old
+ new

@@ -33,10 +33,11 @@ # Decide if we need provide direct access to connection attr_reader :connection # TODO: Decide if we need provide direct access to factory attr_reader :factory + attr_reader :opts include Mixin::Inspector inspector :object_id ################################# @@ -56,11 +57,11 @@ # client = GoodData.connect('jon.smith@goodddata.com', 's3cr3tp4sw0rd') # # @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 = {}) + def connect(username, password, opts = { :verify_ssl => true }) 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] @@ -90,20 +91,35 @@ @@instance = client # rubocop:disable ClassVars client end def disconnect - if @@instance # rubocop:disable ClassVars + if @@instance # rubocop:disable ClassVars, Style/GuardClause @@instance.disconnect # rubocop:disable ClassVars @@instance = nil # rubocop:disable ClassVars end end 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 + end + alias_method :client, :connection end # Constructor of client # @param opts [Hash] Client options @@ -126,12 +142,12 @@ # Create factory bound to previously created connection @factory = ObjectFactory.new(self) end - def create_project(options = {}) - GoodData::Project.create(title: 'Project for schedule testing', auth_token: ConnectionHelper::GD_PROJECT_TOKEN, client: self) + def create_project(options = { title: 'Project', auth_token: ENV['GD_PROJECT_TOKEN'] }) + GoodData::Project.create(options.merge(client: self)) end def create_project_from_blueprint(blueprint, options = {}) GoodData::Model::ProjectCreator.migrate(spec: blueprint, token: options[:auth_token], client: self) end @@ -191,12 +207,12 @@ end # HTTP GET # # @param uri [String] Target URI - def get(uri, opts = {}) - @connection.get uri, opts + def get(uri, opts = {}, & block) + @connection.get uri, opts, & block end # FIXME: Invstigate _file argument def get_project_webdav_path(_file, opts = { :project => GoodData.project }) p = opts[:project] @@ -235,11 +251,11 @@ sleep_interval = options[:sleep_interval] || DEFAULT_SLEEP_INTERVAL response = get(link, :process => false) while response.code == code sleep sleep_interval - retryable(:tries => 3, :on => RestClient::InternalServerError) do + GoodData::Rest::Client.retryable(:tries => 3, :on => RestClient::InternalServerError) do sleep sleep_interval response = get(link, :process => false) end end if options[:process] == false @@ -261,11 +277,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 - retryable(:tries => 3, :on => RestClient::InternalServerError) do + GoodData::Rest::Client.retryable(:tries => 3, :on => RestClient::InternalServerError) do sleep sleep_interval response = get(link) end end response @@ -283,46 +299,63 @@ # @param uri [String] Target URI def post(uri, data, opts = {}) @connection.post uri, data, opts end - # Retry blok if exception thrown - def retryable(options = {}, &block) - opts = { :tries => 1, :on => Exception }.merge(options) + # Uploads file to staging + # + # @param file [String] file to be uploaded + # @param options [Hash] must contain :staging_url key (file will be uploaded to :staging_url + File.basename(file)) + def upload(file, options = {}) + @connection.upload file, options + end - retry_exception, retries = opts[:on], opts[:tries] + # Downloads file from staging + # + # @param source_relative_path [String] path relative to @param options[:staging_url] + # @param target_file_path [String] path to be downloaded to + # @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 - begin - return yield - rescue retry_exception - retry if (retries -= 1) > 0 - end + def download_from_user_webdav(source_relative_path, target_file_path, options = {}) + download(source_relative_path, target_file_path, options.merge( + :directory => options[:directory], + :staging_url => get_user_webdav_url(options) + )) + end - yield + def upload_to_user_webdav(file, options = {}) + upload(file, options.merge( + :directory => options[:directory], + :staging_url => get_user_webdav_url(options) + )) end - # Uploads file - def upload(file, options = {}) - @connection.upload file, options + def with_project(pid, &block) + GoodData.with_project(pid, client: self, &block) end - def upload_to_user_webdav(file, options = {}) + ###################### PRIVATE ###################### + + private + + def get_user_webdav_url(options = {}) p = options[:project] fail ArgumentError, 'No :project specified' if p.nil? - project = GoodData::Project[p, options] + project = options[:project] || GoodData::Project[p, options] fail ArgumentError, 'Wrong :project specified' if project.nil? u = URI(project.links['uploads']) - url = URI.join(u.to_s.chomp(u.path.to_s), '/uploads/') - upload(file, options.merge( - :directory => options[:directory], - :staging_url => url - )) - end + us = u.to_s + ws = options[:client].opts[:webdav_server] + if !us.empty? && !us.downcase.start_with?('http') && !ws.empty? + u = URI.join(ws, us) + end - def with_project(pid, &block) - GoodData.with_project(pid, client: self, &block) + URI.join(u.to_s.chomp(u.path.to_s), '/uploads/') end end end end