lib/vra/client.rb in vmware-vra-1.5.4 vs lib/vra/client.rb in vmware-vra-1.6.0

- old
+ new

@@ -15,12 +15,12 @@ # See the License for the specific language governing permissions and # limitations under the License. # require 'ffi_yajl' -require 'rest-client' require 'passwordmasker' +require 'vra/http' module Vra # rubocop:disable ClassLength class Client attr_accessor :page_size @@ -91,71 +91,68 @@ def authorized? return false if @bearer_token.value.nil? response = http_head("/identity/api/tokens/#{@bearer_token.value}", :skip_auth) - if response.code == 204 - true - else - false - end + response.success_no_content? end def generate_bearer_token @bearer_token.value = nil validate_client_options! - response = http_post('/identity/api/tokens', FFI_Yajl::Encoder.encode(bearer_token_request_body), :skip_auth) - if response.code != 200 + response = http_post('/identity/api/tokens', + FFI_Yajl::Encoder.encode(bearer_token_request_body), + :skip_auth) + unless response.success_ok? raise Vra::Exception::Unauthorized, "Unable to get bearer token: #{response.body}" end @bearer_token.value = FFI_Yajl::Parser.parse(response.body)['id'] end def full_url(path) "#{@base_url}#{path}" end - def http_head(path, skip_auth=nil) + def http_fetch(method, path, skip_auth=nil) authorize! unless skip_auth - response = RestClient::Request.execute(method: :head, - url: full_url(path), - headers: request_headers, - verify_ssl: @verify_ssl) + response = Vra::Http.execute(method: method, + url: full_url(path), + headers: request_headers, + verify_ssl: @verify_ssl) rescue => e raise_http_exception(e, path) else response end - def http_get(path, skip_auth=nil) - authorize! unless skip_auth + def http_head(path, skip_auth=nil) + http_fetch(:head, path, skip_auth) + end - response = RestClient::Request.execute(method: :get, - url: full_url(path), - headers: request_headers, - verify_ssl: @verify_ssl) - rescue => e - raise_http_exception(e, path) - else - response + def http_get(path, skip_auth=nil) + http_fetch(:get, path, skip_auth) end def http_get!(path) response = http_get(path) response.body end + def get_parsed(path) + FFI_Yajl::Parser.parse(http_get!(path)) + end + def http_get_paginated_array!(path) items = [] page = 1 base_path = path + "?limit=#{page_size}" loop do - response = FFI_Yajl::Parser.parse(http_get!("#{base_path}&page=#{page}")) + response = get_parsed("#{base_path}&page=#{page}") items += response['content'] break if page >= response['metadata']['totalPages'] page += 1 end @@ -170,14 +167,14 @@ end def http_post(path, payload, skip_auth=nil) authorize! unless skip_auth - response = RestClient::Request.execute(method: :post, - url: full_url(path), - headers: request_headers, - payload: payload, - verify_ssl: @verify_ssl) + response = Vra::Http.execute(method: :post, + url: full_url(path), + headers: request_headers, + payload: payload, + verify_ssl: @verify_ssl) rescue => e raise_http_exception(e, path) else response end