statuscake-rb 1.0.0.pre.beta.4

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 (58) hide show
  1. checksums.yaml +7 -0
  2. data/lib/statuscake/api/contact_groups_api.rb +411 -0
  3. data/lib/statuscake/api/locations_api.rb +166 -0
  4. data/lib/statuscake/api/maintenance_windows_api.rb +444 -0
  5. data/lib/statuscake/api/pagespeed_api.rb +550 -0
  6. data/lib/statuscake/api/ssl_api.rb +458 -0
  7. data/lib/statuscake/api/uptime_api.rb +881 -0
  8. data/lib/statuscake/api_client.rb +407 -0
  9. data/lib/statuscake/api_error.rb +75 -0
  10. data/lib/statuscake/configuration.rb +296 -0
  11. data/lib/statuscake/models/api_error.rb +249 -0
  12. data/lib/statuscake/models/api_response.rb +241 -0
  13. data/lib/statuscake/models/api_response_data.rb +243 -0
  14. data/lib/statuscake/models/contact_group.rb +318 -0
  15. data/lib/statuscake/models/contact_group_response.rb +241 -0
  16. data/lib/statuscake/models/contact_groups.rb +258 -0
  17. data/lib/statuscake/models/links.rb +242 -0
  18. data/lib/statuscake/models/maintenance_window.rb +364 -0
  19. data/lib/statuscake/models/maintenance_window_repeat_interval.rb +59 -0
  20. data/lib/statuscake/models/maintenance_window_response.rb +241 -0
  21. data/lib/statuscake/models/maintenance_window_state.rb +57 -0
  22. data/lib/statuscake/models/maintenance_windows.rb +258 -0
  23. data/lib/statuscake/models/monitoring_location.rb +306 -0
  24. data/lib/statuscake/models/monitoring_location_status.rb +56 -0
  25. data/lib/statuscake/models/monitoring_locations.rb +244 -0
  26. data/lib/statuscake/models/pagespeed_test.rb +444 -0
  27. data/lib/statuscake/models/pagespeed_test_check_rate.rb +61 -0
  28. data/lib/statuscake/models/pagespeed_test_history.rb +269 -0
  29. data/lib/statuscake/models/pagespeed_test_history_result.rb +373 -0
  30. data/lib/statuscake/models/pagespeed_test_region.rb +64 -0
  31. data/lib/statuscake/models/pagespeed_test_response.rb +241 -0
  32. data/lib/statuscake/models/pagespeed_test_stats.rb +354 -0
  33. data/lib/statuscake/models/pagespeed_test_throttling.rb +60 -0
  34. data/lib/statuscake/models/pagespeed_tests.rb +258 -0
  35. data/lib/statuscake/models/pagination.rb +363 -0
  36. data/lib/statuscake/models/ssl_test.rb +594 -0
  37. data/lib/statuscake/models/ssl_test_check_rate.rb +60 -0
  38. data/lib/statuscake/models/ssl_test_flags.rb +347 -0
  39. data/lib/statuscake/models/ssl_test_mixed_content.rb +257 -0
  40. data/lib/statuscake/models/ssl_test_response.rb +241 -0
  41. data/lib/statuscake/models/ssl_tests.rb +258 -0
  42. data/lib/statuscake/models/uptime_test.rb +811 -0
  43. data/lib/statuscake/models/uptime_test_alert.rb +300 -0
  44. data/lib/statuscake/models/uptime_test_alerts.rb +269 -0
  45. data/lib/statuscake/models/uptime_test_check_rate.rb +62 -0
  46. data/lib/statuscake/models/uptime_test_history.rb +269 -0
  47. data/lib/statuscake/models/uptime_test_history_result.rb +302 -0
  48. data/lib/statuscake/models/uptime_test_overview.rb +388 -0
  49. data/lib/statuscake/models/uptime_test_period.rb +291 -0
  50. data/lib/statuscake/models/uptime_test_periods.rb +269 -0
  51. data/lib/statuscake/models/uptime_test_processing_state.rb +58 -0
  52. data/lib/statuscake/models/uptime_test_response.rb +241 -0
  53. data/lib/statuscake/models/uptime_test_status.rb +56 -0
  54. data/lib/statuscake/models/uptime_test_type.rb +61 -0
  55. data/lib/statuscake/models/uptime_tests.rb +258 -0
  56. data/lib/statuscake/version.rb +32 -0
  57. data/lib/statuscake.rb +107 -0
  58. metadata +221 -0
