66The version of the OpenAPI document: 0.1.0
77
88Generated by: https://openapi-generator.tech
9- OpenAPI Generator version: 5.4.0
9+ OpenAPI Generator version: 7.0.1
1010
1111=end
1212
1616require 'tempfile'
1717require 'time'
1818require 'faraday'
19+ require 'faraday/multipart' if Gem ::Version . new ( Faraday ::VERSION ) >= Gem ::Version . new ( '2.0' )
1920
2021module MxPlatformRuby
2122 class ApiClient
@@ -47,47 +48,31 @@ def self.default
4748 # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
4849 # the data deserialized from response body (could be nil), response status code and response headers.
4950 def call_api ( http_method , path , opts = { } )
50- ssl_options = {
51- :ca_file => @config . ssl_ca_file ,
52- :verify => @config . ssl_verify ,
53- :verify_mode => @config . ssl_verify_mode ,
54- :client_cert => @config . ssl_client_cert ,
55- :client_key => @config . ssl_client_key
56- }
57-
58- connection = Faraday . new ( :url => config . base_url , :ssl => ssl_options ) do |conn |
59- conn . request ( :basic_auth , config . username , config . password )
60- @config . configure_middleware ( conn )
61- if opts [ :header_params ] [ "Content-Type" ] == "multipart/form-data"
62- conn . request :multipart
63- conn . request :url_encoded
64- end
65- conn . adapter ( Faraday . default_adapter )
66- end
67-
6851 begin
69- response = connection . public_send ( http_method . to_sym . downcase ) do |req |
52+ response = connection ( opts ) . public_send ( http_method . to_sym . downcase ) do |req |
7053 build_request ( http_method , path , req , opts )
7154 end
7255
73- if @ config. debugging
74- @ config. logger . debug "HTTP response body ~BEGIN~\n #{ response . body } \n ~END~\n "
56+ if config . debugging
57+ config . logger . debug "HTTP response body ~BEGIN~\n #{ response . body } \n ~END~\n "
7558 end
7659
7760 unless response . success?
78- if response . status == 0
61+ if response . status == 0 && response . respond_to? ( :return_message )
7962 # Errors from libcurl will be made visible here
80- fail ApiError . new ( : code => 0 ,
81- : message => response . return_message )
63+ fail ApiError . new ( code : 0 ,
64+ message : response . return_message )
8265 else
83- fail ApiError . new ( : code => response . status ,
84- : response_headers => response . headers ,
85- : response_body => response . body ) ,
66+ fail ApiError . new ( code : response . status ,
67+ response_headers : response . headers ,
68+ response_body : response . body ) ,
8669 response . reason_phrase
8770 end
8871 end
8972 rescue Faraday ::TimeoutError
9073 fail ApiError . new ( 'Connection timed out' )
74+ rescue Faraday ::ConnectionFailed
75+ fail ApiError . new ( 'Connection failed' )
9176 end
9277
9378 if opts [ :return_type ]
@@ -106,7 +91,7 @@ def call_api(http_method, path, opts = {})
10691 # @option opts [Hash] :query_params Query parameters
10792 # @option opts [Hash] :form_params Query parameters
10893 # @option opts [Object] :body HTTP body (JSON/XML)
109- # @return [Typhoeus ::Request] A Typhoeus Request
94+ # @return [Faraday ::Request] A Faraday Request
11095 def build_request ( http_method , path , request , opts = { } )
11196 url = build_request_url ( path , opts )
11297 http_method = http_method . to_sym . downcase
@@ -117,24 +102,22 @@ def build_request(http_method, path, request, opts = {})
117102
118103 update_params_for_auth! header_params , query_params , opts [ :auth_names ]
119104
120- req_opts = {
121- :params_encoding => @config . params_encoding ,
122- :timeout => @config . timeout ,
123- :verbose => @config . debugging
124- }
125-
126105 if [ :post , :patch , :put , :delete ] . include? ( http_method )
127106 req_body = build_request_body ( header_params , form_params , opts [ :body ] )
128- if @ config. debugging
129- @ config. logger . debug "HTTP request body param ~BEGIN~\n #{ req_body } \n ~END~\n "
107+ if config . debugging
108+ config . logger . debug "HTTP request body param ~BEGIN~\n #{ req_body } \n ~END~\n "
130109 end
131110 end
132111 request . headers = header_params
133112 request . body = req_body
134- request . options = OpenStruct . new ( req_opts )
113+
114+ # Overload default options only if provided
115+ request . options . params_encoder = config . params_encoder if config . params_encoder
116+ request . options . timeout = config . timeout if config . timeout
117+
135118 request . url url
136119 request . params = query_params
137- download_file ( request ) if opts [ :return_type ] == 'File'
120+ download_file ( request ) if opts [ :return_type ] == 'File' || opts [ :return_type ] == 'Binary'
138121 request
139122 end
140123
@@ -154,7 +137,7 @@ def build_request_body(header_params, form_params, body)
154137 case value
155138 when ::File , ::Tempfile
156139 # TODO hardcode to application/octet-stream, need better way to detect content type
157- data [ key ] = Faraday ::UploadIO . new ( value . path , 'application/octet-stream' , value . path )
140+ data [ key ] = Faraday ::FilePart . new ( value . path , 'application/octet-stream' , value . path )
158141 when ::Array , nil
159142 # let Faraday handle Array and nil parameters
160143 data [ key ] = value
@@ -179,6 +162,51 @@ def download_file(request)
179162 end
180163 end
181164
165+ def connection ( opts )
166+ opts [ :header_params ] [ 'Content-Type' ] == 'multipart/form-data' ? connection_multipart : connection_regular
167+ end
168+
169+ def connection_multipart
170+ @connection_multipart ||= build_connection do |conn |
171+ conn . request :multipart
172+ conn . request :url_encoded
173+ end
174+ end
175+
176+ def connection_regular
177+ @connection_regular ||= build_connection
178+ end
179+
180+ def build_connection
181+ Faraday . new ( url : config . base_url , ssl : ssl_options , proxy : config . proxy ) do |conn |
182+ basic_auth ( conn )
183+ config . configure_middleware ( conn )
184+ yield ( conn ) if block_given?
185+ conn . adapter ( Faraday . default_adapter )
186+ config . configure_connection ( conn )
187+ end
188+ end
189+
190+ def ssl_options
191+ {
192+ ca_file : config . ssl_ca_file ,
193+ verify : config . ssl_verify ,
194+ verify_mode : config . ssl_verify_mode ,
195+ client_cert : config . ssl_client_cert ,
196+ client_key : config . ssl_client_key
197+ }
198+ end
199+
200+ def basic_auth ( conn )
201+ if config . username && config . password
202+ if Gem ::Version . new ( Faraday ::VERSION ) >= Gem ::Version . new ( '2.0' )
203+ conn . request ( :authorization , :basic , config . username , config . password )
204+ else
205+ conn . request ( :basic_auth , config . username , config . password )
206+ end
207+ end
208+ end
209+
182210 # Check if the given MIME is a JSON MIME.
183211 # JSON MIME examples:
184212 # application/json
@@ -201,23 +229,30 @@ def deserialize(response, return_type)
201229 # handle file downloading - return the File instance processed in request callbacks
202230 # note that response body is empty when the file is written in chunks in request on_body callback
203231 if return_type == 'File'
204- content_disposition = response . headers [ 'Content-Disposition' ]
205- if content_disposition && content_disposition =~ /filename=/i
206- filename = content_disposition [ /filename=['"]?([^'" \s ]+)['"]?/ , 1 ]
207- prefix = sanitize_filename ( filename )
232+ if @config . return_binary_data == true
233+ # return byte stream
234+ encoding = body . encoding
235+ return @stream . join . force_encoding ( encoding )
208236 else
209- prefix = 'download-'
237+ # return file instead of binary data
238+ content_disposition = response . headers [ 'Content-Disposition' ]
239+ if content_disposition && content_disposition =~ /filename=/i
240+ filename = content_disposition [ /filename=['"]?([^'"\s ]+)['"]?/ , 1 ]
241+ prefix = sanitize_filename ( filename )
242+ else
243+ prefix = 'download-'
244+ end
245+ prefix = prefix + '-' unless prefix . end_with? ( '-' )
246+ encoding = body . encoding
247+ @tempfile = Tempfile . open ( prefix , @config . temp_folder_path , encoding : encoding )
248+ @tempfile . write ( @stream . join . force_encoding ( encoding ) )
249+ @tempfile . close
250+ @config . logger . info "Temp file written to #{ @tempfile . path } , please copy the file to a proper folder " \
251+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file " \
252+ "will be deleted automatically with GC. It's also recommended to delete the temp file " \
253+ "explicitly with `tempfile.delete`"
254+ return @tempfile
210255 end
211- prefix = prefix + '-' unless prefix . end_with? ( '-' )
212- encoding = body . encoding
213- @tempfile = Tempfile . open ( prefix , @config . temp_folder_path , encoding : encoding )
214- @tempfile . write ( @stream . join . force_encoding ( encoding ) )
215- @tempfile . close
216- @config . logger . info "Temp file written to #{ @tempfile . path } , please copy the file to a proper folder " \
217- "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file " \
218- "will be deleted automatically with GC. It's also recommended to delete the temp file " \
219- "explicitly with `tempfile.delete`"
220- return @tempfile
221256 end
222257
223258 return nil if body . nil? || body . empty?
0 commit comments