worker_ruby 0.5.7

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 (156) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +201 -0
  3. data/README.md +118 -0
  4. data/VERSION +1 -0
  5. data/VERSION.txt +1 -0
  6. data/docs/Complete.md +10 -0
  7. data/docs/CoreApi.md +200 -0
  8. data/docs/Error.md +8 -0
  9. data/docs/ErrorBody.md +9 -0
  10. data/docs/Group.md +12 -0
  11. data/docs/GroupWrapper.md +8 -0
  12. data/docs/GroupsApi.md +197 -0
  13. data/docs/GroupsWrapper.md +8 -0
  14. data/docs/IdStatus.md +9 -0
  15. data/docs/Image.md +10 -0
  16. data/docs/ImageWrapper.md +8 -0
  17. data/docs/ImagesApi.md +98 -0
  18. data/docs/ImagesWrapper.md +8 -0
  19. data/docs/Job.md +25 -0
  20. data/docs/JobWrapper.md +8 -0
  21. data/docs/JobsApi.md +684 -0
  22. data/docs/JobsWrapper.md +10 -0
  23. data/docs/Log.md +8 -0
  24. data/docs/NewJob.md +14 -0
  25. data/docs/NewJobWithImage.md +15 -0
  26. data/docs/NewJobsWrapper.md +8 -0
  27. data/docs/NewTask.md +14 -0
  28. data/docs/NewTasksWrapper.md +8 -0
  29. data/docs/Reason.md +7 -0
  30. data/docs/RunnerApi.md +170 -0
  31. data/docs/Start.md +8 -0
  32. data/docs/Task.md +25 -0
  33. data/docs/TaskWrapper.md +8 -0
  34. data/docs/TasksApi.md +684 -0
  35. data/docs/TasksWrapper.md +10 -0
  36. data/git_push.sh +67 -0
  37. data/iron_ruby.gemspec +55 -0
  38. data/iron_titan.gemspec +32 -0
  39. data/lib/iron_ruby.rb +66 -0
  40. data/lib/iron_ruby/api/groups_api.rb +262 -0
  41. data/lib/iron_ruby/api/runner_api.rb +229 -0
  42. data/lib/iron_ruby/api/tasks_api.rb +842 -0
  43. data/lib/iron_ruby/api_client.rb +378 -0
  44. data/lib/iron_ruby/api_error.rb +47 -0
  45. data/lib/iron_ruby/configuration.rb +207 -0
  46. data/lib/iron_ruby/models/complete.rb +220 -0
  47. data/lib/iron_ruby/models/error.rb +199 -0
  48. data/lib/iron_ruby/models/error_body.rb +208 -0
  49. data/lib/iron_ruby/models/group.rb +242 -0
  50. data/lib/iron_ruby/models/group_wrapper.rb +200 -0
  51. data/lib/iron_ruby/models/groups_wrapper.rb +202 -0
  52. data/lib/iron_ruby/models/id_status.rb +243 -0
  53. data/lib/iron_ruby/models/new_task.rb +270 -0
  54. data/lib/iron_ruby/models/new_tasks_wrapper.rb +202 -0
  55. data/lib/iron_ruby/models/start.rb +200 -0
  56. data/lib/iron_ruby/models/task.rb +427 -0
  57. data/lib/iron_ruby/models/task_wrapper.rb +200 -0
  58. data/lib/iron_ruby/models/tasks_wrapper.rb +221 -0
  59. data/lib/iron_ruby/version.rb +26 -0
  60. data/lib/iron_titan.rb +66 -0
  61. data/lib/iron_titan/api/core_api.rb +260 -0
  62. data/lib/iron_titan/api/groups_api.rb +262 -0
  63. data/lib/iron_titan/api/images_api.rb +138 -0
  64. data/lib/iron_titan/api/jobs_api.rb +842 -0
  65. data/lib/iron_titan/api/runner_api.rb +229 -0
  66. data/lib/iron_titan/api_client.rb +372 -0
  67. data/lib/iron_titan/api_error.rb +47 -0
  68. data/lib/iron_titan/configuration.rb +186 -0
  69. data/lib/iron_titan/models/complete.rb +220 -0
  70. data/lib/iron_titan/models/error.rb +199 -0
  71. data/lib/iron_titan/models/error_body.rb +208 -0
  72. data/lib/iron_titan/models/group.rb +242 -0
  73. data/lib/iron_titan/models/group_wrapper.rb +200 -0
  74. data/lib/iron_titan/models/groups_wrapper.rb +202 -0
  75. data/lib/iron_titan/models/id_status.rb +243 -0
  76. data/lib/iron_titan/models/image.rb +184 -0
  77. data/lib/iron_titan/models/image_wrapper.rb +161 -0
  78. data/lib/iron_titan/models/images_wrapper.rb +163 -0
  79. data/lib/iron_titan/models/job.rb +427 -0
  80. data/lib/iron_titan/models/job_array.rb +160 -0
  81. data/lib/iron_titan/models/job_wrapper.rb +200 -0
  82. data/lib/iron_titan/models/jobs_wrapper.rb +221 -0
  83. data/lib/iron_titan/models/log.rb +161 -0
  84. data/lib/iron_titan/models/new_job.rb +270 -0
  85. data/lib/iron_titan/models/new_job_array.rb +160 -0
  86. data/lib/iron_titan/models/new_job_with_image.rb +249 -0
  87. data/lib/iron_titan/models/new_jobs_wrapper.rb +202 -0
  88. data/lib/iron_titan/models/reason.rb +164 -0
  89. data/lib/iron_titan/models/start.rb +200 -0
  90. data/lib/iron_titan/version.rb +26 -0
  91. data/lib/worker_ruby.rb +66 -0
  92. data/lib/worker_ruby/api/groups_api.rb +262 -0
  93. data/lib/worker_ruby/api/runner_api.rb +229 -0
  94. data/lib/worker_ruby/api/tasks_api.rb +842 -0
  95. data/lib/worker_ruby/api_client.rb +378 -0
  96. data/lib/worker_ruby/api_error.rb +47 -0
  97. data/lib/worker_ruby/configuration.rb +207 -0
  98. data/lib/worker_ruby/models/complete.rb +220 -0
  99. data/lib/worker_ruby/models/error.rb +199 -0
  100. data/lib/worker_ruby/models/error_body.rb +208 -0
  101. data/lib/worker_ruby/models/group.rb +242 -0
  102. data/lib/worker_ruby/models/group_wrapper.rb +200 -0
  103. data/lib/worker_ruby/models/groups_wrapper.rb +202 -0
  104. data/lib/worker_ruby/models/id_status.rb +243 -0
  105. data/lib/worker_ruby/models/new_task.rb +270 -0
  106. data/lib/worker_ruby/models/new_tasks_wrapper.rb +202 -0
  107. data/lib/worker_ruby/models/start.rb +200 -0
  108. data/lib/worker_ruby/models/task.rb +427 -0
  109. data/lib/worker_ruby/models/task_wrapper.rb +200 -0
  110. data/lib/worker_ruby/models/tasks_wrapper.rb +221 -0
  111. data/lib/worker_ruby/version.rb +26 -0
  112. data/spec/api/CoreApi_spec.rb +99 -0
  113. data/spec/api/JobsApi_spec.rb +131 -0
  114. data/spec/api/core_api_spec.rb +99 -0
  115. data/spec/api/groups_api_spec.rb +94 -0
  116. data/spec/api/images_api_spec.rb +66 -0
  117. data/spec/api/jobs_api_spec.rb +220 -0
  118. data/spec/api/runner_api_spec.rb +88 -0
  119. data/spec/api/tasks_api_spec.rb +220 -0
  120. data/spec/api_client_spec.rb +237 -0
  121. data/spec/configuration_spec.rb +53 -0
  122. data/spec/models/ErrorBody_spec.rb +56 -0
  123. data/spec/models/JobArray_spec.rb +46 -0
  124. data/spec/models/JobWrapper_spec.rb +46 -0
  125. data/spec/models/NewJobArray_spec.rb +46 -0
  126. data/spec/models/NewJob_spec.rb +96 -0
  127. data/spec/models/complete_spec.rb +65 -0
  128. data/spec/models/error_body_spec.rb +59 -0
  129. data/spec/models/error_spec.rb +53 -0
  130. data/spec/models/group_spec.rb +77 -0
  131. data/spec/models/group_wrapper_spec.rb +53 -0
  132. data/spec/models/groups_wrapper_spec.rb +53 -0
  133. data/spec/models/id_status_spec.rb +63 -0
  134. data/spec/models/image_spec.rb +66 -0
  135. data/spec/models/image_wrapper_spec.rb +46 -0
  136. data/spec/models/images_wrapper_spec.rb +46 -0
  137. data/spec/models/job_array_spec.rb +46 -0
  138. data/spec/models/job_spec.rb +161 -0
  139. data/spec/models/job_wrapper_spec.rb +53 -0
  140. data/spec/models/jobs_wrapper_spec.rb +65 -0
  141. data/spec/models/log_spec.rb +46 -0
  142. data/spec/models/new_job_array_spec.rb +46 -0
  143. data/spec/models/new_job_spec.rb +89 -0
  144. data/spec/models/new_job_with_image_spec.rb +116 -0
  145. data/spec/models/new_jobs_wrapper_spec.rb +53 -0
  146. data/spec/models/new_task_spec.rb +89 -0
  147. data/spec/models/new_tasks_wrapper_spec.rb +53 -0
  148. data/spec/models/reason_spec.rb +36 -0
  149. data/spec/models/start_spec.rb +53 -0
  150. data/spec/models/task_spec.rb +163 -0
  151. data/spec/models/task_wrapper_spec.rb +53 -0
  152. data/spec/models/tasks_wrapper_spec.rb +65 -0
  153. data/spec/spec_helper.rb +122 -0
  154. data/worker_ruby-0.5.5.gem +0 -0
  155. data/worker_ruby.gemspec +55 -0
  156. metadata +420 -0
