Method: DatadogAPIClient::APIClient#deserialize

Defined in:
lib/datadog_api_client/api_client.rb

#deserialize(api_version, response, return_type) ⇒ Object

Deserialize the response to the given return type.

Parameters:

  • the (String)

    api version

  • response (Response)

    HTTP response

  • return_type (String)

    some examples: “User”, “Array<User>”, “Hash<String, Integer>”



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
311
312
313
# File 'lib/datadog_api_client/api_client.rb', line 279

def deserialize(api_version, response, return_type)
  body = response.body

  # handle file downloading - return the File instance processed in request callbacks
  # note that response body is empty when the file is written in chunks in request on_body callback
  return @tempfile if return_type == 'File'

  return nil if body.nil? || body.empty?

  if response.headers['Content-Encoding'].eql?('gzip') then
    gzip = Zlib::Inflate.new(Zlib::MAX_WBITS + 16)
    body = gzip.inflate(body)
    gzip.close
  end

  # return response body directly for String return type
  return body if return_type == 'String'

  # ensuring a default content type
  content_type = response.headers['Content-Type'] || 'application/json'

  fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)

  begin
    data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
  rescue JSON::ParserError => e
    if %w(String Date Time).include?(return_type)
      data = body
    else
      raise e
    end
  end

  convert_to_type data, return_type, api_version
end