Class: GoodData::Rest::Connection
- Includes:
- MonitorMixin
- Defined in:
- lib/gooddata/rest/connection.rb
Overview
Wrapper of low-level HTTP/REST client/library
Constant Summary collapse
- DEFAULT_URL =
'https://secure.gooddata.com'- LOGIN_PATH =
'/gdc/account/login'- TOKEN_PATH =
'/gdc/account/token'- KEYS_TO_SCRUB =
[:password, :verifyPassword, :authorizationToken]
- ID_LENGTH =
16- DEFAULT_HEADERS =
{ :content_type => :json, :accept => [:json, :zip], :user_agent => GoodData.gem_version_string }
- DEFAULT_WEBDAV_HEADERS =
{ :user_agent => GoodData.gem_version_string }
- DEFAULT_LOGIN_PAYLOAD =
{ :headers => DEFAULT_HEADERS, :verify_ssl => true }
- RETRYABLE_ERRORS =
[ Net::HTTPBadResponse, RestClient::InternalServerError, RestClient::RequestTimeout, RestClient::MethodNotAllowed, SystemCallError, Timeout::Error ]
- RETRIES_ON_TOO_MANY_REQUESTS_ERROR =
12- RETRY_TIME_INITIAL_VALUE =
1- RETRY_TIME_COEFFICIENT =
1.5
Instance Attribute Summary collapse
-
#request_params ⇒ Object
(also: #cookies, #headers)
readonly
Returns the value of attribute request_params.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#verify_ssl ⇒ Object
readonly
Returns the value of attribute verify_ssl.
Class Method Summary collapse
- .construct_login_payload(username, password) ⇒ Object
-
.generate_string(length = ID_LENGTH) ⇒ String
Generate random string with URL safe base64 encoding.
-
.retryable(options = {}, &_block) ⇒ Object
Retry block if exception thrown.
Instance Method Summary collapse
-
#connect(username, password, options = {}) ⇒ Object
Connect using username and password.
-
#delete(uri, options = {}) ⇒ Object
HTTP DELETE.
-
#disconnect ⇒ Object
Disconnect.
- #download(what, where, options = {}) ⇒ Object
- #generate_request_id ⇒ Object
-
#get(uri, options = {}, &user_block) ⇒ Object
HTTP GET.
-
#initialize(opts) ⇒ Connection
constructor
A new instance of Connection.
-
#log_error(e, uri, params, options = {}) ⇒ Object
Helper for logging error.
-
#post(uri, data = nil, options = {}) ⇒ Object
HTTP POST.
-
#put(uri, data, options = {}) ⇒ Object
HTTP PUT.
- #refresh_token(_options = {}) ⇒ Object
- #request(method, uri, data, options = {}, &user_block) ⇒ Object
-
#server_url ⇒ String
Returns server URI.
-
#sst_token ⇒ Object
Reader method for SST token.
- #stats_table(values = stats) ⇒ Object
-
#tt_token ⇒ Object
Reader method for TT token.
-
#upload(file, options = {}) ⇒ Object
Uploads a file to GoodData server.
Constructor Details
#initialize(opts) ⇒ Connection
Returns a new instance of Connection.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/gooddata/rest/connection.rb', line 164 def initialize(opts) super() @stats = ThreadSafe::Hash.new headers = opts[:headers] || {} @webdav_headers = DEFAULT_WEBDAV_HEADERS.merge(headers) @user = nil @server = nil @opts = opts @verify_ssl = @opts[:verify_ssl] == false || @opts[:verify_ssl] == OpenSSL::SSL::VERIFY_NONE ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER # Initialize headers reset_headers! @at_exit_handler_installed = nil end |
Instance Attribute Details
#request_params ⇒ Object (readonly) Also known as: , headers
Returns the value of attribute request_params.
154 155 156 |
# File 'lib/gooddata/rest/connection.rb', line 154 def request_params @request_params end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
159 160 161 |
# File 'lib/gooddata/rest/connection.rb', line 159 def server @server end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
160 161 162 |
# File 'lib/gooddata/rest/connection.rb', line 160 def stats @stats end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
161 162 163 |
# File 'lib/gooddata/rest/connection.rb', line 161 def user @user end |
#verify_ssl ⇒ Object (readonly)
Returns the value of attribute verify_ssl.
162 163 164 |
# File 'lib/gooddata/rest/connection.rb', line 162 def verify_ssl @verify_ssl end |
Class Method Details
.construct_login_payload(username, password) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/gooddata/rest/connection.rb', line 99 def construct_login_payload(username, password) res = { 'postUserLogin' => { 'login' => username, 'password' => password, 'remember' => 1, 'verify_level' => 2 } } res end |
.generate_string(length = ID_LENGTH) ⇒ String
Generate random string with URL safe base64 encoding
116 117 118 |
# File 'lib/gooddata/rest/connection.rb', line 116 def generate_string(length = ID_LENGTH) SecureRandom.urlsafe_base64(length) end |
.retryable(options = {}, &_block) ⇒ Object
Retry block if exception thrown
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/gooddata/rest/connection.rb', line 121 def retryable( = {}, &_block) opts = { :tries => 1, :on => RETRYABLE_ERRORS }.merge() retry_exception = opts[:on] retries = opts[:tries] too_many_requests_tries = RETRIES_ON_TOO_MANY_REQUESTS_ERROR unless retry_exception.is_a?(Array) retry_exception = [retry_exception] end retry_time = RETRY_TIME_INITIAL_VALUE begin return yield rescue RestClient::Unauthorized, RestClient::Forbidden => e # , RestClient::Unauthorized => e raise e unless [:refresh_token] raise e if [:dont_reauth] [:refresh_token].call # (dont_reauth: true) retry if (retries -= 1) > 0 rescue RestClient::TooManyRequests, RestClient::ServiceUnavailable GoodData.logger.warn "Too many requests, retrying in #{retry_time} seconds" sleep retry_time retry_time *= RETRY_TIME_COEFFICIENT # 10 requests with 1.5 coefficent should take ~ 3 mins to finish retry if (too_many_requests_tries -= 1) > 1 rescue *retry_exception => e GoodData.logger.warn e.inspect retry if (retries -= 1) > 1 end yield end |
Instance Method Details
#connect(username, password, options = {}) ⇒ Object
Connect using username and password
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/gooddata/rest/connection.rb', line 183 def connect(username, password, = {}) server = [:server] || Helpers::AuthHelper.read_server = DEFAULT_LOGIN_PAYLOAD.merge() headers = [:headers] || {} = .merge(headers) @server = RestClient::Resource.new server, # Install at_exit handler first unless @at_exit_handler_installed begin at_exit { disconnect if @user } rescue RestClient::Unauthorized GoodData.logger.info 'Already logged out' ensure @at_exit_handler_installed = true end end # Reset old cookies first if [:sst_token] merge_headers!(:x_gdc_authsst => [:sst_token]) get('/gdc/account/token', @request_params) @user = get(get('/gdc/app/account/bootstrap')['bootstrapResource']['accountSetting']['links']['self']) GoodData.logger.info("Connected using SST to server #{@server.url} to profile \"#{@user['accountSetting']['login']}\"") @auth = {} refresh_token :dont_reauth => true else GoodData.logger.info("Connected using username \"#{username}\" to server #{@server.url}") credentials = Connection.construct_login_payload(username, password) generate_session_id @auth = post(LOGIN_PATH, credentials, :dont_reauth => true)['userLogin'] refresh_token :dont_reauth => true @user = get(@auth['profile']) end GoodData.logger.info('Connection successful') rescue RestClient::Unauthorized => e GoodData.logger.info('Bad Login or Password') GoodData.logger.info('Connection failed') raise e rescue RestClient::Forbidden => e GoodData.logger.info('Connection failed') raise e end |
#delete(uri, options = {}) ⇒ Object
HTTP DELETE
328 329 330 |
# File 'lib/gooddata/rest/connection.rb', line 328 def delete(uri, = {}) request(:delete, uri, nil, ) end |
#disconnect ⇒ Object
Disconnect
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/gooddata/rest/connection.rb', line 231 def disconnect # TODO: Wrap somehow url = @auth['state'] begin clear_session_id delete(url, :x_gdc_authsst => sst_token) if url rescue RestClient::Unauthorized GoodData.logger.info 'Already disconnected' end @auth = nil @server = nil @user = nil reset_headers! end |
#download(what, where, options = {}) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/gooddata/rest/connection.rb', line 249 def download(what, where, = {}) # handle the path (directory) given in what ilast_slash = what.rindex('/') if ilast_slash.nil? what_dir = '' else # take the directory from the path what_dir = what[0..ilast_slash - 1] # take the filename from the path what = what[ilast_slash + 1..-1] end option_dir = [:directory] || '' option_dir = option_dir[0..-2] if option_dir[-1] == '/' # join the otion dir with the what_dir # [option dir empty, what dir empty] => the joined dir dir_hash = { [true, true] => '', [true, false] => what_dir, [false, true] => option_dir, [false, false] => "#{what_dir}/#{option_dir}" } dir = dir_hash[[option_dir.empty?, what_dir.empty?]] staging_uri = [:staging_url].to_s base_url = dir.empty? ? staging_uri : URI.join("#{server}", staging_uri, "#{dir}/").to_s url = URI.join("#{server}", base_url, CGI.escape(what)).to_s b = proc do |f| raw = { :headers => @webdav_headers.merge(:x_gdc_authtt => headers[:x_gdc_authtt]), :method => :get, :url => url, :verify_ssl => verify_ssl } RestClient::Request.execute(raw) do |chunk, _x, response| if response.code.to_s != '200' fail ArgumentError, "Error downloading #{url}. Got response: #{response.code} #{response} #{response.body}" end f.write chunk end end GoodData::Rest::Connection.retryable(:tries => Helpers::GD_MAX_RETRY, :refresh_token => proc { refresh_token }) do if where.is_a?(IO) || where.is_a?(StringIO) b.call(where) else # Assume it is a string or file File.open(where, 'w') do |f| b.call(f) end end end end |
#generate_request_id ⇒ Object
471 472 473 |
# File 'lib/gooddata/rest/connection.rb', line 471 def generate_request_id "#{session_id}:#{call_id}" end |
#get(uri, options = {}, &user_block) ⇒ Object
HTTP GET
377 378 379 |
# File 'lib/gooddata/rest/connection.rb', line 377 def get(uri, = {}, &user_block) request(:get, uri, nil, , &user_block) end |
#log_error(e, uri, params, options = {}) ⇒ Object
Helper for logging error
337 338 339 340 341 342 343 |
# File 'lib/gooddata/rest/connection.rb', line 337 def log_error(e, uri, params, = {}) return if e.response && e.response.code == 401 && !uri.include?('token') && !uri.include?('login') if [:do_not_log].nil? || [:do_not_log].index(e.class).nil? GoodData.logger.error(format_error(e, params)) end end |
#post(uri, data = nil, options = {}) ⇒ Object
HTTP POST
391 392 393 |
# File 'lib/gooddata/rest/connection.rb', line 391 def post(uri, data = nil, = {}) request(:post, uri, data, ) end |
#put(uri, data, options = {}) ⇒ Object
HTTP PUT
384 385 386 |
# File 'lib/gooddata/rest/connection.rb', line 384 def put(uri, data, = {}) request(:put, uri, data, ) end |
#refresh_token(_options = {}) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/gooddata/rest/connection.rb', line 306 def refresh_token( = {}) begin # rubocop:disable RedundantBegin # avoid infinite loop GET fails with 401 response = get(TOKEN_PATH, :x_gdc_authsst => sst_token, :dont_reauth => true) # Remove when TT sent in headers. Currently we need to parse from body merge_headers!(:x_gdc_authtt => GoodData::Helpers.get_path(response, %w(userToken token))) rescue Exception => e # rubocop:disable RescueException puts e. raise e end end |
#request(method, uri, data, options = {}, &user_block) ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/gooddata/rest/connection.rb', line 345 def request(method, uri, data, = {}, &user_block) request_id = [:request_id] || generate_request_id log_info(.merge(request_id: request_id)) payload = data.is_a?(Hash) ? data.to_json : data GoodData.rest_logger.info "#{method.to_s.upcase}: #{@server.url}#{uri}, #{scrub_params(data, KEYS_TO_SCRUB)}" profile "#{method.to_s.upcase} #{uri}" do b = proc do params = fresh_request_params(request_id).merge() begin case method when :get @server[uri].get(params, &user_block) when :put @server[uri].put(payload, params) when :delete @server[uri].delete(params) when :post @server[uri].post(payload, params) end rescue RestClient::Exception => e log_error(e, uri, params, ) raise e end end process_response(, &b) end end |
#server_url ⇒ String
Returns server URI
321 322 323 |
# File 'lib/gooddata/rest/connection.rb', line 321 def server_url @server && @server.url end |
#sst_token ⇒ Object
Reader method for SST token
398 399 400 |
# File 'lib/gooddata/rest/connection.rb', line 398 def sst_token request_params[:x_gdc_authsst] end |
#stats_table(values = stats) ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/gooddata/rest/connection.rb', line 402 def stats_table(values = stats) sorted = values.sort_by { |_k, v| v[:avg] } Terminal::Table.new :headings => %w(title avg min max total calls) do |t| overall = { :avg => 0, :calls => 0, :total => 0 } sorted.each do |l| avg = l[1][:avg] min = l[1][:min] max = l[1][:max] total = l[1][:total] calls = l[1][:calls] row = [ l[0], sprintf('%.3f', avg), sprintf('%.3f', min), sprintf('%.3f', max), sprintf('%.3f', total), calls ] overall[:min] = min if overall[:min].nil? || min < overall[:min] overall[:max] = max if overall[:max].nil? || max > overall[:max] overall[:total] += total overall[:calls] += calls overall[:avg] += avg t.add_row row end overall[:avg] = overall[:avg] / sorted.length row = [ 'TOTAL', sprintf('%.3f', overall[:avg]), sprintf('%.3f', overall[:min]), sprintf('%.3f', overall[:max]), sprintf('%.3f', overall[:total]), overall[:calls] ] t.add_row :separator t.add_row row end end |
#tt_token ⇒ Object
Reader method for TT token
454 455 456 |
# File 'lib/gooddata/rest/connection.rb', line 454 def tt_token request_params[:x_gdc_authtt] end |
#upload(file, options = {}) ⇒ Object
Uploads a file to GoodData server
460 461 462 463 464 465 466 467 468 469 |
# File 'lib/gooddata/rest/connection.rb', line 460 def upload(file, = {}) dir = [:directory] || '' staging_uri = [:staging_url].to_s url = dir.empty? ? staging_uri : URI.join("#{server}", staging_uri, "#{dir}/").to_s # Make a directory, if needed create_webdav_dir_if_needed url unless dir.empty? webdav_filename = [:filename] || File.basename(file) do_stream_file URI.join("#{server}", url, CGI.escape(webdav_filename)), file end |