@@ -0,0 +1,229 @@
1
+ =begin
2
+ Titan API
3
+
4
+ The ultimate, language agnostic, container based job processing framework.
5
+
6
+ OpenAPI spec version: 0.4.9
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+ Licensed under the Apache License, Version 2.0 (the "License");
11
+ you may not use this file except in compliance with the License.
12
+ You may obtain a copy of the License at
13
+
14
+ http://www.apache.org/licenses/LICENSE-2.0
15
+
16
+ Unless required by applicable law or agreed to in writing, software
17
+ distributed under the License is distributed on an "AS IS" BASIS,
18
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ See the License for the specific language governing permissions and
20
+ limitations under the License.
21
+
22
+ =end
23
+
24
+ require "uri"
25
+
26
+ module IronTitan
27
+ class RunnerApi
28
+ attr_accessor :api_client
29
+
30
+ def initialize(api_client = ApiClient.default)
31
+ @api_client = api_client
32
+ end
33
+
34
+ # Mark job as failed.
35
+ # Job is marked as failed if it was in a valid state. Job's `finished_at` time is initialized.
36
+ # @param name Name of group for this set of jobs.
37
+ # @param id Job id
38
+ # @param body
39
+ # @param [Hash] opts the optional parameters
40
+ # @return [JobWrapper]
41
+ def groups_name_jobs_id_error_post(name, id, body, opts = {})
42
+ data, _status_code, _headers = groups_name_jobs_id_error_post_with_http_info(name, id, body, opts)
43
+ return data
44
+ end
45
+
46
+ # Mark job as failed.
47
+ # Job is marked as failed if it was in a valid state. Job's `finished_at` time is initialized.
48
+ # @param name Name of group for this set of jobs.
49
+ # @param id Job id
50
+ # @param body
51
+ # @param [Hash] opts the optional parameters
52
+ # @return [Array<(JobWrapper, Fixnum, Hash)>] JobWrapper data, response status code and response headers
53
+ def groups_name_jobs_id_error_post_with_http_info(name, id, body, opts = {})
54
+ if @api_client.config.debugging
55
+ @api_client.config.logger.debug "Calling API: RunnerApi.groups_name_jobs_id_error_post ..."
56
+ end
57
+ # verify the required parameter 'name' is set
58
+ fail ArgumentError, "Missing the required parameter 'name' when calling RunnerApi.groups_name_jobs_id_error_post" if name.nil?
59
+ # verify the required parameter 'id' is set
60
+ fail ArgumentError, "Missing the required parameter 'id' when calling RunnerApi.groups_name_jobs_id_error_post" if id.nil?
61
+ # verify the required parameter 'body' is set
62
+ fail ArgumentError, "Missing the required parameter 'body' when calling RunnerApi.groups_name_jobs_id_error_post" if body.nil?
63
+ # resource path
64
+ local_var_path = "/groups/{name}/jobs/{id}/error".sub('{format}','json').sub('{' + 'name' + '}', name.to_s).sub('{' + 'id' + '}', id.to_s)
65
+
66
+ # query parameters
67
+ query_params = {}
68
+
69
+ # header parameters
70
+ header_params = {}
71
+
72
+ # HTTP header 'Accept' (if needed)
73
+ local_header_accept = ['application/json']
74
+ local_header_accept_result = @api_client.select_header_accept(local_header_accept) and header_params['Accept'] = local_header_accept_result
75
+
76
+ # HTTP header 'Content-Type'
77
+ local_header_content_type = ['application/json']
78
+ header_params['Content-Type'] = @api_client.select_header_content_type(local_header_content_type)
79
+
80
+ # form parameters
81
+ form_params = {}
82
+
83
+ # http body (model)
84
+ post_body = @api_client.object_to_http_body(body)
85
+ auth_names = []
86
+ data, status_code, headers = @api_client.call_api(:POST, local_var_path,
87
+ :header_params => header_params,
88
+ :query_params => query_params,
89
+ :form_params => form_params,
90
+ :body => post_body,
91
+ :auth_names => auth_names,
92
+ :return_type => 'JobWrapper')
93
+ if @api_client.config.debugging
94
+ @api_client.config.logger.debug "API called: RunnerApi#groups_name_jobs_id_error_post\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
95
+ end
96
+ return data, status_code, headers
97
+ end
98
+
99
+ # Mark job as started, ie: status = 'running'
100
+ # Job status is changed to 'running' if it was in a valid state before. Job's `started_at` time is initialized.
101
+ # @param name Name of group for this set of jobs.
102
+ # @param id Job id
103
+ # @param body
104
+ # @param [Hash] opts the optional parameters
105
+ # @return [JobWrapper]
106
+ def groups_name_jobs_id_start_post(name, id, body, opts = {})
107
+ data, _status_code, _headers = groups_name_jobs_id_start_post_with_http_info(name, id, body, opts)
108
+ return data
109
+ end
110
+
111
+ # Mark job as started, ie: status &#x3D; &#39;running&#39;
112
+ # Job status is changed to &#39;running&#39; if it was in a valid state before. Job&#39;s &#x60;started_at&#x60; time is initialized.
113
+ # @param name Name of group for this set of jobs.
114
+ # @param id Job id
115
+ # @param body
116
+ # @param [Hash] opts the optional parameters
117
+ # @return [Array<(JobWrapper, Fixnum, Hash)>] JobWrapper data, response status code and response headers
118
+ def groups_name_jobs_id_start_post_with_http_info(name, id, body, opts = {})
119
+ if @api_client.config.debugging
120
+ @api_client.config.logger.debug "Calling API: RunnerApi.groups_name_jobs_id_start_post ..."
121
+ end
122
+ # verify the required parameter 'name' is set
123
+ fail ArgumentError, "Missing the required parameter 'name' when calling RunnerApi.groups_name_jobs_id_start_post" if name.nil?
124
+ # verify the required parameter 'id' is set
125
+ fail ArgumentError, "Missing the required parameter 'id' when calling RunnerApi.groups_name_jobs_id_start_post" if id.nil?
126
+ # verify the required parameter 'body' is set
127
+ fail ArgumentError, "Missing the required parameter 'body' when calling RunnerApi.groups_name_jobs_id_start_post" if body.nil?
128
+ # resource path
129
+ local_var_path = "/groups/{name}/jobs/{id}/start".sub('{format}','json').sub('{' + 'name' + '}', name.to_s).sub('{' + 'id' + '}', id.to_s)
130
+
131
+ # query parameters
132
+ query_params = {}
133
+
134
+ # header parameters
135
+ header_params = {}
136
+
137
+ # HTTP header 'Accept' (if needed)
138
+ local_header_accept = ['application/json']
139
+ local_header_accept_result = @api_client.select_header_accept(local_header_accept) and header_params['Accept'] = local_header_accept_result
140
+
141
+ # HTTP header 'Content-Type'
142
+ local_header_content_type = ['application/json']
143
+ header_params['Content-Type'] = @api_client.select_header_content_type(local_header_content_type)
144
+
145
+ # form parameters
146
+ form_params = {}
147
+
148
+ # http body (model)
149
+ post_body = @api_client.object_to_http_body(body)
150
+ auth_names = []
151
+ data, status_code, headers = @api_client.call_api(:POST, local_var_path,
152
+ :header_params => header_params,
153
+ :query_params => query_params,
154
+ :form_params => form_params,
155
+ :body => post_body,
156
+ :auth_names => auth_names,
157
+ :return_type => 'JobWrapper')
158
+ if @api_client.config.debugging
159
+ @api_client.config.logger.debug "API called: RunnerApi#groups_name_jobs_id_start_post\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
160
+ end
161
+ return data, status_code, headers
162
+ end
163
+
164
+ # Mark job as succeeded.
165
+ # Job status is changed to succeeded if it was in a valid state before. Job's `completed_at` time is initialized.
166
+ # @param name Name of group for this set of jobs.
167
+ # @param id Job id
168
+ # @param body
169
+ # @param [Hash] opts the optional parameters
170
+ # @return [JobWrapper]
171
+ def groups_name_jobs_id_success_post(name, id, body, opts = {})
172
+ data, _status_code, _headers = groups_name_jobs_id_success_post_with_http_info(name, id, body, opts)
173
+ return data
174
+ end
175
+
176
+ # Mark job as succeeded.
177
+ # Job status is changed to succeeded if it was in a valid state before. Job&#39;s &#x60;completed_at&#x60; time is initialized.
178
+ # @param name Name of group for this set of jobs.
179
+ # @param id Job id
180
+ # @param body
181
+ # @param [Hash] opts the optional parameters
182
+ # @return [Array<(JobWrapper, Fixnum, Hash)>] JobWrapper data, response status code and response headers
183
+ def groups_name_jobs_id_success_post_with_http_info(name, id, body, opts = {})
184
+ if @api_client.config.debugging
185
+ @api_client.config.logger.debug "Calling API: RunnerApi.groups_name_jobs_id_success_post ..."
186
+ end
187
+ # verify the required parameter 'name' is set
188
+ fail ArgumentError, "Missing the required parameter 'name' when calling RunnerApi.groups_name_jobs_id_success_post" if name.nil?
189
+ # verify the required parameter 'id' is set
190
+ fail ArgumentError, "Missing the required parameter 'id' when calling RunnerApi.groups_name_jobs_id_success_post" if id.nil?
191
+ # verify the required parameter 'body' is set
192
+ fail ArgumentError, "Missing the required parameter 'body' when calling RunnerApi.groups_name_jobs_id_success_post" if body.nil?
193
+ # resource path
194
+ local_var_path = "/groups/{name}/jobs/{id}/success".sub('{format}','json').sub('{' + 'name' + '}', name.to_s).sub('{' + 'id' + '}', id.to_s)
195
+
196
+ # query parameters
197
+ query_params = {}
198
+
199
+ # header parameters
200
+ header_params = {}
201
+
202
+ # HTTP header 'Accept' (if needed)
203
+ local_header_accept = ['application/json']
204
+ local_header_accept_result = @api_client.select_header_accept(local_header_accept) and header_params['Accept'] = local_header_accept_result
205
+
206
+ # HTTP header 'Content-Type'
207
+ local_header_content_type = ['application/json']
208
+ header_params['Content-Type'] = @api_client.select_header_content_type(local_header_content_type)
209
+
210
+ # form parameters
211
+ form_params = {}
212
+
213
+ # http body (model)
214
+ post_body = @api_client.object_to_http_body(body)
215
+ auth_names = []
216
+ data, status_code, headers = @api_client.call_api(:POST, local_var_path,
217
+ :header_params => header_params,
218
+ :query_params => query_params,
219
+ :form_params => form_params,
220
+ :body => post_body,
221
+ :auth_names => auth_names,
222
+ :return_type => 'JobWrapper')
223
+ if @api_client.config.debugging
224
+ @api_client.config.logger.debug "API called: RunnerApi#groups_name_jobs_id_success_post\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
225
+ end
226
+ return data, status_code, headers
227
+ end
228
+ end
229
+ end
@@ -0,0 +1,372 @@
1
+ =begin
2
+ Titan API
3
+
4
+ The ultimate, language agnostic, container based job processing framework.
5
+
6
+ OpenAPI spec version: 0.4.9
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+ Licensed under the Apache License, Version 2.0 (the "License");
11
+ you may not use this file except in compliance with the License.
12
+ You may obtain a copy of the License at
13
+
14
+ http://www.apache.org/licenses/LICENSE-2.0
15
+
16
+ Unless required by applicable law or agreed to in writing, software
17
+ distributed under the License is distributed on an "AS IS" BASIS,
18
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ See the License for the specific language governing permissions and
20
+ limitations under the License.
21
+
22
+ =end
23
+
24
+ require 'date'
25
+ require 'json'
26
+ require 'logger'
27
+ require 'tempfile'
28
+ require 'typhoeus'
29
+ require 'uri'
30
+
31
+ module IronTitan
32
+ class ApiClient
33
+ # The Configuration object holding settings to be used in the API client.
34
+ attr_accessor :config
35
+
36
+ # Defines the headers to be used in HTTP requests of all API calls by default.
37
+ #
38
+ # @return [Hash]
39
+ attr_accessor :default_headers
40
+
41
+ # Initializes the ApiClient
42
+ # @option config [Configuration] Configuraiton for initializing the object, default to Configuration.default
43
+ def initialize(config = Configuration.default)
44
+ @config = config
45
+ @user_agent = "Swagger-Codegen/#{VERSION}/ruby"
46
+ @default_headers = {
47
+ 'Content-Type' => "application/json",
48
+ 'User-Agent' => @user_agent
49
+ }
50
+ end
51
+
52
+ def self.default
53
+ @@default ||= ApiClient.new
54
+ end
55
+
56
+ # Call an API with given options.
57
+ #
58
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
59
+ # the data deserialized from response body (could be nil), response status code and response headers.
60
+ def call_api(http_method, path, opts = {})
61
+ request = build_request(http_method, path, opts)
62
+ response = request.run
63
+
64
+ if @config.debugging
65
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
66
+ end
67
+
68
+ unless response.success?
69
+ fail ApiError.new(:code => response.code,
70
+ :response_headers => response.headers,
71
+ :response_body => response.body),
72
+ response.status_message
73
+ end
74
+
75
+ if opts[:return_type]
76
+ data = deserialize(response, opts[:return_type])
77
+ else
78
+ data = nil
79
+ end
80
+ return data, response.code, response.headers
81
+ end
82
+
83
+ # Builds the HTTP request
84
+ #
85
+ # @param [String] http_method HTTP method/verb (e.g. POST)
86
+ # @param [String] path URL path (e.g. /account/new)
87
+ # @option opts [Hash] :header_params Header parameters
88
+ # @option opts [Hash] :query_params Query parameters
89
+ # @option opts [Hash] :form_params Query parameters
90
+ # @option opts [Object] :body HTTP body (JSON/XML)
91
+ # @return [Typhoeus::Request] A Typhoeus Request
92
+ def build_request(http_method, path, opts = {})
93
+ url = build_request_url(path)
94
+ http_method = http_method.to_sym.downcase
95
+
96
+ header_params = @default_headers.merge(opts[:header_params] || {})
97
+ query_params = opts[:query_params] || {}
98
+ form_params = opts[:form_params] || {}
99
+
100
+
101
+ req_opts = {
102
+ :method => http_method,
103
+ :headers => header_params,
104
+ :params => query_params,
105
+ :timeout => @config.timeout,
106
+ :ssl_verifypeer => @config.verify_ssl,
107
+ :sslcert => @config.cert_file,
108
+ :sslkey => @config.key_file,
109
+ :verbose => @config.debugging
110
+ }
111
+
112
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
113
+
114
+ if [:post, :patch, :put, :delete].include?(http_method)
115
+ req_body = build_request_body(header_params, form_params, opts[:body])
116
+ req_opts.update :body => req_body
117
+ if @config.debugging
118
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
119
+ end
120
+ end
121
+
122
+ Typhoeus::Request.new(url, req_opts)
123
+ end
124
+
125
+ # Check if the given MIME is a JSON MIME.
126
+ # JSON MIME examples:
127
+ # application/json
128
+ # application/json; charset=UTF8
129
+ # APPLICATION/JSON
130
+ # @param [String] mime MIME
131
+ # @return [Boolean] True if the MIME is applicaton/json
132
+ def json_mime?(mime)
133
+ !(mime =~ /\Aapplication\/json(;.*)?\z/i).nil?
134
+ end
135
+
136
+ # Deserialize the response to the given return type.
137
+ #
138
+ # @param [Response] response HTTP response
139
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
140
+ def deserialize(response, return_type)
141
+ body = response.body
142
+ return nil if body.nil? || body.empty?
143
+
144
+ # return response body directly for String return type
145
+ return body if return_type == 'String'
146
+
147
+ # handle file downloading - save response body into a tmp file and return the File instance
148
+ return download_file(response) if return_type == 'File'
149
+
150
+ # ensuring a default content type
151
+ content_type = response.headers['Content-Type'] || 'application/json'
152
+
153
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
154
+
155
+ begin
156
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
157
+ rescue JSON::ParserError => e
158
+ if %w(String Date DateTime).include?(return_type)
159
+ data = body
160
+ else
161
+ raise e
162
+ end
163
+ end
164
+
165
+ convert_to_type data, return_type
166
+ end
167
+
168
+ # Convert data to the given return type.
169
+ # @param [Object] data Data to be converted
170
+ # @param [String] return_type Return type
171
+ # @return [Mixed] Data in a particular type
172
+ def convert_to_type(data, return_type)
173
+ return nil if data.nil?
174
+ case return_type
175
+ when 'String'
176
+ data.to_s
177
+ when 'Integer'
178
+ data.to_i
179
+ when 'Float'
180
+ data.to_f
181
+ when 'BOOLEAN'
182
+ data == true
183
+ when 'DateTime'
184
+ # parse date time (expecting ISO 8601 format)
185
+ DateTime.parse data
186
+ when 'Date'
187
+ # parse date time (expecting ISO 8601 format)
188
+ Date.parse data
189
+ when 'Object'
190
+ # generic object (usually a Hash), return directly
191
+ data
192
+ when /\AArray<(.+)>\z/
193
+ # e.g. Array<Pet>
194
+ sub_type = $1
195
+ data.map {|item| convert_to_type(item, sub_type) }
196
+ when /\AHash\<String, (.+)\>\z/
197
+ # e.g. Hash<String, Integer>
198
+ sub_type = $1
199
+ {}.tap do |hash|
200
+ data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
201
+ end
202
+ else
203
+ # models, e.g. Pet
204
+ IronTitan.const_get(return_type).new.tap do |model|
205
+ model.build_from_hash data
206
+ end
207
+ end
208
+ end
209
+
210
+ # Save response body into a file in (the defined) temporary folder, using the filename
211
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
212
+ #
213
+ # @see Configuration#temp_folder_path
214
+ # @return [Tempfile] the file downloaded
215
+ def download_file(response)
216
+ content_disposition = response.headers['Content-Disposition']
217
+ if content_disposition
218
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
219
+ prefix = sanitize_filename(filename)
220
+ else
221
+ prefix = 'download-'
222
+ end
223
+ prefix = prefix + '-' unless prefix.end_with?('-')
224
+
225
+ tempfile = nil
226
+ encoding = response.body.encoding
227
+ Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file|
228
+ file.write(response.body)
229
+ tempfile = file
230
+ end
231
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
232
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
233
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
234
+ "explicitly with `tempfile.delete`"
235
+ tempfile
236
+ end
237
+
238
+ # Sanitize filename by removing path.
239
+ # e.g. ../../sun.gif becomes sun.gif
240
+ #
241
+ # @param [String] filename the filename to be sanitized
242
+ # @return [String] the sanitized filename
243
+ def sanitize_filename(filename)
244
+ filename.gsub(/.*[\/\\]/, '')
245
+ end
246
+
247
+ def build_request_url(path)
248
+ # Add leading and trailing slashes to path
249
+ path = "/#{path}".gsub(/\/+/, '/')
250
+ URI.encode(@config.base_url + path)
251
+ end
252
+
253
+ # Builds the HTTP request body
254
+ #
255
+ # @param [Hash] header_params Header parameters
256
+ # @param [Hash] form_params Query parameters
257
+ # @param [Object] body HTTP body (JSON/XML)
258
+ # @return [String] HTTP body data in the form of string
259
+ def build_request_body(header_params, form_params, body)
260
+ # http form
261
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
262
+ header_params['Content-Type'] == 'multipart/form-data'
263
+ data = {}
264
+ form_params.each do |key, value|
265
+ case value
266
+ when File, Array, nil
267
+ # let typhoeus handle File, Array and nil parameters
268
+ data[key] = value
269
+ else
270
+ data[key] = value.to_s
271
+ end
272
+ end
273
+ elsif body
274
+ data = body.is_a?(String) ? body : body.to_json
275
+ else
276
+ data = nil
277
+ end
278
+ data
279
+ end
280
+
281
+ # Update hearder and query params based on authentication settings.
282
+ #
283
+ # @param [Hash] header_params Header parameters
284
+ # @param [Hash] form_params Query parameters
285
+ # @param [String] auth_names Authentication scheme name
286
+ def update_params_for_auth!(header_params, query_params, auth_names)
287
+ Array(auth_names).each do |auth_name|
288
+ auth_setting = @config.auth_settings[auth_name]
289
+ next unless auth_setting
290
+ case auth_setting[:in]
291
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
292
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
293
+ else fail ArgumentError, 'Authentication token must be in `query` of `header`'
294
+ end
295
+ end
296
+ end
297
+
298
+ # Sets user agent in HTTP header
299
+ #
300
+ # @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
301
+ def user_agent=(user_agent)
302
+ @user_agent = user_agent
303
+ @default_headers['User-Agent'] = @user_agent
304
+ end
305
+
306
+ # Return Accept header based on an array of accepts provided.
307
+ # @param [Array] accepts array for Accept
308
+ # @return [String] the Accept header (e.g. application/json)
309
+ def select_header_accept(accepts)
310
+ return nil if accepts.nil? || accepts.empty?
311
+ # use JSON when present, otherwise use all of the provided
312
+ json_accept = accepts.find { |s| json_mime?(s) }
313
+ return json_accept || accepts.join(',')
314
+ end
315
+
316
+ # Return Content-Type header based on an array of content types provided.
317
+ # @param [Array] content_types array for Content-Type
318
+ # @return [String] the Content-Type header (e.g. application/json)
319
+ def select_header_content_type(content_types)
320
+ # use application/json by default
321
+ return 'application/json' if content_types.nil? || content_types.empty?
322
+ # use JSON when present, otherwise use the first one
323
+ json_content_type = content_types.find { |s| json_mime?(s) }
324
+ return json_content_type || content_types.first
325
+ end
326
+
327
+ # Convert object (array, hash, object, etc) to JSON string.
328
+ # @param [Object] model object to be converted into JSON string
329
+ # @return [String] JSON string representation of the object
330
+ def object_to_http_body(model)
331
+ return model if model.nil? || model.is_a?(String)
332
+ local_body = nil
333
+ if model.is_a?(Array)
334
+ local_body = model.map{|m| object_to_hash(m) }
335
+ else
336
+ local_body = object_to_hash(model)
337
+ end
338
+ local_body.to_json
339
+ end
340
+
341
+ # Convert object(non-array) to hash.
342
+ # @param [Object] obj object to be converted into JSON string
343
+ # @return [String] JSON string representation of the object
344
+ def object_to_hash(obj)
345
+ if obj.respond_to?(:to_hash)
346
+ obj.to_hash
347
+ else
348
+ obj
349
+ end
350
+ end
351
+
352
+ # Build parameter value according to the given collection format.
353
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
354
+ def build_collection_param(param, collection_format)
355
+ case collection_format
356
+ when :csv
357
+ param.join(',')
358
+ when :ssv
359
+ param.join(' ')
360
+ when :tsv
361
+ param.join("\t")
362
+ when :pipes
363
+ param.join('|')
364
+ when :multi
365
+ # return the array directly as typhoeus will handle it as expected
366
+ param
367
+ else
368
+ fail "unknown collection format: #{collection_format.inspect}"
369
+ end
370
+ end
371
+ end
372
+ end