ruby-linstor-client 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +9 -0
  3. data/Gemfile.lock +86 -0
  4. data/LICENSE +21 -0
  5. data/README.md +21 -0
  6. data/Rakefile +10 -0
  7. data/codegen.sh +22 -0
  8. data/generator/custom/one_of_drbd_resource_definition_layer.rb +7 -0
  9. data/generator/custom/one_of_drbd_volume_definition.rb +7 -0
  10. data/generator/custom/one_of_drbd_volume_luks_volume_storage_nvme_volme_writecache_volume.rb +11 -0
  11. data/generator/openapi-generator.yaml +11 -0
  12. data/generator/template/Gemfile.mustache +9 -0
  13. data/generator/template/README.mustache +148 -0
  14. data/generator/template/Rakefile.mustache +10 -0
  15. data/generator/template/api.mustache +209 -0
  16. data/generator/template/api_client.mustache +264 -0
  17. data/generator/template/api_client_faraday_partial.mustache +136 -0
  18. data/generator/template/api_client_spec.mustache +220 -0
  19. data/generator/template/api_client_typhoeus_partial.mustache +153 -0
  20. data/generator/template/api_doc.mustache +118 -0
  21. data/generator/template/api_error.mustache +49 -0
  22. data/generator/template/api_info.mustache +12 -0
  23. data/generator/template/api_test.mustache +47 -0
  24. data/generator/template/base_object.mustache +120 -0
  25. data/generator/template/configuration.mustache +385 -0
  26. data/generator/template/configuration_spec.mustache +34 -0
  27. data/generator/template/configuration_tls_faraday_partial.mustache +29 -0
  28. data/generator/template/configuration_tls_typhoeus_partial.mustache +34 -0
  29. data/generator/template/gem.mustache +53 -0
  30. data/generator/template/gemspec.mustache +35 -0
  31. data/generator/template/git_push.sh.mustache +57 -0
  32. data/generator/template/gitignore.mustache +39 -0
  33. data/generator/template/model.mustache +26 -0
  34. data/generator/template/model_doc.mustache +12 -0
  35. data/generator/template/model_test.mustache +77 -0
  36. data/generator/template/partial_model_enum_class.mustache +20 -0
  37. data/generator/template/partial_model_generic.mustache +371 -0
  38. data/generator/template/partial_model_generic_doc.mustache +28 -0
  39. data/generator/template/partial_oneof_module.mustache +137 -0
  40. data/generator/template/partial_oneof_module_doc.mustache +92 -0
  41. data/generator/template/rspec.mustache +2 -0
  42. data/generator/template/rubocop.mustache +148 -0
  43. data/generator/template/spec_helper.mustache +103 -0
  44. data/generator/template/travis.mustache +14 -0
  45. data/generator/template/version.mustache +7 -0
  46. data/lib/ruby-linstor-client/api/developers_api.rb +5856 -0
  47. data/lib/ruby-linstor-client/api_client.rb +393 -0
  48. data/lib/ruby-linstor-client/api_error.rb +57 -0
  49. data/lib/ruby-linstor-client/configuration.rb +298 -0
  50. data/lib/ruby-linstor-client/models/api_call_rc.rb +293 -0
  51. data/lib/ruby-linstor-client/models/auto_place_request.rb +245 -0
  52. data/lib/ruby-linstor-client/models/auto_select_filter.rb +305 -0
  53. data/lib/ruby-linstor-client/models/candidate.rb +248 -0
  54. data/lib/ruby-linstor-client/models/controller_props_modify.rb +243 -0
  55. data/lib/ruby-linstor-client/models/controller_version.rb +245 -0
  56. data/lib/ruby-linstor-client/models/drbd_proxy_enable.rb +219 -0
  57. data/lib/ruby-linstor-client/models/drbd_proxy_modify.rb +288 -0
  58. data/lib/ruby-linstor-client/models/drbd_resource.rb +276 -0
  59. data/lib/ruby-linstor-client/models/drbd_resource_definition_layer.rb +283 -0
  60. data/lib/ruby-linstor-client/models/drbd_volume.rb +285 -0
  61. data/lib/ruby-linstor-client/models/drbd_volume_definition.rb +236 -0
  62. data/lib/ruby-linstor-client/models/error_report.rb +252 -0
  63. data/lib/ruby-linstor-client/models/key_value_store.rb +231 -0
  64. data/lib/ruby-linstor-client/models/key_value_store_modify.rb +243 -0
  65. data/lib/ruby-linstor-client/models/layer_type.rb +40 -0
  66. data/lib/ruby-linstor-client/models/luks_resource.rb +220 -0
  67. data/lib/ruby-linstor-client/models/luks_volume.rb +275 -0
  68. data/lib/ruby-linstor-client/models/max_volume_sizes.rb +229 -0
  69. data/lib/ruby-linstor-client/models/net_interface.rb +309 -0
  70. data/lib/ruby-linstor-client/models/node.rb +381 -0
  71. data/lib/ruby-linstor-client/models/node_modify.rb +252 -0
  72. data/lib/ruby-linstor-client/models/nvme_resource.rb +220 -0
  73. data/lib/ruby-linstor-client/models/nvme_volume.rb +266 -0
  74. data/lib/ruby-linstor-client/models/one_of_drbd_resource_definition_layer.rb +7 -0
  75. data/lib/ruby-linstor-client/models/one_of_drbd_volume_definition.rb +7 -0
  76. data/lib/ruby-linstor-client/models/one_of_drbd_volume_luks_volume_storage_nvme_volme_writecache_volume.rb +11 -0
  77. data/lib/ruby-linstor-client/models/pass_phrase_create.rb +227 -0
  78. data/lib/ruby-linstor-client/models/physical_storage.rb +238 -0
  79. data/lib/ruby-linstor-client/models/physical_storage_create.rb +337 -0
  80. data/lib/ruby-linstor-client/models/physical_storage_device.rb +245 -0
  81. data/lib/ruby-linstor-client/models/physical_storage_storage_pool_create.rb +232 -0
  82. data/lib/ruby-linstor-client/models/provider_kind.rb +43 -0
  83. data/lib/ruby-linstor-client/models/resource.rb +278 -0
  84. data/lib/ruby-linstor-client/models/resource_connection.rb +261 -0
  85. data/lib/ruby-linstor-client/models/resource_connection_modify.rb +243 -0
  86. data/lib/ruby-linstor-client/models/resource_create.rb +238 -0
  87. data/lib/ruby-linstor-client/models/resource_definition.rb +295 -0
  88. data/lib/ruby-linstor-client/models/resource_definition_create.rb +262 -0
  89. data/lib/ruby-linstor-client/models/resource_definition_layer.rb +228 -0
  90. data/lib/ruby-linstor-client/models/resource_definition_modify.rb +274 -0
  91. data/lib/ruby-linstor-client/models/resource_group.rb +262 -0
  92. data/lib/ruby-linstor-client/models/resource_group_modify.rb +261 -0
  93. data/lib/ruby-linstor-client/models/resource_group_spawn.rb +255 -0
  94. data/lib/ruby-linstor-client/models/resource_layer.rb +283 -0
  95. data/lib/ruby-linstor-client/models/resource_modify.rb +243 -0
  96. data/lib/ruby-linstor-client/models/resource_state.rb +218 -0
  97. data/lib/ruby-linstor-client/models/resource_with_volumes.rb +297 -0
  98. data/lib/ruby-linstor-client/models/resource_with_volumes_all_of.rb +220 -0
  99. data/lib/ruby-linstor-client/models/snapshot.rb +282 -0
  100. data/lib/ruby-linstor-client/models/snapshot_restore.rb +236 -0
  101. data/lib/ruby-linstor-client/models/snapshot_volume_definition.rb +228 -0
  102. data/lib/ruby-linstor-client/models/storage_pool.rb +333 -0
  103. data/lib/ruby-linstor-client/models/storage_pool_definition.rb +230 -0
  104. data/lib/ruby-linstor-client/models/storage_pool_definition_modify.rb +243 -0
  105. data/lib/ruby-linstor-client/models/storage_resource.rb +220 -0
  106. data/lib/ruby-linstor-client/models/storage_volume.rb +256 -0
  107. data/lib/ruby-linstor-client/models/volume.rb +327 -0
  108. data/lib/ruby-linstor-client/models/volume_definition.rb +277 -0
  109. data/lib/ruby-linstor-client/models/volume_definition_create.rb +232 -0
  110. data/lib/ruby-linstor-client/models/volume_definition_layer.rb +233 -0
  111. data/lib/ruby-linstor-client/models/volume_definition_modify.rb +264 -0
  112. data/lib/ruby-linstor-client/models/volume_group.rb +251 -0
  113. data/lib/ruby-linstor-client/models/volume_group_modify.rb +255 -0
  114. data/lib/ruby-linstor-client/models/volume_layer.rb +228 -0
  115. data/lib/ruby-linstor-client/models/volume_modify.rb +243 -0
  116. data/lib/ruby-linstor-client/models/volume_state.rb +218 -0
  117. data/lib/ruby-linstor-client/models/writecache_resource.rb +220 -0
  118. data/lib/ruby-linstor-client/models/writecache_volume.rb +266 -0
  119. data/lib/ruby-linstor-client/version.rb +15 -0
  120. data/lib/ruby-linstor-client.rb +109 -0
  121. data/pkg/ruby-linstor-client-1.0.0.gem +0 -0
  122. data/ruby-linstor-client.gemspec +39 -0
  123. data/spec/api/developers_api_spec.rb +1116 -0
  124. data/spec/api_client_spec.rb +188 -0
  125. data/spec/configuration_spec.rb +42 -0
  126. data/spec/models/api_call_rc_spec.rb +70 -0
  127. data/spec/models/auto_place_request_spec.rb +46 -0
  128. data/spec/models/auto_select_filter_spec.rb +82 -0
  129. data/spec/models/candidate_spec.rb +52 -0
  130. data/spec/models/controller_props_modify_spec.rb +46 -0
  131. data/spec/models/controller_version_spec.rb +52 -0
  132. data/spec/models/drbd_proxy_enable_spec.rb +34 -0
  133. data/spec/models/drbd_proxy_modify_spec.rb +56 -0
  134. data/spec/models/drbd_resource_definition_layer_spec.rb +76 -0
  135. data/spec/models/drbd_resource_spec.rb +70 -0
  136. data/spec/models/drbd_volume_definition_spec.rb +46 -0
  137. data/spec/models/drbd_volume_spec.rb +76 -0
  138. data/spec/models/error_report_spec.rb +52 -0
  139. data/spec/models/key_value_store_modify_spec.rb +46 -0
  140. data/spec/models/key_value_store_spec.rb +40 -0
  141. data/spec/models/layer_type_spec.rb +28 -0
  142. data/spec/models/luks_resource_spec.rb +34 -0
  143. data/spec/models/luks_volume_spec.rb +70 -0
  144. data/spec/models/max_volume_sizes_spec.rb +40 -0
  145. data/spec/models/net_interface_spec.rb +68 -0
  146. data/spec/models/node_modify_spec.rb +52 -0
  147. data/spec/models/node_spec.rb +102 -0
  148. data/spec/models/nvme_resource_spec.rb +34 -0
  149. data/spec/models/nvme_volume_spec.rb +64 -0
  150. data/spec/models/pass_phrase_create_spec.rb +40 -0
  151. data/spec/models/physical_storage_create_spec.rb +80 -0
  152. data/spec/models/physical_storage_device_spec.rb +52 -0
  153. data/spec/models/physical_storage_spec.rb +46 -0
  154. data/spec/models/physical_storage_storage_pool_create_spec.rb +40 -0
  155. data/spec/models/provider_kind_spec.rb +28 -0
  156. data/spec/models/resource_connection_modify_spec.rb +46 -0
  157. data/spec/models/resource_connection_spec.rb +58 -0
  158. data/spec/models/resource_create_spec.rb +46 -0
  159. data/spec/models/resource_definition_create_spec.rb +58 -0
  160. data/spec/models/resource_definition_layer_spec.rb +40 -0
  161. data/spec/models/resource_definition_modify_spec.rb +64 -0
  162. data/spec/models/resource_definition_spec.rb +76 -0
  163. data/spec/models/resource_group_modify_spec.rb +58 -0
  164. data/spec/models/resource_group_spawn_spec.rb +52 -0
  165. data/spec/models/resource_group_spec.rb +58 -0
  166. data/spec/models/resource_layer_spec.rb +76 -0
  167. data/spec/models/resource_modify_spec.rb +46 -0
  168. data/spec/models/resource_spec.rb +70 -0
  169. data/spec/models/resource_state_spec.rb +34 -0
  170. data/spec/models/resource_with_volumes_all_of_spec.rb +34 -0
  171. data/spec/models/resource_with_volumes_spec.rb +76 -0
  172. data/spec/models/snapshot_restore_spec.rb +40 -0
  173. data/spec/models/snapshot_spec.rb +70 -0
  174. data/spec/models/snapshot_volume_definition_spec.rb +40 -0
  175. data/spec/models/storage_pool_definition_modify_spec.rb +46 -0
  176. data/spec/models/storage_pool_definition_spec.rb +40 -0
  177. data/spec/models/storage_pool_spec.rb +94 -0
  178. data/spec/models/storage_resource_spec.rb +34 -0
  179. data/spec/models/storage_volume_spec.rb +58 -0
  180. data/spec/models/volume_definition_create_spec.rb +40 -0
  181. data/spec/models/volume_definition_layer_spec.rb +40 -0
  182. data/spec/models/volume_definition_modify_spec.rb +58 -0
  183. data/spec/models/volume_definition_spec.rb +64 -0
  184. data/spec/models/volume_group_modify_spec.rb +52 -0
  185. data/spec/models/volume_group_spec.rb +52 -0
  186. data/spec/models/volume_layer_spec.rb +40 -0
  187. data/spec/models/volume_modify_spec.rb +46 -0
  188. data/spec/models/volume_spec.rb +100 -0
  189. data/spec/models/volume_state_spec.rb +34 -0
  190. data/spec/models/writecache_resource_spec.rb +34 -0
  191. data/spec/models/writecache_volume_spec.rb +64 -0
  192. data/spec/spec_helper.rb +111 -0
  193. metadata +344 -0
