templates/api_client.mustache in dropbox-sign-1.4.1 vs templates/api_client.mustache in dropbox-sign-1.5.0
- old
+ new
@@ -5,16 +5,22 @@
require 'date'
require 'json'
require 'logger'
require 'tempfile'
require 'time'
-{{^isFaraday}}
+{{#isTyphoeus}}
require 'typhoeus'
-{{/isFaraday}}
+{{/isTyphoeus}}
{{#isFaraday}}
require 'faraday'
+require 'faraday/multipart' if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
+require 'marcel'
{{/isFaraday}}
+{{#isHttpx}}
+require 'httpx'
+require 'net/http/status'
+{{/isHttpx}}
module Dropbox
end
module {{moduleName}}
@@ -40,66 +46,41 @@
def self.default
@@default ||= ApiClient.new
end
-{{^isFaraday}}
+{{#isTyphoeus}}
{{> api_client_typhoeus_partial}}
-{{/isFaraday}}
+{{/isTyphoeus}}
{{#isFaraday}}
{{> api_client_faraday_partial}}
{{/isFaraday}}
+{{#isHttpx}}
+{{> api_client_httpx_partial}}
+{{/isHttpx}}
# Check if the given MIME is a JSON MIME.
# JSON MIME examples:
# application/json
# application/json; charset=UTF8
# APPLICATION/JSON
# */*
# @param [String] mime MIME
# @return [Boolean] True if the MIME is application/json
def json_mime?(mime)
- (mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
+ (mime == '*/*') || !(mime =~ /^Application\/.*json(?!p)(;.*)?/i).nil?
end
# Deserialize the response to the given return type.
#
# @param [Response] response HTTP response
# @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>"
def deserialize(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
- {{^isFaraday}}
- return @tempfile if return_type == 'File'
- {{/isFaraday}}
- {{#isFaraday}}
- if return_type == 'File'
- content_disposition = response.headers['Content-Disposition']
- if content_disposition && content_disposition =~ /filename=/i
- filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
- prefix = sanitize_filename(filename)
- else
- prefix = 'download-'
- end
- prefix = prefix + '-' unless prefix.end_with?('-')
- encoding = body.encoding
- @tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
- @tempfile.write(@stream.join.force_encoding(encoding))
- @tempfile.close
- @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`"
- return @tempfile
- end
- {{/isFaraday}}
-
return nil if body.nil? || body.empty?
# return response body directly for String return type
- return body if return_type == 'String'
+ return body.to_s 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)
@@ -162,10 +143,10 @@
# 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(/.*[\/\\]/, '')
+ filename.split(/[\/\\]/).last
end
def build_request_url(path, opts = {})
# Add leading and trailing slashes to path
path = "/#{path}".gsub(/\/+/, '/')