dkron-ruby 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 (44) hide show
  1. checksums.yaml +7 -0
  2. data/DEVELOP.md +596 -0
  3. data/Gemfile +7 -0
  4. data/Gemfile.lock +69 -0
  5. data/README.md +99 -0
  6. data/Rakefile +8 -0
  7. data/dkron-ruby.gemspec +45 -0
  8. data/docs/DefaultApi.md +131 -0
  9. data/docs/Execution.md +13 -0
  10. data/docs/ExecutionsApi.md +55 -0
  11. data/docs/Job.md +28 -0
  12. data/docs/JobsApi.md +295 -0
  13. data/docs/Member.md +18 -0
  14. data/docs/MembersApi.md +49 -0
  15. data/docs/Processors.md +7 -0
  16. data/docs/Status.md +10 -0
  17. data/git_push.sh +55 -0
  18. data/lib/dkron-ruby.rb +48 -0
  19. data/lib/dkron-ruby/api/default_api.rb +161 -0
  20. data/lib/dkron-ruby/api/executions_api.rb +75 -0
  21. data/lib/dkron-ruby/api/jobs_api.rb +335 -0
  22. data/lib/dkron-ruby/api/members_api.rb +69 -0
  23. data/lib/dkron-ruby/api_client.rb +390 -0
  24. data/lib/dkron-ruby/api_error.rb +38 -0
  25. data/lib/dkron-ruby/configuration.rb +202 -0
  26. data/lib/dkron-ruby/models/execution.rb +236 -0
  27. data/lib/dkron-ruby/models/job.rb +403 -0
  28. data/lib/dkron-ruby/models/member.rb +288 -0
  29. data/lib/dkron-ruby/models/processors.rb +176 -0
  30. data/lib/dkron-ruby/models/status.rb +206 -0
  31. data/lib/dkron-ruby/version.rb +15 -0
  32. data/spec/api/default_api_spec.rb +65 -0
  33. data/spec/api/executions_api_spec.rb +46 -0
  34. data/spec/api/jobs_api_spec.rb +102 -0
  35. data/spec/api/members_api_spec.rb +45 -0
  36. data/spec/api_client_spec.rb +243 -0
  37. data/spec/configuration_spec.rb +42 -0
  38. data/spec/models/execution_spec.rb +71 -0
  39. data/spec/models/job_spec.rb +161 -0
  40. data/spec/models/member_spec.rb +101 -0
  41. data/spec/models/processors_spec.rb +35 -0
  42. data/spec/models/status_spec.rb +53 -0
  43. data/spec/spec_helper.rb +111 -0
  44. metadata +283 -0
