lib/ionoscloud/api_client.rb in ionoscloud-5.1.2 vs lib/ionoscloud/api_client.rb in ionoscloud-6.0.0.beta.1

- old
+ new

@@ -1,31 +1,26 @@ =begin #CLOUD API #An enterprise-grade Infrastructure is provided as a Service (IaaS) solution that can be managed through a browser-based \"Data Center Designer\" (DCD) tool or via an easy to use API. The API allows you to perform a variety of management tasks such as spinning up additional servers, adding volumes, adjusting networking, and so forth. It is designed to allow users to leverage the same power and flexibility found within the DCD visual tool. Both tools are consistent with their concepts and lend well to making the experience smooth and intuitive. -The version of the OpenAPI document: 5.0 +The version of the OpenAPI document: 6.0 Generated by: https://openapi-generator.tech -OpenAPI Generator version: 5.2.1-SNAPSHOT +OpenAPI Generator version: 5.0.1-SNAPSHOT =end require 'date' require 'json' require 'logger' +require 'net/http' +require 'set' require 'tempfile' require 'time' require 'typhoeus' - -TooManyRequests = 429 -BadGateway = 502 -ServiceUnavailable = 503 -GatewayTimeout = 504 - - module Ionoscloud class ApiClient # The Configuration object holding settings to be used in the API client. attr_accessor :config @@ -36,11 +31,11 @@ # Initializes the ApiClient # @option config [Configuration] Configuration for initializing the object, default to Configuration.default def initialize(config = Configuration.default) @config = config - @user_agent = "ionos-cloud-sdk-ruby/5.1.2" + @user_agent = "ionos-cloud-sdk-ruby/v6" @default_headers = { 'Content-Type' => 'application/json', 'User-Agent' => @user_agent } end @@ -52,11 +47,10 @@ # Call an API with given options. # # @return [Array<(Object, Integer, Hash)>] an array of 3 elements: # the data deserialized from response body (could be nil), response status code and response headers. def call_api(http_method, path, opts = {}) - retry_count = 0 request = build_request(http_method, path, opts) while true retry_count += 1 @@ -64,18 +58,18 @@ if @config.debugging @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n" end - case response.code - when BadGateway, ServiceUnavailable, GatewayTimeout + case Net::HTTPResponse::CODE_TO_OBJ[response.code.to_s] + when Set[Net::HTTPBadGateway, Net::HTTPServiceUnavailable, Net::HTTPGatewayTimeout] backoff_time = @config.wait_time - when TooManyRequests + when Set[Net::HTTPTooManyRequests] backoff_time = @config.wait_time begin backoff_time = Integer(response.headers['Retry-After']) - rescue *[ArgumentError, TypeError] + rescue ArgumentError end else break end @@ -437,31 +431,35 @@ # @param [Integer] scaleup Double polling interval every scaleup steps, which will be doubled. def wait_for_completion(request_id, timeout = 3600, initial_wait = 5, scaleup = 10) if request_id.nil? return nil end - + wait_period = initial_wait next_increase = Time.now.to_i + wait_period * scaleup unless timeout.nil? timeout = Time.now.to_i + timeout end while true - request = Ionoscloud::RequestApi.new(self).requests_status_get(request_id) + request = Ionoscloud::RequestsApi.new(self).requests_status_get(request_id) if request.metadata.status == 'DONE' break else if request.metadata.status == 'FAILED' - raise Ionoscloud::ApiError.new(message: "Request #{request_id} failed to complete: #{request.metadata.message}") + raise Ionoscloud::ApiError.new( + 'Request {0} failed to complete: {1}'.format(request_id, request.metadata.message), + ) end end current_time = Time.now.to_i if timeout && current_time > timeout - raise Ionoscloud::ApiError.new(message: "Timed out waiting for request #{request_id}.") + raise Ionoscloud::ApiError.new( + 'Timed out waiting for request {0}.'.format(request_id), + ) end if current_time > next_increase wait_period *= 2 next_increase = Time.now.to_i + wait_period * scaleup @@ -483,11 +481,11 @@ # @param [Integer] scaleup Double polling interval every scaleup steps, which will be doubled. def wait_for(timeout = 3600, initial_wait = 5, scaleup = 10, &_block) if _block.nil? return nil end - + wait_period = initial_wait next_increase = Time.now.to_i + wait_period * scaleup unless timeout.nil? timeout = Time.now.to_i + timeout end @@ -497,10 +495,12 @@ break end current_time = Time.now.to_i if timeout && current_time > timeout - raise Ionoscloud::ApiError.new(message: "Timed out.") + raise Ionoscloud::ApiError.new( + 'Timed out waiting for request {0}.'.format(request_id), + ) end if current_time > next_increase wait_period *= 2 next_increase = Time.now.to_i + wait_period * scaleup