Class: IbmAppconfigurationRubySdk::ConfigFetcher
- Inherits:
-
Object
- Object
- IbmAppconfigurationRubySdk::ConfigFetcher
- Includes:
- Utils
- Defined in:
- lib/ibm_appconfiguration_ruby_sdk/config_fetcher.rb
Constant Summary collapse
- IBM_ERROR_CODE_TO_HTTP_STATUS =
Maps IBM App Configuration error codes to their HTTP equivalents. The IBM Cloud SDK Core gem puts the service's errorCode string in ApiException#code rather than the HTTP status integer.
{ # 404 — resource not found "FTEC1000E" => 404, # Collection not found "FTEC1001E" => 404, # Environment not found "FTEC1002E" => 404, # Feature not found "FTEC1003E" => 404, # Property not found # 401 — authentication "BXNIM0415E" => 401, # API key not found "BXNIM0106E" => 401, # Token expired # 429 — rate limit "FTEC4290E" => 429 }.freeze
Instance Method Summary collapse
-
#fetch ⇒ Hash
Fetch configuration from API.
-
#initialize(collection_id:, environment_id:, handler:, logger: nil) ⇒ ConfigFetcher
constructor
Initialize the config fetcher.
-
#load_configurations_to_cache(data) ⇒ Boolean
Load configurations to cache.
-
#process_and_load_configurations(api_response) ⇒ Boolean
Process API response and load to cache.
Methods included from Utils
append_segment_id, compute_hash, extract_configurations, extract_environment_data, extract_resources, get_current_rollout_percentage, get_normalized_value, parse_rollout_configuration_phases, symbolize_keys, validate_resource
Constructor Details
#initialize(collection_id:, environment_id:, handler:, logger: nil) ⇒ ConfigFetcher
Initialize the config fetcher
55 56 57 58 59 60 |
# File 'lib/ibm_appconfiguration_ruby_sdk/config_fetcher.rb', line 55 def initialize(collection_id:, environment_id:, handler:, logger: nil) @collection_id = collection_id @environment_id = environment_id @handler = handler @logger = logger || Logger.instance end |
Instance Method Details
#fetch ⇒ Hash
Fetch configuration from API
Makes a direct API call to the /config endpoint Returns a hash with status information
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/ibm_appconfiguration_ruby_sdk/config_fetcher.rb', line 68 def fetch # Force the IAM token manager to check expiry and refresh before every # config GET. Without this, long-running processes reuse a cached token # that the server rejects with 401 once it expires (~1 hour). ApiManager.refresh_token! # Get the BaseService client client = ApiManager.base_service_client url_builder = UrlBuilder.instance # Build the API endpoint URL api_path = "/apprapp/feature/v1/instances/#{url_builder.guid}/config" @logger.info("Calling API: #{url_builder.base_service_url}#{api_path}") # Make the API request response = client.request( method: "GET", url: api_path, headers: ApiManager.headers, params: { action: "sdkConfig", collection_id: @collection_id, environment_id: @environment_id } ) # Success case if response.status == 200 @logger.info(Constants::CONFIG_API_CALL_SUCCESS) { ok: true, retryable: false, status: 200, data: response.result } else # Unexpected status code @logger.warning("Unexpected status code: #{response.status}") { ok: false, retryable: true, status: response.status, data: nil } end rescue IBMCloudSdkCore::ApiException => e # e.code is the IBM error-code string (e.g. "FTEC1000E"), not the HTTP status. # Map known IBM error codes to HTTP statuses; fall back to parsing as integer. http_status = IBM_ERROR_CODE_TO_HTTP_STATUS.fetch(e.code.to_s, e.code.to_i) # If still 0, infer from the error message text if http_status.zero? http_status = case e.error.to_s when /not found/i, /not available/i then 404 when /unauthorized/i, /not authorized/i then 401 when /forbidden/i then 403 when /too many requests/i then 429 else 500 end end api_error = IbmAppconfigurationRubySdk::APIError.from_status(http_status, message: e.) @logger.error("API Exception (Status: #{http_status})") retryable = http_status == 429 || http_status >= 500 { ok: false, retryable: retryable, status: http_status, data: nil } rescue StandardError => e @logger.error("Unexpected error: #{e.class.name} - #{e.}") # Treat unexpected errors as retryable { ok: false, retryable: true, status: 500, data: nil } end |
#load_configurations_to_cache(data) ⇒ Boolean
Load configurations to cache
Delegates to ConfigurationHandler singleton to maintain a single source of truth. This ensures all parts of the application use the same cache.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/ibm_appconfiguration_ruby_sdk/config_fetcher.rb', line 208 def load_configurations_to_cache(data) return false unless data begin @logger.info(Constants::CONFIG_LOADING_TO_CACHE) # Delegate to the injected handler (single source of truth) @handler.load_configurations_to_cache(data) @logger.info(Constants::CONFIG_LOADED_TO_CACHE) true rescue StandardError => e @logger.error("Error loading configurations to cache: #{e.class.name} - #{e.}") false end end |
#process_and_load_configurations(api_response) ⇒ Boolean
Process API response and load to cache
This method:
- Takes the raw API response
- Calls extract_configurations to parse and validate the data
- Calls load_configurations_to_cache to store in cache
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/ibm_appconfiguration_ruby_sdk/config_fetcher.rb', line 161 def process_and_load_configurations(api_response) return false unless api_response begin @logger.info(Constants::CONFIG_PROCESSING_RESPONSE) # The IBM SDK normally returns a parsed Hash, but on some retry paths # it may return a raw JSON String — parse it if so. parsed_response = api_response.is_a?(String) ? JSON.parse(api_response) : api_response # Convert string keys to symbol keys if needed symbolized_data = symbolize_keys(parsed_response) # Extract configurations using utils.rb method # This validates the data and extracts only the relevant features, properties, and segments # for the specified environment and collection extracted_config = extract_configurations( symbolized_data, @environment_id, @collection_id ) @logger.info(Constants::CONFIG_EXTRACTED_SUCCESSFULLY) # Load the extracted configurations to cache success = load_configurations_to_cache(extracted_config) if success @logger.info(Constants::CONFIG_PROCESSED_AND_LOADED) else @logger.error(Constants::CONFIG_LOAD_FAILED) end success rescue StandardError => e @logger.error("Error processing API response: #{e.class.name} - #{e.}") false end end |