@@ -0,0 +1,407 @@
1
+ # frozen_string_literal: true
2
+
3
+ # StatusCake API
4
+ #
5
+ # Copyright (c) 2022
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to
9
+ # deal in the Software without restriction, including without limitation the
10
+ # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11
+ # sell copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in
15
+ # all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
+ # IN THE SOFTWARE.
24
+ #
25
+ # API version: 1.0.0
26
+ # Contact: support@statuscake.com
27
+ #
28
+ # Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
29
+
30
+ require 'date'
31
+ require 'json'
32
+ require 'logger'
33
+ require 'tempfile'
34
+ require 'time'
35
+ require 'typhoeus'
36
+
37
+ module StatusCake
38
+ # :nodoc
39
+ class ApiClient
40
+ # The Configuration object holding settings to be used in the API client.
41
+ attr_accessor :config
42
+
43
+ # Defines the headers to be used in HTTP requests of all API calls by default.
44
+ #
45
+ # @return [Hash]
46
+ attr_accessor :default_headers
47
+
48
+ # Initializes the ApiClient
49
+ # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
50
+ def initialize(config = Configuration.default)
51
+ @config = config
52
+ @user_agent = "statuscake/rb"
53
+ @default_headers = {
54
+ 'Content-Type' => 'application/json',
55
+ 'User-Agent' => @user_agent,
56
+ }
57
+ end
58
+
59
+ def self.default
60
+ @default ||= ApiClient.new
61
+ end
62
+
63
+ # Call an API with given options.
64
+ #
65
+ # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
66
+ # the data deserialized from response body (could be nil), response status code and response headers.
67
+ def call_api(http_method, path, opts = {})
68
+ request = build_request(http_method, path, opts)
69
+ response = request.run
70
+
71
+ if @config.debugging
72
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
73
+ end
74
+
75
+ unless response.success?
76
+ if response.timed_out?
77
+ raise StandardError, ApiError.new('Connection timed out')
78
+ elsif response.code == 0
79
+ # Errors from libcurl will be made visible here
80
+ raise StandardError, ApiError.new(:code => 0,
81
+ :message => response.return_message)
82
+ else
83
+ raise StandardError, ApiError.new(:code => response.code,
84
+ :response_headers => response.headers,
85
+ :response_body => response.body)
86
+ end
87
+ end
88
+
89
+ data = if opts[:return_type]
90
+ deserialize(response, opts[:return_type])
91
+ end
92
+
93
+ [data, response.code, response.headers]
94
+ end
95
+
96
+ # Builds the HTTP request
97
+ #
98
+ # @param [String] http_method HTTP method/verb (e.g. POST)
99
+ # @param [String] path URL path (e.g. /account/new)
100
+ # @option opts [Hash] :header_params Header parameters
101
+ # @option opts [Hash] :query_params Query parameters
102
+ # @option opts [Hash] :form_params Query parameters
103
+ # @option opts [Object] :body HTTP body (JSON/XML)
104
+ # @return [Typhoeus::Request] A Typhoeus Request
105
+ def build_request(http_method, path, opts = {})
106
+ url = build_request_url(path, opts)
107
+ http_method = http_method.to_sym.downcase
108
+
109
+ header_params = @default_headers.merge(opts[:header_params] || {})
110
+ query_params = opts[:query_params] || {}
111
+ form_params = opts[:form_params] || {}
112
+ follow_location = opts[:follow_location] || true
113
+
114
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
115
+
116
+ # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
117
+ verify_ssl_host = @config.verify_ssl_host ? 2 : 0
118
+
119
+ req_opts = {
120
+ :method => http_method,
121
+ :headers => header_params,
122
+ :params => query_params,
123
+ :params_encoding => @config.params_encoding,
124
+ :timeout => @config.timeout,
125
+ :ssl_verifypeer => @config.verify_ssl,
126
+ :ssl_verifyhost => verify_ssl_host,
127
+ :sslcert => @config.cert_file,
128
+ :sslkey => @config.key_file,
129
+ :verbose => @config.debugging,
130
+ :followlocation => follow_location,
131
+ }
132
+
133
+ # set custom cert, if provided
134
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
135
+
136
+ if %i[post patch put delete].include?(http_method)
137
+ req_body = build_request_body(header_params, form_params, opts[:body])
138
+ req_opts.update :body => req_body
139
+ if @config.debugging
140
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
141
+ end
142
+ end
143
+
144
+ request = Typhoeus::Request.new(url, req_opts)
145
+ download_file(request) if opts[:return_type] == 'File'
146
+ request
147
+ end
148
+
149
+ # Builds the HTTP request body
150
+ #
151
+ # @param [Hash] header_params Header parameters
152
+ # @param [Hash] form_params Query parameters
153
+ # @param [Object] body HTTP body (JSON/XML)
154
+ # @return [String] HTTP body data in the form of string
155
+ def build_request_body(header_params, form_params, body)
156
+ # http form
157
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
158
+ header_params['Content-Type'] == 'multipart/form-data'
159
+ data = {}
160
+ form_params.each do |key, value|
161
+ data[key] = case value
162
+ when ::File, ::Array, nil
163
+ # let typhoeus handle File, Array and nil parameters
164
+ value
165
+ else
166
+ value.to_s
167
+ end
168
+ end
169
+ elsif body
170
+ data = body.is_a?(String) ? body : body.to_json
171
+ else
172
+ data = nil
173
+ end
174
+ data
175
+ end
176
+
177
+ # Save response body into a file in (the defined) temporary folder, using the filename
178
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
179
+ # The response body is written to the file in chunks in order to handle files which
180
+ # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
181
+ # process can use.
182
+ #
183
+ # @see Configuration#temp_folder_path
184
+ def download_file(request)
185
+ tempfile = nil
186
+ encoding = nil
187
+ request.on_headers do |response|
188
+ content_disposition = response.headers['Content-Disposition']
189
+ if content_disposition && content_disposition =~ /filename=/i
190
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
191
+ prefix = sanitize_filename(filename)
192
+ else
193
+ prefix = 'download-'
194
+ end
195
+ prefix += '-' unless prefix.end_with?('-')
196
+ encoding = response.body.encoding
197
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
198
+ @tempfile = tempfile
199
+ end
200
+ request.on_body do |chunk|
201
+ chunk.force_encoding(encoding)
202
+ tempfile.write(chunk)
203
+ end
204
+ request.on_complete do |_|
205
+ if tempfile
206
+ tempfile.close
207
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder " \
208
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file " \
209
+ "will be deleted automatically with GC. It's also recommended to delete the temp file " \
210
+ "explicitly with `tempfile.delete`"
211
+ end
212
+ end
213
+ end
214
+
215
+ # Check if the given MIME is a JSON MIME.
216
+ # JSON MIME examples:
217
+ # application/json
218
+ # application/json; charset=UTF8
219
+ # APPLICATION/JSON
220
+ # */*
221
+ # @param [String] mime MIME
222
+ # @return [Boolean] True if the MIME is application/json
223
+ def json_mime?(mime)
224
+ (mime == '*/*') || !(mime =~ %r{Application/.*json(?!p)(;.*)?}i).nil?
225
+ end
226
+
227
+ # Deserialize the response to the given return type.
228
+ #
229
+ # @param [Response] response HTTP response
230
+ # @param [String] return_type some examples: "User", "Array<User>", "Hash<String, Integer>"
231
+ def deserialize(response, return_type)
232
+ body = response.body
233
+
234
+ # handle file downloading - return the File instance processed in request callbacks
235
+ # note that response body is empty when the file is written in chunks in request on_body callback
236
+ return @tempfile if return_type == 'File'
237
+
238
+ return nil if body.nil? || body.empty?
239
+
240
+ # return response body directly for String return type
241
+ return body if return_type == 'String'
242
+
243
+ # ensuring a default content type
244
+ content_type = response.headers['Content-Type'] || 'application/json'
245
+
246
+ raise ArgumentError, "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
247
+
248
+ begin
249
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
250
+ rescue JSON::ParserError => e
251
+ if %w[String Date Time].include?(return_type)
252
+ data = body
253
+ else
254
+ raise e
255
+ end
256
+ end
257
+
258
+ convert_to_type data, return_type
259
+ end
260
+
261
+ # Convert data to the given return type.
262
+ # @param [Object] data Data to be converted
263
+ # @param [String] return_type Return type
264
+ # @return [Mixed] Data in a particular type
265
+ def convert_to_type(data, return_type)
266
+ return nil if data.nil?
267
+ case return_type
268
+ when 'String'
269
+ data.to_s
270
+ when 'Integer'
271
+ data.to_i
272
+ when 'Float'
273
+ data.to_f
274
+ when 'Boolean'
275
+ data == true
276
+ when 'Time'
277
+ # parse date time (expecting ISO 8601 format)
278
+ Time.parse data
279
+ when 'Date'
280
+ # parse date time (expecting ISO 8601 format)
281
+ Date.parse data
282
+ when 'Object'
283
+ # generic object (usually a Hash), return directly
284
+ data
285
+ when /\AArray<(.+)>\z/
286
+ # e.g. Array<Pet>
287
+ sub_type = Regexp.last_match(1)
288
+ data.map { |item| convert_to_type(item, sub_type) }
289
+ when /\AHash<String, (.+)>\z/
290
+ # e.g. Hash<String, Integer>
291
+ sub_type = Regexp.last_match(1)
292
+ {}.tap do |hash|
293
+ data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
294
+ end
295
+ else
296
+ # models (e.g. Pet) or oneOf
297
+ klass = StatusCake.const_get(return_type)
298
+ klass.respond_to?(:openapi_one_of) ? klass.build(data) : klass.build_from_hash(data)
299
+ end
300
+ end
301
+
302
+ # Sanitize filename by removing path.
303
+ # e.g. ../../sun.gif becomes sun.gif
304
+ #
305
+ # @param [String] filename the filename to be sanitized
306
+ # @return [String] the sanitized filename
307
+ def sanitize_filename(filename)
308
+ filename.gsub(%r{.*[/\\]}, '')
309
+ end
310
+
311
+ def build_request_url(path, opts = {})
312
+ # Add leading and trailing slashes to path
313
+ path = "/#{path}".gsub(%r{/+}, '/')
314
+ @config.base_url(opts[:operation]) + path
315
+ end
316
+
317
+ # Update header and query params based on authentication settings.
318
+ #
319
+ # @param [Hash] header_params Header parameters
320
+ # @param [Hash] query_params Query parameters
321
+ # @param [String] auth_names Authentication scheme name
322
+ def update_params_for_auth!(header_params, query_params, auth_names)
323
+ Array(auth_names).each do |auth_name|
324
+ auth_setting = @config.auth_settings[auth_name]
325
+ next unless auth_setting
326
+ case auth_setting[:in]
327
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
328
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
329
+ else raise ArgumentError, 'Authentication token must be in `query` or `header`'
330
+ end
331
+ end
332
+ end
333
+
334
+ # Sets user agent in HTTP header
335
+ #
336
+ # @param [String] user_agent User agent (e.g. openapi-generator/ruby/1.0.0)
337
+ def user_agent=(user_agent)
338
+ @user_agent = user_agent
339
+ @default_headers['User-Agent'] = @user_agent
340
+ end
341
+
342
+ # Return Accept header based on an array of accepts provided.
343
+ # @param [Array] accepts array for Accept
344
+ # @return [String] the Accept header (e.g. application/json)
345
+ def select_header_accept(accepts)
346
+ return nil if accepts.nil? || accepts.empty?
347
+ # use JSON when present, otherwise use all of the provided
348
+ json_accept = accepts.find { |s| json_mime?(s) }
349
+ json_accept || accepts.join(',')
350
+ end
351
+
352
+ # Return Content-Type header based on an array of content types provided.
353
+ # @param [Array] content_types array for Content-Type
354
+ # @return [String] the Content-Type header (e.g. application/json)
355
+ def select_header_content_type(content_types)
356
+ # return nil by default
357
+ return if content_types.nil? || content_types.empty?
358
+ # use JSON when present, otherwise use the first one
359
+ json_content_type = content_types.find { |s| json_mime?(s) }
360
+ json_content_type || content_types.first
361
+ end
362
+
363
+ # Convert object (array, hash, object, etc) to JSON string.
364
+ # @param [Object] model object to be converted into JSON string
365
+ # @return [String] JSON string representation of the object
366
+ def object_to_http_body(model)
367
+ return model if model.nil? || model.is_a?(String)
368
+ local_body = if model.is_a?(Array)
369
+ model.map { |m| object_to_hash(m) }
370
+ else
371
+ object_to_hash(model)
372
+ end
373
+ local_body.to_json
374
+ end
375
+
376
+ # Convert object(non-array) to hash.
377
+ # @param [Object] obj object to be converted into JSON string
378
+ # @return [String] JSON string representation of the object
379
+ def object_to_hash(obj)
380
+ if obj.respond_to?(:to_hash)
381
+ obj.to_hash
382
+ else
383
+ obj
384
+ end
385
+ end
386
+
387
+ # Build parameter value according to the given collection format.
388
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
389
+ def build_collection_param(param, collection_format)
390
+ case collection_format
391
+ when :csv
392
+ param.join(',')
393
+ when :ssv
394
+ param.join(' ')
395
+ when :tsv
396
+ param.join("\t")
397
+ when :pipes
398
+ param.join('|')
399
+ when :multi
400
+ # return the array directly as typhoeus will handle it as expected
401
+ param
402
+ else
403
+ raise ArgumentError, "unknown collection format: #{collection_format.inspect}"
404
+ end
405
+ end
406
+ end
407
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ # StatusCake API
4
+ #
5
+ # Copyright (c) 2022
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to
9
+ # deal in the Software without restriction, including without limitation the
10
+ # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11
+ # sell copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in
15
+ # all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
+ # IN THE SOFTWARE.
24
+ #
25
+ # API version: 1.0.0
26
+ # Contact: support@statuscake.com
27
+ #
28
+ # Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
29
+
30
+ module StatusCake
31
+ # :nodoc
32
+ class ApiError < StandardError
33
+ attr_reader :code, :response_headers, :response_body
34
+
35
+ # Usage examples:
36
+ # ApiError.new
37
+ # ApiError.new("message")
38
+ # ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
39
+ # ApiError.new(:code => 404, :message => "Not Found")
40
+ def initialize(arg = nil)
41
+ if arg.is_a? Hash
42
+ if arg.key?(:message) || arg.key?('message')
43
+ super(arg[:message] || arg['message'])
44
+ else
45
+ super arg
46
+ end
47
+
48
+ arg.each do |k, v|
49
+ instance_variable_set "@#{k}", v
50
+ end
51
+ else
52
+ super arg
53
+ end
54
+ end
55
+
56
+ # Override to_s to display a friendly error message
57
+ def to_s
58
+ message
59
+ end
60
+
61
+ def message
62
+ msg = if @message.nil?
63
+ "Error message: the server returns an error"
64
+ else
65
+ @message
66
+ end
67
+
68
+ msg += "\nHTTP status code: #{code}" if code
69
+ msg += "\nResponse headers: #{response_headers}" if response_headers
70
+ msg += "\nResponse body: #{response_body}" if response_body
71
+
72
+ msg
73
+ end
74
+ end
75
+ end