@@ -0,0 +1,69 @@
1
+ =begin
2
+ #Dkron REST API
3
+
4
+ #You can communicate with Dkron using a RESTful JSON API over HTTP. Dkron nodes usually listen on port `8080` for API requests. All examples in this section assume that you've found a running leader at `localhost:8080`. Dkron implements a RESTful JSON API over HTTP to communicate with software clients. Dkron listens in port `8080` by default. All examples in this section assume that you're using the default port. Default API responses are unformatted JSON add the `pretty=true` param to format the response.
5
+
6
+ OpenAPI spec version: 2.2
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+ Swagger Codegen version: 2.4.13
10
+
11
+ =end
12
+
13
+ require 'uri'
14
+
15
+ module Dkron
16
+ class MembersApi
17
+ attr_accessor :api_client
18
+
19
+ def initialize(api_client = ApiClient.default)
20
+ @api_client = api_client
21
+ end
22
+ # List members.
23
+ # @param [Hash] opts the optional parameters
24
+ # @return [Array<Member>]
25
+ def get_member(opts = {})
26
+ data, _status_code, _headers = get_member_with_http_info(opts)
27
+ data
28
+ end
29
+
30
+ # List members.
31
+ # @param [Hash] opts the optional parameters
32
+ # @return [Array<(Array<Member>, Fixnum, Hash)>] Array<Member> data, response status code and response headers
33
+ def get_member_with_http_info(opts = {})
34
+ if @api_client.config.debugging
35
+ @api_client.config.logger.debug 'Calling API: MembersApi.get_member ...'
36
+ end
37
+ # resource path
38
+ local_var_path = '/members'
39
+
40
+ # query parameters
41
+ query_params = {}
42
+
43
+ # header parameters
44
+ header_params = {}
45
+ # HTTP header 'Accept' (if needed)
46
+ header_params['Accept'] = @api_client.select_header_accept(['application/json'])
47
+ # HTTP header 'Content-Type'
48
+ header_params['Content-Type'] = @api_client.select_header_content_type(['application/json'])
49
+
50
+ # form parameters
51
+ form_params = {}
52
+
53
+ # http body (model)
54
+ post_body = nil
55
+ auth_names = []
56
+ data, status_code, headers = @api_client.call_api(:GET, local_var_path,
57
+ :header_params => header_params,
58
+ :query_params => query_params,
59
+ :form_params => form_params,
60
+ :body => post_body,
61
+ :auth_names => auth_names,
62
+ :return_type => 'Array<Member>')
63
+ if @api_client.config.debugging
64
+ @api_client.config.logger.debug "API called: MembersApi#get_member\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
65
+ end
66
+ return data, status_code, headers
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,390 @@
1
+ =begin
2
+ #Dkron REST API
3
+
4
+ #You can communicate with Dkron using a RESTful JSON API over HTTP. Dkron nodes usually listen on port `8080` for API requests. All examples in this section assume that you've found a running leader at `localhost:8080`. Dkron implements a RESTful JSON API over HTTP to communicate with software clients. Dkron listens in port `8080` by default. All examples in this section assume that you're using the default port. Default API responses are unformatted JSON add the `pretty=true` param to format the response.
5
+
6
+ OpenAPI spec version: 2.2
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+ Swagger Codegen version: 2.4.13
10
+
11
+ =end
12
+
13
+ require 'date'
14
+ require 'json'
15
+ require 'logger'
16
+ require 'tempfile'
17
+ require 'typhoeus'
18
+ require 'uri'
19
+
20
+ module Dkron
21
+ class ApiClient
22
+ # The Configuration object holding settings to be used in the API client.
23
+ attr_accessor :config
24
+
25
+ # Defines the headers to be used in HTTP requests of all API calls by default.
26
+ #
27
+ # @return [Hash]
28
+ attr_accessor :default_headers
29
+
30
+ # Initializes the ApiClient
31
+ # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
32
+ def initialize(config = Configuration.default)
33
+ @config = config
34
+ @user_agent = "Swagger-Codegen/#{VERSION}/ruby"
35
+ @default_headers = {
36
+ 'Content-Type' => 'application/json',
37
+ 'User-Agent' => @user_agent
38
+ }
39
+ end
40
+
41
+ def self.default
42
+ @@default ||= ApiClient.new
43
+ end
44
+
45
+ # Call an API with given options.
46
+ #
47
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
48
+ # the data deserialized from response body (could be nil), response status code and response headers.
49
+ def call_api(http_method, path, opts = {})
50
+ request = build_request(http_method, path, opts)
51
+ response = request.run
52
+
53
+ if @config.debugging
54
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
55
+ end
56
+
57
+ unless response.success?
58
+ if response.timed_out?
59
+ fail ApiError.new('Connection timed out')
60
+ elsif response.code == 0
61
+ # Errors from libcurl will be made visible here
62
+ fail ApiError.new(:code => 0,
63
+ :message => response.return_message)
64
+ else
65
+ fail ApiError.new(:code => response.code,
66
+ :response_headers => response.headers,
67
+ :response_body => response.body),
68
+ response.status_message
69
+ end
70
+ end
71
+
72
+ if opts[:return_type]
73
+ data = deserialize(response, opts[:return_type])
74
+ else
75
+ data = nil
76
+ end
77
+ return data, response.code, response.headers
78
+ end
79
+
80
+ # Builds the HTTP request
81
+ #
82
+ # @param [String] http_method HTTP method/verb (e.g. POST)
83
+ # @param [String] path URL path (e.g. /account/new)
84
+ # @option opts [Hash] :header_params Header parameters
85
+ # @option opts [Hash] :query_params Query parameters
86
+ # @option opts [Hash] :form_params Query parameters
87
+ # @option opts [Object] :body HTTP body (JSON/XML)
88
+ # @return [Typhoeus::Request] A Typhoeus Request
89
+ def build_request(http_method, path, opts = {})
90
+ url = build_request_url(path)
91
+ http_method = http_method.to_sym.downcase
92
+
93
+ header_params = @default_headers.merge(opts[:header_params] || {})
94
+ query_params = opts[:query_params] || {}
95
+ form_params = opts[:form_params] || {}
96
+
97
+
98
+ # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
99
+ _verify_ssl_host = @config.verify_ssl_host ? 2 : 0
100
+
101
+ req_opts = {
102
+ :method => http_method,
103
+ :headers => header_params,
104
+ :params => query_params,
105
+ :params_encoding => @config.params_encoding,
106
+ :timeout => @config.timeout,
107
+ :ssl_verifypeer => @config.verify_ssl,
108
+ :ssl_verifyhost => _verify_ssl_host,
109
+ :sslcert => @config.cert_file,
110
+ :sslkey => @config.key_file,
111
+ :verbose => @config.debugging
112
+ }
113
+
114
+ req_opts.merge!(multipart: true) if header_params['Content-Type'].start_with? "multipart/"
115
+
116
+ # set custom cert, if provided
117
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
118
+
119
+ if [:post, :patch, :put, :delete].include?(http_method)
120
+ req_body = build_request_body(header_params, form_params, opts[:body])
121
+ req_opts.update :body => req_body
122
+ if @config.debugging
123
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
124
+ end
125
+ end
126
+
127
+ request = Typhoeus::Request.new(url, req_opts)
128
+ download_file(request) if opts[:return_type] == 'File'
129
+ request
130
+ end
131
+
132
+ # Check if the given MIME is a JSON MIME.
133
+ # JSON MIME examples:
134
+ # application/json
135
+ # application/json; charset=UTF8
136
+ # APPLICATION/JSON
137
+ # */*
138
+ # @param [String] mime MIME
139
+ # @return [Boolean] True if the MIME is application/json
140
+ def json_mime?(mime)
141
+ (mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
142
+ end
143
+
144
+ # Deserialize the response to the given return type.
145
+ #
146
+ # @param [Response] response HTTP response
147
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
148
+ def deserialize(response, return_type)
149
+ body = response.body
150
+
151
+ # handle file downloading - return the File instance processed in request callbacks
152
+ # note that response body is empty when the file is written in chunks in request on_body callback
153
+ return @tempfile if return_type == 'File'
154
+
155
+ return nil if body.nil? || body.empty?
156
+
157
+ # return response body directly for String return type
158
+ return body if return_type == 'String'
159
+
160
+ # ensuring a default content type
161
+ content_type = response.headers['Content-Type'] || 'application/json'
162
+
163
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
164
+
165
+ begin
166
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
167
+ rescue JSON::ParserError => e
168
+ if %w(String Date DateTime).include?(return_type)
169
+ data = body
170
+ else
171
+ raise e
172
+ end
173
+ end
174
+
175
+ convert_to_type data, return_type
176
+ end
177
+
178
+ # Convert data to the given return type.
179
+ # @param [Object] data Data to be converted
180
+ # @param [String] return_type Return type
181
+ # @return [Mixed] Data in a particular type
182
+ def convert_to_type(data, return_type)
183
+ return nil if data.nil?
184
+ case return_type
185
+ when 'String'
186
+ data.to_s
187
+ when 'Integer'
188
+ data.to_i
189
+ when 'Float'
190
+ data.to_f
191
+ when 'BOOLEAN'
192
+ data == true
193
+ when 'DateTime'
194
+ # parse date time (expecting ISO 8601 format)
195
+ DateTime.parse data
196
+ when 'Date'
197
+ # parse date time (expecting ISO 8601 format)
198
+ Date.parse data
199
+ when 'Object'
200
+ # generic object (usually a Hash), return directly
201
+ data
202
+ when /\AArray<(.+)>\z/
203
+ # e.g. Array<Pet>
204
+ sub_type = $1
205
+ data.map { |item| convert_to_type(item, sub_type) }
206
+ when /\AHash\<String, (.+)\>\z/
207
+ # e.g. Hash<String, Integer>
208
+ sub_type = $1
209
+ {}.tap do |hash|
210
+ data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
211
+ end
212
+ else
213
+ # models, e.g. Pet
214
+ Dkron.const_get(return_type).new.tap do |model|
215
+ model.build_from_hash data
216
+ end
217
+ end
218
+ end
219
+
220
+ # Save response body into a file in (the defined) temporary folder, using the filename
221
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
222
+ # The response body is written to the file in chunks in order to handle files which
223
+ # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
224
+ # process can use.
225
+ #
226
+ # @see Configuration#temp_folder_path
227
+ def download_file(request)
228
+ tempfile = nil
229
+ encoding = nil
230
+ request.on_headers do |response|
231
+ content_disposition = response.headers['Content-Disposition']
232
+ if content_disposition && content_disposition =~ /filename=/i
233
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
234
+ prefix = sanitize_filename(filename)
235
+ else
236
+ prefix = 'download-'
237
+ end
238
+ prefix = prefix + '-' unless prefix.end_with?('-')
239
+ encoding = response.body.encoding
240
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
241
+ @tempfile = tempfile
242
+ end
243
+ request.on_body do |chunk|
244
+ chunk.force_encoding(encoding)
245
+ tempfile.write(chunk)
246
+ end
247
+ request.on_complete do |response|
248
+ tempfile.close
249
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
250
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
251
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
252
+ "explicitly with `tempfile.delete`"
253
+ end
254
+ end
255
+
256
+ # Sanitize filename by removing path.
257
+ # e.g. ../../sun.gif becomes sun.gif
258
+ #
259
+ # @param [String] filename the filename to be sanitized
260
+ # @return [String] the sanitized filename
261
+ def sanitize_filename(filename)
262
+ filename.gsub(/.*[\/\\]/, '')
263
+ end
264
+
265
+ def build_request_url(path)
266
+ # Add leading and trailing slashes to path
267
+ path = "/#{path}".gsub(/\/+/, '/')
268
+ URI.encode(@config.base_url + path)
269
+ end
270
+
271
+ # Builds the HTTP request body
272
+ #
273
+ # @param [Hash] header_params Header parameters
274
+ # @param [Hash] form_params Query parameters
275
+ # @param [Object] body HTTP body (JSON/XML)
276
+ # @return [String] HTTP body data in the form of string
277
+ def build_request_body(header_params, form_params, body)
278
+ # http form
279
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
280
+ header_params['Content-Type'] == 'multipart/form-data'
281
+ data = {}
282
+ form_params.each do |key, value|
283
+ case value
284
+ when ::File, ::Array, nil
285
+ # let typhoeus handle File, Array and nil parameters
286
+ data[key] = value
287
+ else
288
+ data[key] = value.to_s
289
+ end
290
+ end
291
+ elsif body
292
+ data = body.is_a?(String) ? body : body.to_json
293
+ else
294
+ data = nil
295
+ end
296
+ data
297
+ end
298
+
299
+ # Update hearder and query params based on authentication settings.
300
+ #
301
+ # @param [Hash] header_params Header parameters
302
+ # @param [Hash] query_params Query parameters
303
+ # @param [String] auth_names Authentication scheme name
304
+ def update_params_for_auth!(header_params, query_params, auth_names)
305
+ Array(auth_names).each do |auth_name|
306
+ auth_setting = @config.auth_settings[auth_name]
307
+ next unless auth_setting
308
+ case auth_setting[:in]
309
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
310
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
311
+ else fail ArgumentError, 'Authentication token must be in `query` of `header`'
312
+ end
313
+ end
314
+ end
315
+
316
+ # Sets user agent in HTTP header
317
+ #
318
+ # @param [String] user_agent User agent (e.g. swagger-codegen/ruby/1.0.0)
319
+ def user_agent=(user_agent)
320
+ @user_agent = user_agent
321
+ @default_headers['User-Agent'] = @user_agent
322
+ end
323
+
324
+ # Return Accept header based on an array of accepts provided.
325
+ # @param [Array] accepts array for Accept
326
+ # @return [String] the Accept header (e.g. application/json)
327
+ def select_header_accept(accepts)
328
+ return nil if accepts.nil? || accepts.empty?
329
+ # use JSON when present, otherwise use all of the provided
330
+ json_accept = accepts.find { |s| json_mime?(s) }
331
+ json_accept || accepts.join(',')
332
+ end
333
+
334
+ # Return Content-Type header based on an array of content types provided.
335
+ # @param [Array] content_types array for Content-Type
336
+ # @return [String] the Content-Type header (e.g. application/json)
337
+ def select_header_content_type(content_types)
338
+ # use application/json by default
339
+ return 'application/json' if content_types.nil? || content_types.empty?
340
+ # use JSON when present, otherwise use the first one
341
+ json_content_type = content_types.find { |s| json_mime?(s) }
342
+ json_content_type || content_types.first
343
+ end
344
+
345
+ # Convert object (array, hash, object, etc) to JSON string.
346
+ # @param [Object] model object to be converted into JSON string
347
+ # @return [String] JSON string representation of the object
348
+ def object_to_http_body(model)
349
+ return model if model.nil? || model.is_a?(String)
350
+ local_body = nil
351
+ if model.is_a?(Array)
352
+ local_body = model.map { |m| object_to_hash(m) }
353
+ else
354
+ local_body = object_to_hash(model)
355
+ end
356
+ local_body.to_json
357
+ end
358
+
359
+ # Convert object(non-array) to hash.
360
+ # @param [Object] obj object to be converted into JSON string
361
+ # @return [String] JSON string representation of the object
362
+ def object_to_hash(obj)
363
+ if obj.respond_to?(:to_hash)
364
+ obj.to_hash
365
+ else
366
+ obj
367
+ end
368
+ end
369
+
370
+ # Build parameter value according to the given collection format.
371
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
372
+ def build_collection_param(param, collection_format)
373
+ case collection_format
374
+ when :csv
375
+ param.join(',')
376
+ when :ssv
377
+ param.join(' ')
378
+ when :tsv
379
+ param.join("\t")
380
+ when :pipes
381
+ param.join('|')
382
+ when :multi
383
+ # return the array directly as typhoeus will handle it as expected
384
+ param
385
+ else
386
+ fail "unknown collection format: #{collection_format.inspect}"
387
+ end
388
+ end
389
+ end
390
+ end