lib/cfoundry/v2/base.rb in cfoundry-0.3.46 vs lib/cfoundry/v2/base.rb in cfoundry-0.3.47

- old
+ new

@@ -33,11 +33,11 @@ end # Cloud metadata def info - get("info", nil => :json) + get("info", :accept => :json) end [ :app, :organization, :space, :user, :runtime, :framework, :service, :domain, :route, :service_plan, :service_binding, :service_instance, @@ -49,69 +49,73 @@ depth, _ = args depth ||= 1 params = { :"inline-relations-depth" => depth } - get("v2", plural, guid, nil => :json, :params => params) + get("v2", plural, guid, :accept => :json, :params => params) end define_method(:"create_#{obj}") do |payload| - post(payload, "v2", plural, :json => :json) + post(payload, "v2", plural, :content => :json, :accept => :json) end define_method(:"delete_#{obj}") do |guid| delete("v2", plural, guid, nil => nil) true end define_method(:"update_#{obj}") do |guid, payload| - put(payload, "v2", plural, guid, :json => :json) + put(payload, "v2", plural, guid, :content => :json, :accept => :json) end define_method(plural) do |*args| + params = params_from(args) + all_pages( - get("v2", plural, nil => :json, :params => params_from(args))) + params, + get("v2", plural, :accept => :json, :params => params)) end - end def resource_match(fingerprints) - put(fingerprints, "v2", "resource_match", :json => :json) + put(fingerprints, "v2", "resource_match", + :content => :json, :accept => :json) end def upload_app(guid, zipfile, resources = []) payload = { :resources => MultiJson.dump(resources), - :multipart => true, :application => - if zipfile.is_a? File - zipfile - elsif zipfile.is_a? String - File.new(zipfile, "rb") - end + UploadIO.new( + if zipfile.is_a? File + zipfile + elsif zipfile.is_a? String + File.new(zipfile, "rb") + end, + "application/zip") } put(payload, "v2", "apps", guid, "bits") - rescue RestClient::ServerBrokeConnection + rescue EOFError retry end def files(guid, instance, *path) get("v2", "apps", guid, "instances", instance, "files", *path) end alias :file :files def instances(guid) - get("v2", "apps", guid, "instances", nil => :json) + get("v2", "apps", guid, "instances", :accept => :json) end def crashes(guid) - get("v2", "apps", guid, "crashes", nil => :json) + get("v2", "apps", guid, "crashes", :accept => :json) end def stats(guid) - get("v2", "apps", guid, "stats", nil => :json) + get("v2", "apps", guid, "stats", :accept => :json) end def params_from(args) depth, query = args @@ -124,62 +128,62 @@ end params end - def all_pages(paginated) + def all_pages(params, paginated) payload = paginated[:resources] while next_page = paginated[:next_url] - paginated = request_path(:get, next_page, nil => :json) + paginated = request_path( + Net::HTTP::Get, next_page, :accept => :json, :params => params) + payload += paginated[:resources] end payload end private def handle_response(response, accept) - json = accept == :json - - case response.code - when 200, 201, 204, 301, 302, 307 + case response + when Net::HTTPSuccess, Net::HTTPRedirection if accept == :headers - return response.headers + return sane_headers(response) end - if json - if response.code == 204 + if accept == :json + if response.is_a?(Net::HTTPNoContent) raise "Expected JSON response, got 204 No Content" end - parse_json(response) + parse_json(response.body) else - response + response.body end - when 400 - info = parse_json(response) + when Net::HTTPBadRequest + info = parse_json(response.body) raise CFoundry::APIError.new(info[:code], info[:description]) - when 401, 403 - info = parse_json(response) + when Net::HTTPUnauthorized, Net::HTTPForbidden + info = parse_json(response.body) raise CFoundry::Denied.new(info[:code], info[:description]) - when 404 + when Net::HTTPNotFound raise CFoundry::NotFound - when 411, 500, 504 + when Net::HTTPServerError begin - info = parse_json(response) + info = parse_json(response.body) raise CFoundry::APIError.new(info[:code], info[:description]) rescue MultiJson::DecodeError - raise CFoundry::BadResponse.new(response.code, response) + raise CFoundry::BadResponse.new(response.code, response.body) end else - raise CFoundry::BadResponse.new(response.code, response) + raise CFoundry::BadResponse.new(response.code, response.body) end end def log_line(io, data) io.printf(