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 =
[ 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)
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.
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
-
#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.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/gooddata/rest/connection.rb', line 161 def initialize(opts) super() @stats = ThreadSafe::Hash.new headers = opts[:headers] || {} @webdav_headers = DEFAULT_WEBDAV_HEADERS.merge(headers) @user = nil @server = nil @opts = opts # Initialize cookies @at_exit_handler_installed = nil end |
Instance Attribute Details
#request_params ⇒ Object (readonly) Also known as:
Returns the value of attribute request_params.
153 154 155 |
# File 'lib/gooddata/rest/connection.rb', line 153 def request_params @request_params end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
157 158 159 |
# File 'lib/gooddata/rest/connection.rb', line 157 def server @server end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
158 159 160 |
# File 'lib/gooddata/rest/connection.rb', line 158 def stats @stats end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
159 160 161 |
# File 'lib/gooddata/rest/connection.rb', line 159 def user @user end |
Class Method Details
.construct_login_payload(username, password) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/gooddata/rest/connection.rb', line 99 def construct_login_payload(username, password) res = { 'postUserLogin' => { 'login' => username, 'password' => password, 'remember' => 1 } } res end |
.generate_string(length = ID_LENGTH) ⇒ String
Generate random string with URL safe base64 encoding
115 116 117 |
# File 'lib/gooddata/rest/connection.rb', line 115 def generate_string(length = ID_LENGTH) SecureRandom.urlsafe_base64(length) end |
.retryable(options = {}, &_block) ⇒ Object
Retry block if exception thrown
120 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 |
# File 'lib/gooddata/rest/connection.rb', line 120 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
179 180 181 182 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 |
# File 'lib/gooddata/rest/connection.rb', line 179 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] ('GDCAuthSST' => [: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
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/gooddata/rest/connection.rb', line 331 def delete(uri, = {}) = log_info() GoodData.rest_logger.info "DELETE: #{@server.url}#{uri}" profile "DELETE #{uri}" do b = proc do params = fresh_request_params([:request_id]) begin @server[uri].delete(params) rescue RestClient::Exception => e # log the error if it happens log_error(e, uri, params, ) raise e end end process_response(, &b) end end |
#disconnect ⇒ Object
Disconnect
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/gooddata/rest/connection.rb', line 227 def disconnect # TODO: Wrap somehow url = @auth['state'] begin clear_session_id delete url if url rescue RestClient::Unauthorized GoodData.logger.info 'Already disconnected' end @auth = nil @server = nil @user = nil end |
#download(what, where, options = {}) ⇒ Object
245 246 247 248 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 305 306 307 308 309 310 |
# File 'lib/gooddata/rest/connection.rb', line 245 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(staging_uri, "#{dir}/").to_s url = URI.join(base_url, CGI.escape(what)).to_s b = proc do raw = { :headers => { :user_agent => GoodData.gem_version_string }, :method => :get, :url => url, :verify_ssl => (@opts[:verify_ssl] == false || @opts[:verify_ssl] == OpenSSL::SSL::VERIFY_NONE) ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER }.merge() if where.is_a?(IO) || where.is_a?(StringIO) 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 where.write chunk end else # Assume it is a string or file File.open(where, 'w') do |f| 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 end end res = nil GoodData::Rest::Connection.retryable(:tries => 2, :refresh_token => proc { refresh_token }) do res = b.call end res end |
#generate_request_id ⇒ Object
502 503 504 |
# File 'lib/gooddata/rest/connection.rb', line 502 def generate_request_id "#{session_id}:#{call_id}" end |
#get(uri, options = {}, &user_block) ⇒ Object
HTTP GET
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/gooddata/rest/connection.rb', line 365 def get(uri, = {}, &user_block) = log_info() GoodData.rest_logger.info "GET: #{@server.url}#{uri}, #{}" profile "GET #{uri}" do b = proc do params = fresh_request_params([:request_id]).merge() begin @server[uri].get(params, &user_block) rescue RestClient::Exception => e # log the error if it happens log_error(e, uri, params, ) raise e end end process_response(, &b) end end |
#log_error(e, uri, params, options = {}) ⇒ Object
Helper for logging error
354 355 356 357 358 359 360 |
# File 'lib/gooddata/rest/connection.rb', line 354 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
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/gooddata/rest/connection.rb', line 408 def post(uri, data = nil, = {}) = log_info() GoodData.rest_logger.info "POST: #{@server.url}#{uri}, #{scrub_params(data, KEYS_TO_SCRUB)}" profile "POST #{uri}" do payload = data.is_a?(Hash) ? data.to_json : data b = proc do params = fresh_request_params([:request_id]) begin @server[uri].post(payload, params) rescue RestClient::Exception => e # log the error if it happens log_error(e, uri, params, ) raise e end end process_response(, &b) end end |
#put(uri, data, options = {}) ⇒ Object
HTTP PUT
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/gooddata/rest/connection.rb', line 386 def put(uri, data, = {}) = log_info() payload = data.is_a?(Hash) ? data.to_json : data GoodData.rest_logger.info "PUT: #{@server.url}#{uri}, #{scrub_params(data, KEYS_TO_SCRUB)}" profile "PUT #{uri}" do b = proc do params = fresh_request_params([:request_id]) begin @server[uri].put(payload, params) rescue RestClient::Exception => e # log the error if it happens log_error(e, uri, params, ) raise e end end process_response(, &b) end end |
#refresh_token(_options = {}) ⇒ Object
312 313 314 315 316 317 318 319 |
# File 'lib/gooddata/rest/connection.rb', line 312 def refresh_token( = {}) begin # rubocop:disable RedundantBegin get TOKEN_PATH, :dont_reauth => true # avoid infinite loop GET fails with 401 rescue Exception => e # rubocop:disable RescueException puts e. raise e end end |
#server_url ⇒ String
Returns server URI
324 325 326 |
# File 'lib/gooddata/rest/connection.rb', line 324 def server_url @server && @server.url end |
#sst_token ⇒ Object
Reader method for SST token
430 431 432 |
# File 'lib/gooddata/rest/connection.rb', line 430 def sst_token request_params[:cookies]['GDCAuthSST'] end |
#stats_table(values = stats) ⇒ Object
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/gooddata/rest/connection.rb', line 434 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
486 487 488 |
# File 'lib/gooddata/rest/connection.rb', line 486 def tt_token request_params[:cookies]['GDCAuthTT'] end |
#upload(file, options = {}) ⇒ Object
Uploads a file to GoodData server
491 492 493 494 495 496 497 498 499 500 |
# File 'lib/gooddata/rest/connection.rb', line 491 def upload(file, = {}) dir = [:directory] || '' staging_uri = [:staging_url].to_s url = dir.empty? ? staging_uri : URI.join(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(url, CGI.escape(webdav_filename)), file end |