@@ -6,7 +6,12 @@ require 'date'
66require 'json'
77require 'logger'
88require 'tempfile'
9+ { {^isFaraday} }
910require 'typhoeus'
11+ { {/isFaraday} }
12+ { {#isFaraday} }
13+ require 'faraday'
14+ { {/isFaraday} }
1015
1116module { {moduleName} }
1217 class ApiClient
@@ -33,94 +38,12 @@ module {{moduleName}}
3338 @@default ||= ApiClient.new
3439 end
3540
36- # Call an API with given options.
37- #
38- # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
39- # the data deserialized from response body (could be nil), response status code and response headers.
40- def call_api(http_method, path, opts = { } )
41- request = build_request(http_method, path, opts)
42- response = request.run
43-
44- if @config.debugging
45- @config.logger.debug "HTTP response body ~BEGIN~\n#{ response.body} \n~END~\n"
46- end
47-
48- unless response.success?
49- if response.timed_out?
50- fail ApiError.new('Connection timed out')
51- elsif response.code == 0
52- # Errors from libcurl will be made visible here
53- fail ApiError.new(:code => 0,
54- :message => response.return_message)
55- else
56- fail ApiError.new(:code => response.code,
57- :response_headers => response.headers,
58- :response_body => response.body),
59- response.status_message
60- end
61- end
62-
63- if opts[:return_type]
64- data = deserialize(response, opts[:return_type])
65- else
66- data = nil
67- end
68- return data, response.code, response.headers
69- end
70-
71- # Builds the HTTP request
72- #
73- # @param [String] http_method HTTP method/verb (e.g. POST)
74- # @param [String] path URL path (e.g. /account/new)
75- # @option opts [Hash] :header_params Header parameters
76- # @option opts [Hash] :query_params Query parameters
77- # @option opts [Hash] :form_params Query parameters
78- # @option opts [Object] :body HTTP body (JSON/XML)
79- # @return [Typhoeus::Request] A Typhoeus Request
80- def build_request(http_method, path, opts = { } )
81- url = build_request_url(path)
82- http_method = http_method.to_sym.downcase
83-
84- header_params = @default_headers.merge(opts[:header_params] || { } )
85- query_params = opts[:query_params] || { }
86- form_params = opts[:form_params] || { }
87-
88- { {#hasAuthMethods} }
89- update_params_for_auth! header_params, query_params, opts[:auth_names]
90- { {/hasAuthMethods} }
91-
92- # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
93- _verify_ssl_host = @config.verify_ssl_host ? 2 : 0
94-
95- req_opts = {
96- :method => http_method,
97- :headers => header_params,
98- :params => query_params,
99- :params_encoding => @config.params_encoding,
100- :timeout => @config.timeout,
101- :ssl_verifypeer => @config.verify_ssl,
102- :ssl_verifyhost => _verify_ssl_host,
103- :sslcert => @config.cert_file,
104- :sslkey => @config.key_file,
105- :verbose => @config.debugging
106- }
107-
108- # set custom cert, if provided
109- req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
110-
111- if [:post, :patch, :put, :delete].include?(http_method)
112- req_body = build_request_body(header_params, form_params, opts[:body])
113- req_opts.update :body => req_body
114- if @config.debugging
115- @config.logger.debug "HTTP request body param ~BEGIN~\n#{ req_body} \n~END~\n"
116- end
117- end
118-
119- request = Typhoeus::Request.new(url, req_opts)
120- download_file(request) if opts[:return_type] == 'File'
121- request
122- end
123-
41+ { {^isFaraday} }
42+ { {> api_client_typhoeus_partial} }
43+ { {/isFaraday} }
44+ { {#isFaraday} }
45+ { {> api_client_faraday_partial} }
46+ { {/isFaraday} }
12447 # Check if the given MIME is a JSON MIME.
12548 # JSON MIME examples:
12649 # application/json
@@ -258,34 +181,6 @@ module {{moduleName}}
258181 @config.base_url + path
259182 end
260183
261- # Builds the HTTP request body
262- #
263- # @param [Hash] header_params Header parameters
264- # @param [Hash] form_params Query parameters
265- # @param [Object] body HTTP body (JSON/XML)
266- # @return [String] HTTP body data in the form of string
267- def build_request_body(header_params, form_params, body)
268- # http form
269- if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
270- header_params['Content-Type'] == 'multipart/form-data'
271- data = { }
272- form_params.each do |key, value|
273- case value
274- when ::File, ::Array, nil
275- # let typhoeus handle File, Array and nil parameters
276- data[key] = value
277- else
278- data[key] = value.to_s
279- end
280- end
281- elsif body
282- data = body.is_a?(String) ? body : body.to_json
283- else
284- data = nil
285- end
286- data
287- end
288-
289184 # Update hearder and query params based on authentication settings.
290185 #
291186 # @param [Hash] header_params Header parameters
0 commit comments