Method: RubyBox::Session#request

Defined in:
lib/ruby-box/session.rb

#request(uri, request, raw = false, retries = 0) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruby-box/session.rb', line 56

def request(uri, request, raw=false, retries=0)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.ssl_version = :SSLv3
  
  if @access_token
    request.add_field('Authorization', "Bearer #{@access_token.token}")
  else
    request.add_field('Authorization', build_auth_header)
  end

  response = http.request(request)

  if response.is_a? Net::HTTPNotFound
    raise RubyBox::ObjectNotFound
  end

  # Got unauthorized (401) status, try to refresh the token
  if response.code.to_i == 401 and @refresh_token and retries == 0
    refresh_token(@refresh_token)
    request(uri, request, raw, retries + 1)
  end

  sleep(@backoff) # try not to excessively hammer API.

  handle_errors( response.code.to_i, response.body, raw )
end