From e773454e0fd825338232d927858f944616eb42fd Mon Sep 17 00:00:00 2001 From: Ricky Smith Date: Tue, 26 Feb 2019 15:39:59 -0500 Subject: [PATCH] Correct bug detecting content type and parsing json responses This is likely a breaking bug fix, since requests will now _correctly_ parse the response body as json and return a hash, instead of returning the body as a string. Fixes #34 --- lib/fog/aliyun/compute.rb | 17 +++++++++++------ lib/fog/aliyun/models/compute/servers.rb | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/fog/aliyun/compute.rb b/lib/fog/aliyun/compute.rb index eefae11..37de741 100644 --- a/lib/fog/aliyun/compute.rb +++ b/lib/fog/aliyun/compute.rb @@ -300,13 +300,18 @@ def reload def request(params) begin - response = @connection.request(params.merge(headers: { + headers = { 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'X-Auth-Token' => @auth_token - }.merge!(params[:headers] || {}), - path: "#{@path}/#{params[:path]}", - query: params[:query])) + }.merge!(params[:headers] || {}) + + request_params = params.merge( + headers: headers, + path: "#{@path}/#{params[:path]}", + query: params[:query]) + + response = @connection.request(request_params) rescue Excon::Errors::HTTPStatusError => error raise case error when Excon::Errors::NotFound @@ -316,7 +321,7 @@ def request(params) end end - response.body = Fog::JSON.decode(response.body) if !response.body.empty? && response.get_header('Content-Type') == 'application/json' + response.body = Fog::JSON.decode(response.body) if !response.body.empty? && response.get_header('Content-Type').start_with?('application/json') response end @@ -339,7 +344,7 @@ def VPCrequest(params) end end - response.body = Fog::JSON.decode(response.body) if !response.body.empty? && response.get_header('Content-Type') == 'application/json' + response.body = Fog::JSON.decode(response.body) if !response.body.empty? && response.get_header('Content-Type').start_with?('application/json') response end diff --git a/lib/fog/aliyun/models/compute/servers.rb b/lib/fog/aliyun/models/compute/servers.rb index 1767792..4864ece 100644 --- a/lib/fog/aliyun/models/compute/servers.rb +++ b/lib/fog/aliyun/models/compute/servers.rb @@ -10,7 +10,7 @@ class Servers < Fog::Collection model Fog::Compute::Aliyun::Server def all(options = {}) - Fog::JSON.decode(service.list_servers(options).body)['Instances']['Instance'] + service.list_servers(options).body['Instances']['Instance'] end # Creates a new server and populates ssh keys