@@ -0,0 +1,264 @@
1
+ =begin
2
+ {{> api_info}}
3
+ =end
4
+
5
+ require 'date'
6
+ require 'json'
7
+ require 'logger'
8
+ require 'tempfile'
9
+ require 'time'
10
+ {{^isFaraday}}
11
+ require 'typhoeus'
12
+ {{/isFaraday}}
13
+ {{#isFaraday}}
14
+ require 'faraday'
15
+ {{/isFaraday}}
16
+
17
+ module {{moduleName}}
18
+ class ApiClient
19
+ # The Configuration object holding settings to be used in the API client.
20
+ attr_accessor :config
21
+
22
+ # Defines the headers to be used in HTTP requests of all API calls by default.
23
+ #
24
+ # @return [Hash]
25
+ attr_accessor :default_headers
26
+
27
+ # Initializes the ApiClient
28
+ # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
29
+ def initialize(config = Configuration.default)
30
+ @config = config
31
+ @user_agent = "{{{httpUserAgent}}}{{^httpUserAgent}}OpenAPI-Generator/#{VERSION}/ruby{{/httpUserAgent}}"
32
+ @default_headers = {
33
+ 'Content-Type' => 'application/json',
34
+ 'User-Agent' => @user_agent
35
+ }
36
+ end
37
+
38
+ def self.default
39
+ @@default ||= ApiClient.new
40
+ end
41
+
42
+ {{^isFaraday}}
43
+ {{> api_client_typhoeus_partial}}
44
+ {{/isFaraday}}
45
+ {{#isFaraday}}
46
+ {{> api_client_faraday_partial}}
47
+ {{/isFaraday}}
48
+ # Check if the given MIME is a JSON MIME.
49
+ # JSON MIME examples:
50
+ # application/json
51
+ # application/json; charset=UTF8
52
+ # APPLICATION/JSON
53
+ # */*
54
+ # @param [String] mime MIME
55
+ # @return [Boolean] True if the MIME is application/json
56
+ def json_mime?(mime)
57
+ (mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
58
+ end
59
+
60
+ # Deserialize the response to the given return type.
61
+ #
62
+ # @param [Response] response HTTP response
63
+ # @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>"
64
+ def deserialize(response, return_type)
65
+ body = response.body
66
+
67
+ # handle file downloading - return the File instance processed in request callbacks
68
+ # note that response body is empty when the file is written in chunks in request on_body callback
69
+ {{^isFaraday}}
70
+ return @tempfile if return_type == 'File'
71
+ {{/isFaraday}}
72
+ {{#isFaraday}}
73
+ if return_type == 'File'
74
+ content_disposition = response.headers['Content-Disposition']
75
+ if content_disposition && content_disposition =~ /filename=/i
76
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
77
+ prefix = sanitize_filename(filename)
78
+ else
79
+ prefix = 'download-'
80
+ end
81
+ prefix = prefix + '-' unless prefix.end_with?('-')
82
+ encoding = body.encoding
83
+ @tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
84
+ @tempfile.write(@stream.join.force_encoding(encoding))
85
+ @tempfile.close
86
+ @config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\
87
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
88
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
89
+ "explicitly with `tempfile.delete`"
90
+ return @tempfile
91
+ end
92
+ {{/isFaraday}}
93
+
94
+ return nil if body.nil? || body.empty?
95
+
96
+ # return response body directly for String return type
97
+ return body if return_type == 'String'
98
+
99
+ # ensuring a default content type
100
+ content_type = response.headers['Content-Type'] || 'application/json'
101
+
102
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
103
+
104
+ begin
105
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
106
+ rescue JSON::ParserError => e
107
+ if %w(String Date Time).include?(return_type)
108
+ data = body
109
+ else
110
+ raise e
111
+ end
112
+ end
113
+
114
+ convert_to_type data, return_type
115
+ end
116
+
117
+ # Convert data to the given return type.
118
+ # @param [Object] data Data to be converted
119
+ # @param [String] return_type Return type
120
+ # @return [Mixed] Data in a particular type
121
+ def convert_to_type(data, return_type)
122
+ return nil if data.nil?
123
+ case return_type
124
+ when 'String'
125
+ data.to_s
126
+ when 'Integer'
127
+ data.to_i
128
+ when 'Float'
129
+ data.to_f
130
+ when 'Boolean'
131
+ data == true
132
+ when 'Time'
133
+ # parse date time (expecting ISO 8601 format)
134
+ Time.parse data
135
+ when 'Date'
136
+ # parse date time (expecting ISO 8601 format)
137
+ Date.parse data
138
+ when 'Object'
139
+ # generic object (usually a Hash), return directly
140
+ data
141
+ when /\AArray<(.+)>\z/
142
+ # e.g. Array<Pet>
143
+ sub_type = $1
144
+ data.map { |item| convert_to_type(item, sub_type) }
145
+ when /\AHash\<String, (.+)\>\z/
146
+ # e.g. Hash<String, Integer>
147
+ sub_type = $1
148
+ {}.tap do |hash|
149
+ data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
150
+ end
151
+ else
152
+ # models (e.g. Pet) or oneOf
153
+ klass = {{moduleName}}.const_get(return_type)
154
+ klass.respond_to?(:openapi_one_of) ? klass.build(data) : klass.build_from_hash(data)
155
+ end
156
+ end
157
+
158
+ # Sanitize filename by removing path.
159
+ # e.g. ../../sun.gif becomes sun.gif
160
+ #
161
+ # @param [String] filename the filename to be sanitized
162
+ # @return [String] the sanitized filename
163
+ def sanitize_filename(filename)
164
+ filename.gsub(/.*[\/\\]/, '')
165
+ end
166
+
167
+ def build_request_url(path, opts = {})
168
+ # Add leading and trailing slashes to path
169
+ path = "/#{path}".gsub(/\/+/, '/')
170
+ @config.base_url(opts[:operation]) + path
171
+ end
172
+
173
+ # Update header and query params based on authentication settings.
174
+ #
175
+ # @param [Hash] header_params Header parameters
176
+ # @param [Hash] query_params Query parameters
177
+ # @param [String] auth_names Authentication scheme name
178
+ def update_params_for_auth!(header_params, query_params, auth_names)
179
+ Array(auth_names).each do |auth_name|
180
+ auth_setting = @config.auth_settings[auth_name]
181
+ next unless auth_setting
182
+ case auth_setting[:in]
183
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
184
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
185
+ else fail ArgumentError, 'Authentication token must be in `query` or `header`'
186
+ end
187
+ end
188
+ end
189
+
190
+ # Sets user agent in HTTP header
191
+ #
192
+ # @param [String] user_agent User agent (e.g. openapi-generator/ruby/1.0.0)
193
+ def user_agent=(user_agent)
194
+ @user_agent = user_agent
195
+ @default_headers['User-Agent'] = @user_agent
196
+ end
197
+
198
+ # Return Accept header based on an array of accepts provided.
199
+ # @param [Array] accepts array for Accept
200
+ # @return [String] the Accept header (e.g. application/json)
201
+ def select_header_accept(accepts)
202
+ return nil if accepts.nil? || accepts.empty?
203
+ # use JSON when present, otherwise use all of the provided
204
+ json_accept = accepts.find { |s| json_mime?(s) }
205
+ json_accept || accepts.join(',')
206
+ end
207
+
208
+ # Return Content-Type header based on an array of content types provided.
209
+ # @param [Array] content_types array for Content-Type
210
+ # @return [String] the Content-Type header (e.g. application/json)
211
+ def select_header_content_type(content_types)
212
+ # return nil by default
213
+ return if content_types.nil? || content_types.empty?
214
+ # use JSON when present, otherwise use the first one
215
+ json_content_type = content_types.find { |s| json_mime?(s) }
216
+ json_content_type || content_types.first
217
+ end
218
+
219
+ # Convert object (array, hash, object, etc) to JSON string.
220
+ # @param [Object] model object to be converted into JSON string
221
+ # @return [String] JSON string representation of the object
222
+ def object_to_http_body(model)
223
+ return model if model.nil? || model.is_a?(String)
224
+ local_body = nil
225
+ if model.is_a?(Array)
226
+ local_body = model.map { |m| object_to_hash(m) }
227
+ else
228
+ local_body = object_to_hash(model)
229
+ end
230
+ local_body.to_json
231
+ end
232
+
233
+ # Convert object(non-array) to hash.
234
+ # @param [Object] obj object to be converted into JSON string
235
+ # @return [String] JSON string representation of the object
236
+ def object_to_hash(obj)
237
+ if obj.respond_to?(:to_hash)
238
+ obj.to_hash
239
+ else
240
+ obj
241
+ end
242
+ end
243
+
244
+ # Build parameter value according to the given collection format.
245
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
246
+ def build_collection_param(param, collection_format)
247
+ case collection_format
248
+ when :csv
249
+ param.join(',')
250
+ when :ssv
251
+ param.join(' ')
252
+ when :tsv
253
+ param.join("\t")
254
+ when :pipes
255
+ param.join('|')
256
+ when :multi
257
+ # return the array directly as typhoeus will handle it as expected
258
+ param
259
+ else
260
+ fail "unknown collection format: #{collection_format.inspect}"
261
+ end
262
+ end
263
+ end
264
+ end
@@ -0,0 +1,136 @@
1
+ # Call an API with given options.
2
+ #
3
+ # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
4
+ # the data deserialized from response body (could be nil), response status code and response headers.
5
+ def call_api(http_method, path, opts = {})
6
+ ssl_options = {
7
+ :ca_file => @config.ssl_ca_file,
8
+ :verify => @config.ssl_verify,
9
+ :verify_mode => @config.ssl_verify_mode,
10
+ :client_cert => @config.ssl_client_cert,
11
+ :client_key => @config.ssl_client_key
12
+ }
13
+
14
+ connection = Faraday.new(:url => config.base_url, :ssl => ssl_options) do |conn|
15
+ conn.request(:basic_auth, config.username, config.password)
16
+ @config.configure_middleware(conn)
17
+ if opts[:header_params]["Content-Type"] == "multipart/form-data"
18
+ conn.request :multipart
19
+ conn.request :url_encoded
20
+ end
21
+ conn.adapter(Faraday.default_adapter)
22
+ end
23
+
24
+ begin
25
+ response = connection.public_send(http_method.to_sym.downcase) do |req|
26
+ build_request(http_method, path, req, opts)
27
+ end
28
+
29
+ if @config.debugging
30
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
31
+ end
32
+
33
+ unless response.success?
34
+ if response.status == 0
35
+ # Errors from libcurl will be made visible here
36
+ fail ApiError.new(:code => 0,
37
+ :message => response.return_message)
38
+ else
39
+ fail ApiError.new(:code => response.status,
40
+ :response_headers => response.headers,
41
+ :response_body => response.body),
42
+ response.reason_phrase
43
+ end
44
+ end
45
+ rescue Faraday::TimeoutError
46
+ fail ApiError.new('Connection timed out')
47
+ end
48
+
49
+ if opts[:return_type]
50
+ data = deserialize(response, opts[:return_type])
51
+ else
52
+ data = nil
53
+ end
54
+ return data, response.status, response.headers
55
+ end
56
+
57
+ # Builds the HTTP request
58
+ #
59
+ # @param [String] http_method HTTP method/verb (e.g. POST)
60
+ # @param [String] path URL path (e.g. /account/new)
61
+ # @option opts [Hash] :header_params Header parameters
62
+ # @option opts [Hash] :query_params Query parameters
63
+ # @option opts [Hash] :form_params Query parameters
64
+ # @option opts [Object] :body HTTP body (JSON/XML)
65
+ # @return [Typhoeus::Request] A Typhoeus Request
66
+ def build_request(http_method, path, request, opts = {})
67
+ url = build_request_url(path, opts)
68
+ http_method = http_method.to_sym.downcase
69
+
70
+ header_params = @default_headers.merge(opts[:header_params] || {})
71
+ query_params = opts[:query_params] || {}
72
+ form_params = opts[:form_params] || {}
73
+
74
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
75
+
76
+ req_opts = {
77
+ :params_encoding => @config.params_encoding,
78
+ :timeout => @config.timeout,
79
+ :verbose => @config.debugging
80
+ }
81
+
82
+ if [:post, :patch, :put, :delete].include?(http_method)
83
+ req_body = build_request_body(header_params, form_params, opts[:body])
84
+ if @config.debugging
85
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
86
+ end
87
+ end
88
+ request.headers = header_params
89
+ request.body = req_body
90
+ request.options = OpenStruct.new(req_opts)
91
+ request.url url
92
+ request.params = query_params
93
+ download_file(request) if opts[:return_type] == 'File'
94
+ request
95
+ end
96
+
97
+ # Builds the HTTP request body
98
+ #
99
+ # @param [Hash] header_params Header parameters
100
+ # @param [Hash] form_params Query parameters
101
+ # @param [Object] body HTTP body (JSON/XML)
102
+ # @return [String] HTTP body data in the form of string
103
+ def build_request_body(header_params, form_params, body)
104
+ # http form
105
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded'
106
+ data = URI.encode_www_form(form_params)
107
+ elsif header_params['Content-Type'] == 'multipart/form-data'
108
+ data = {}
109
+ form_params.each do |key, value|
110
+ case value
111
+ when ::File, ::Tempfile
112
+ # TODO hardcode to application/octet-stream, need better way to detect content type
113
+ data[key] = Faraday::UploadIO.new(value.path, 'application/octet-stream', value.path)
114
+ when ::Array, nil
115
+ # let Faraday handle Array and nil parameters
116
+ data[key] = value
117
+ else
118
+ data[key] = value.to_s
119
+ end
120
+ end
121
+ elsif body
122
+ data = body.is_a?(String) ? body : body.to_json
123
+ else
124
+ data = nil
125
+ end
126
+ data
127
+ end
128
+
129
+ def download_file(request)
130
+ @stream = []
131
+
132
+ # handle streaming Responses
133
+ request.options.on_data = Proc.new do |chunk, overall_received_bytes|
134
+ @stream << chunk
135
+ end
136
+ end
@@ -0,0 +1,220 @@
1
+ =begin
2
+ {{> api_info}}
3
+ =end
4
+
5
+ require 'spec_helper'
6
+
7
+ describe {{moduleName}}::ApiClient do
8
+ context 'initialization' do
9
+ context 'URL stuff' do
10
+ context 'host' do
11
+ it 'removes http from host' do
12
+ {{moduleName}}.configure { |c| c.host = 'http://example.com' }
13
+ expect({{moduleName}}::Configuration.default.host).to eq('example.com')
14
+ end
15
+
16
+ it 'removes https from host' do
17
+ {{moduleName}}.configure { |c| c.host = 'https://wookiee.com' }
18
+ expect({{moduleName}}::ApiClient.default.config.host).to eq('wookiee.com')
19
+ end
20
+
21
+ it 'removes trailing path from host' do
22
+ {{moduleName}}.configure { |c| c.host = 'hobo.com/v4' }
23
+ expect({{moduleName}}::Configuration.default.host).to eq('hobo.com')
24
+ end
25
+ end
26
+
27
+ context 'base_path' do
28
+ it "prepends a slash to base_path" do
29
+ {{moduleName}}.configure { |c| c.base_path = 'v4/dog' }
30
+ expect({{moduleName}}::Configuration.default.base_path).to eq('/v4/dog')
31
+ end
32
+
33
+ it "doesn't prepend a slash if one is already there" do
34
+ {{moduleName}}.configure { |c| c.base_path = '/v4/dog' }
35
+ expect({{moduleName}}::Configuration.default.base_path).to eq('/v4/dog')
36
+ end
37
+
38
+ it "ends up as a blank string if nil" do
39
+ {{moduleName}}.configure { |c| c.base_path = nil }
40
+ expect({{moduleName}}::Configuration.default.base_path).to eq('')
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ {{^isFaraday}}
47
+ describe 'params_encoding in #build_request' do
48
+ let(:config) { {{moduleName}}::Configuration.new }
49
+ let(:api_client) { {{moduleName}}::ApiClient.new(config) }
50
+
51
+ it 'defaults to nil' do
52
+ expect({{moduleName}}::Configuration.default.params_encoding).to eq(nil)
53
+ expect(config.params_encoding).to eq(nil)
54
+
55
+ request = api_client.build_request(:get, '/test')
56
+ expect(request.options[:params_encoding]).to eq(nil)
57
+ end
58
+
59
+ it 'can be customized' do
60
+ config.params_encoding = :multi
61
+ request = api_client.build_request(:get, '/test')
62
+ expect(request.options[:params_encoding]).to eq(:multi)
63
+ end
64
+ end
65
+
66
+ describe 'timeout in #build_request' do
67
+ let(:config) { {{moduleName}}::Configuration.new }
68
+ let(:api_client) { {{moduleName}}::ApiClient.new(config) }
69
+
70
+ it 'defaults to 0' do
71
+ expect({{moduleName}}::Configuration.default.timeout).to eq(0)
72
+ expect(config.timeout).to eq(0)
73
+
74
+ request = api_client.build_request(:get, '/test')
75
+ expect(request.options[:timeout]).to eq(0)
76
+ end
77
+
78
+ it 'can be customized' do
79
+ config.timeout = 100
80
+ request = api_client.build_request(:get, '/test')
81
+ expect(request.options[:timeout]).to eq(100)
82
+ end
83
+ end
84
+
85
+ {{/isFaraday}}
86
+ describe '#deserialize' do
87
+ it "handles Array<Integer>" do
88
+ api_client = {{moduleName}}::ApiClient.new
89
+ headers = { 'Content-Type' => 'application/json' }
90
+ response = double('response', headers: headers, body: '[12, 34]')
91
+ data = api_client.deserialize(response, 'Array<Integer>')
92
+ expect(data).to be_instance_of(Array)
93
+ expect(data).to eq([12, 34])
94
+ end
95
+
96
+ it 'handles Array<Array<Integer>>' do
97
+ api_client = {{moduleName}}::ApiClient.new
98
+ headers = { 'Content-Type' => 'application/json' }
99
+ response = double('response', headers: headers, body: '[[12, 34], [56]]')
100
+ data = api_client.deserialize(response, 'Array<Array<Integer>>')
101
+ expect(data).to be_instance_of(Array)
102
+ expect(data).to eq([[12, 34], [56]])
103
+ end
104
+
105
+ it 'handles Hash<String, String>' do
106
+ api_client = {{moduleName}}::ApiClient.new
107
+ headers = { 'Content-Type' => 'application/json' }
108
+ response = double('response', headers: headers, body: '{"message": "Hello"}')
109
+ data = api_client.deserialize(response, 'Hash<String, String>')
110
+ expect(data).to be_instance_of(Hash)
111
+ expect(data).to eq(:message => 'Hello')
112
+ end
113
+ end
114
+
115
+ describe "#object_to_hash" do
116
+ it 'ignores nils and includes empty arrays' do
117
+ # uncomment below to test object_to_hash for model
118
+ # api_client = {{moduleName}}::ApiClient.new
119
+ # _model = {{moduleName}}::ModelName.new
120
+ # update the model attribute below
121
+ # _model.id = 1
122
+ # update the expected value (hash) below
123
+ # expected = {id: 1, name: '', tags: []}
124
+ # expect(api_client.object_to_hash(_model)).to eq(expected)
125
+ end
126
+ end
127
+
128
+ describe '#build_collection_param' do
129
+ let(:param) { ['aa', 'bb', 'cc'] }
130
+ let(:api_client) { {{moduleName}}::ApiClient.new }
131
+
132
+ it 'works for csv' do
133
+ expect(api_client.build_collection_param(param, :csv)).to eq('aa,bb,cc')
134
+ end
135
+
136
+ it 'works for ssv' do
137
+ expect(api_client.build_collection_param(param, :ssv)).to eq('aa bb cc')
138
+ end
139
+
140
+ it 'works for tsv' do
141
+ expect(api_client.build_collection_param(param, :tsv)).to eq("aa\tbb\tcc")
142
+ end
143
+
144
+ it 'works for pipes' do
145
+ expect(api_client.build_collection_param(param, :pipes)).to eq('aa|bb|cc')
146
+ end
147
+
148
+ it 'works for multi' do
149
+ expect(api_client.build_collection_param(param, :multi)).to eq(['aa', 'bb', 'cc'])
150
+ end
151
+
152
+ it 'fails for invalid collection format' do
153
+ expect { api_client.build_collection_param(param, :INVALID) }.to raise_error(RuntimeError, 'unknown collection format: :INVALID')
154
+ end
155
+ end
156
+
157
+ describe '#json_mime?' do
158
+ let(:api_client) { {{moduleName}}::ApiClient.new }
159
+
160
+ it 'works' do
161
+ expect(api_client.json_mime?(nil)).to eq false
162
+ expect(api_client.json_mime?('')).to eq false
163
+
164
+ expect(api_client.json_mime?('application/json')).to eq true
165
+ expect(api_client.json_mime?('application/json; charset=UTF8')).to eq true
166
+ expect(api_client.json_mime?('APPLICATION/JSON')).to eq true
167
+
168
+ expect(api_client.json_mime?('application/xml')).to eq false
169
+ expect(api_client.json_mime?('text/plain')).to eq false
170
+ expect(api_client.json_mime?('application/jsonp')).to eq false
171
+ end
172
+ end
173
+
174
+ describe '#select_header_accept' do
175
+ let(:api_client) { {{moduleName}}::ApiClient.new }
176
+
177
+ it 'works' do
178
+ expect(api_client.select_header_accept(nil)).to be_nil
179
+ expect(api_client.select_header_accept([])).to be_nil
180
+
181
+ expect(api_client.select_header_accept(['application/json'])).to eq('application/json')
182
+ expect(api_client.select_header_accept(['application/xml', 'application/json; charset=UTF8'])).to eq('application/json; charset=UTF8')
183
+ expect(api_client.select_header_accept(['APPLICATION/JSON', 'text/html'])).to eq('APPLICATION/JSON')
184
+
185
+ expect(api_client.select_header_accept(['application/xml'])).to eq('application/xml')
186
+ expect(api_client.select_header_accept(['text/html', 'application/xml'])).to eq('text/html,application/xml')
187
+ end
188
+ end
189
+
190
+ describe '#select_header_content_type' do
191
+ let(:api_client) { {{moduleName}}::ApiClient.new }
192
+
193
+ it 'works' do
194
+ expect(api_client.select_header_content_type(nil)).to be_nil
195
+ expect(api_client.select_header_content_type([])).to be_nil
196
+
197
+ expect(api_client.select_header_content_type(['application/json'])).to eq('application/json')
198
+ expect(api_client.select_header_content_type(['application/xml', 'application/json; charset=UTF8'])).to eq('application/json; charset=UTF8')
199
+ expect(api_client.select_header_content_type(['APPLICATION/JSON', 'text/html'])).to eq('APPLICATION/JSON')
200
+ expect(api_client.select_header_content_type(['application/xml'])).to eq('application/xml')
201
+ expect(api_client.select_header_content_type(['text/plain', 'application/xml'])).to eq('text/plain')
202
+ end
203
+ end
204
+
205
+ describe '#sanitize_filename' do
206
+ let(:api_client) { {{moduleName}}::ApiClient.new }
207
+
208
+ it 'works' do
209
+ expect(api_client.sanitize_filename('sun')).to eq('sun')
210
+ expect(api_client.sanitize_filename('sun.gif')).to eq('sun.gif')
211
+ expect(api_client.sanitize_filename('../sun.gif')).to eq('sun.gif')
212
+ expect(api_client.sanitize_filename('/var/tmp/sun.gif')).to eq('sun.gif')
213
+ expect(api_client.sanitize_filename('./sun.gif')).to eq('sun.gif')
214
+ expect(api_client.sanitize_filename('..\sun.gif')).to eq('sun.gif')
215
+ expect(api_client.sanitize_filename('\var\tmp\sun.gif')).to eq('sun.gif')
216
+ expect(api_client.sanitize_filename('c:\var\tmp\sun.gif')).to eq('sun.gif')
217
+ expect(api_client.sanitize_filename('.\sun.gif')).to eq('sun.gif')
218
+ end
219
+ end
220
+ end