lib/ff/ruby/server/sdk/connector/harness_connector.rb in ff-ruby-server-sdk-1.0.6 vs lib/ff/ruby/server/sdk/connector/harness_connector.rb in ff-ruby-server-sdk-1.1.0

- old
+ new

@@ -11,10 +11,11 @@ @api_key = api_key @config = config @on_unauthorized = on_unauthorized @user_agent = "RubySDK " + Ff::Ruby::Server::Sdk::VERSION + @sdk_info = "Ruby #{Ff::Ruby::Server::Sdk::VERSION} Server" @api = OpenapiClient::ClientApi.new(make_api_client) @metrics_api = OpenapiClient::MetricsApi.new(make_metrics_api_client) @config.logger.debug "Api: " + @api.to_s @@ -34,18 +35,25 @@ response = @api.authenticate(opts = options) @token = response.auth_token @config.logger.info "Token has been obtained" process_token - return true + return 200 rescue OpenapiClient::ApiError => e + if e.message.include? "the server returns an error" + # NOTE openapi-generator 5.2.1 has a bug where exceptions don't contain any useful information and we can't + # determine if a timeout has occurred. This is fixed in 6.3.0 but requires Ruby version to be increased to 2.7 + # https://github.com/OpenAPITools/openapi-generator/releases/tag/v6.3.0 + @config.logger.warn "OpenapiClient::ApiError [\n\n#{e}\n]" + return -1 + end + log_error(e) + return e.code end - - false end def get_flags begin @@ -57,10 +65,11 @@ ) rescue OpenapiClient::ApiError => e log_error(e) + return nil end end def get_segments @@ -73,10 +82,11 @@ ) rescue OpenapiClient::ApiError => e log_error(e) + return nil end end def get_flag(identifier) @@ -137,19 +147,23 @@ url = @config.config_url + "/stream?cluster=" + @cluster.to_s headers = { "Authorization" => "Bearer " + @token, - "API-Key" => @api_key - } + "API-Key" => @api_key, + "User-Agent" => @user_agent, + "Harness-SDK-Info" => @sdk_info, + "Harness-AccountID" => @account_id, + "Harness-EnvironmentID" => @environment_id + }.compact @event_source = Events.new( url, headers, updater, - @config.logger + @config ) @event_source end @@ -167,11 +181,14 @@ def make_api_client api_client = OpenapiClient::ApiClient.new api_client.config = @config + api_client.config.connection_timeout = @config.read_timeout / 1000 + api_client.config.read_timeout = @config.read_timeout / 1000 api_client.user_agent = @user_agent + api_client.default_headers['Harness-SDK-Info'] = @sdk_info api_client end def make_metrics_api_client @@ -187,34 +204,36 @@ config.connection_timeout = max_timeout config.config_url = config.event_url api_client.config = config api_client.user_agent = @user_agent + api_client.default_headers['Harness-SDK-Info'] = @sdk_info api_client end def process_token - - headers = { - - "Authorization" => "Bearer " + @token - } - - @api.api_client.default_headers = @api.api_client.default_headers.merge(headers) - @metrics_api.api_client.default_headers = @metrics_api.api_client.default_headers.merge(headers) - decoded_token = JWT.decode @token, nil, false if decoded_token != nil && !decoded_token.empty? @environment = decoded_token[0]["environment"] @cluster = decoded_token[0]["clusterIdentifier"] + @environment_id = decoded_token[0]["environmentIdentifier"] + @account_id = decoded_token[0]["accountID"] + headers = { + "Authorization" => "Bearer " + @token, + "Harness-AccountID" => @account_id, + "Harness-EnvironmentID" => @environment_id + }.compact + + @api.api_client.default_headers = @api.api_client.default_headers.merge(headers) + @metrics_api.api_client.default_headers = @metrics_api.api_client.default_headers.merge(headers) + @config.logger.debug "Token has been processed: environment='" + @environment.to_s + "', cluster='" + @cluster.to_s + "'" else - @config.logger.error "ERROR: Could not obtain the environment and cluster data from the token" end end private @@ -229,8 +248,14 @@ } end def log_error(e) - @config.logger.error "ERROR - Start\n\n" + e.to_s + "\nERROR - End" + if e.code == 0 + type = "typhoeus/libcurl" + else + type = "HTTP code #{e.code}" + end + + @config.logger.warn "OpenapiClient::ApiError (#{type}) [\n\n" + e.to_s + "\n]" end end \ No newline at end of file