swagger_template/api_client.mustache in purecloud-0.35.1 vs swagger_template/api_client.mustache in purecloud-0.36.1

- old
+ new

@@ -1,5 +1,9 @@ +=begin +{{> api_info}} +=end + require 'date' require 'json' require 'logger' require 'tempfile' require 'typhoeus' @@ -15,11 +19,11 @@ # @return [Hash] attr_accessor :default_headers def initialize(config = Configuration.default) @config = config - @user_agent = "PureCloud SDK/Ruby #{VERSION}" + @user_agent = "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/#{VERSION}/ruby{{/httpUserAgent}}" @default_headers = { 'Content-Type' => "application/json", 'User-Agent' => @user_agent } end @@ -73,14 +77,15 @@ :params => query_params, :timeout => @config.timeout, :ssl_verifypeer => @config.verify_ssl, :sslcert => @config.cert_file, :sslkey => @config.key_file, - :cainfo => @config.ssl_ca_cert, :verbose => @config.debugging } + req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert + if [:post, :patch, :put, :delete].include?(http_method) req_body = build_request_body(header_params, form_params, opts[:body]) req_opts.update :body => req_body if @config.debugging @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n" @@ -104,10 +109,13 @@ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]" def deserialize(response, return_type) body = response.body return nil if body.nil? || body.empty? + # return response body directly for String return type + return body if return_type == 'String' + # handle file downloading - save response body into a tmp file and return the File instance return download_file(response) if return_type == 'File' # ensuring a default content type content_type = response.headers['Content-Type'] || 'application/json' @@ -144,11 +152,11 @@ DateTime.parse data when 'Date' # parse date time (expecting ISO 8601 format) Date.parse data when 'Object' - # generic object, return directly + # generic object (usually a Hash), return directly data when /\AArray<(.+)>\z/ # e.g. Array<Pet> sub_type = $1 data.map {|item| convert_to_type(item, sub_type) } @@ -168,29 +176,43 @@ # Save response body into a file in (the defined) temporary folder, using the filename # from the "Content-Disposition" header if provided, otherwise a random filename. # # @see Configuration#temp_folder_path - # @return [File] the file downloaded + # @return [Tempfile] the file downloaded def download_file(response) - tmp_file = Tempfile.new '', @config.temp_folder_path content_disposition = response.headers['Content-Disposition'] if content_disposition filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1] - path = File.join File.dirname(tmp_file), filename + prefix = sanitize_filename(filename) else - path = tmp_file.path + prefix = 'download-' end - # close and delete temp file - tmp_file.close! + prefix = prefix + '-' unless prefix.end_with?('-') - File.open(path, 'w') { |file| file.write(response.body) } - @config.logger.info "File written to #{path}. Please move the file to a proper folder "\ - "for further processing and delete the temp afterwards" - File.new(path) + tempfile = nil + encoding = response.body.encoding + Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file| + file.write(response.body) + tempfile = file + end + @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\ + "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\ + "will be deleted automatically with GC. It's also recommended to delete the temp file "\ + "explicitly with `tempfile.delete`" + tempfile end + # Sanitize filename by removing path. + # e.g. ../../sun.gif becomes sun.gif + # + # @param [String] filename the filename to be sanitized + # @return [String] the sanitized filename + def sanitize_filename(filename) + filename.gsub /.*[\/\\]/, '' + end + def build_request_url(path) # Add leading and trailing slashes to path path = "/#{path}".gsub(/\/+/, '/') URI.encode(@config.base_url + path) end @@ -258,10 +280,10 @@ # Convert object (array, hash, object, etc) to JSON string. # @param [Object] model object to be converted into JSON string # @return [String] JSON string representation of the object def object_to_http_body(model) - return if model.nil? + return model if model.nil? || model.is_a?(String) _body = nil if model.is_a?(Array) _body = model.map{|m| object_to_hash(m) } else _body = object_to_hash(model)