Class: LaunchDarkly::Impl::DataSystem::HTTPPollingRequester Private

Inherits:
Object
  • Object
show all
Includes:
DataSystem::Requester
Defined in:
lib/ldclient-rb/impl/data_system/polling.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

HTTPPollingRequester is a Requester that uses HTTP to make requests to the FDv2 polling endpoint.

Since:

  • 5.5.0

Instance Method Summary collapse

Constructor Details

#initialize(sdk_key, http_config, config) ⇒ HTTPPollingRequester

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of HTTPPollingRequester.

Parameters:

Since:

  • 5.5.0



230
231
232
233
234
235
236
237
238
# File 'lib/ldclient-rb/impl/data_system/polling.rb', line 230

def initialize(sdk_key, http_config, config)
  @etag = nil
  @config = config
  @sdk_key = sdk_key
  @poll_uri = http_config.base_uri + FDV2_POLLING_ENDPOINT
  @http_client = Impl::Util.new_http_client(http_config)
    .use(:auto_inflate)
    .headers("Accept-Encoding" => "gzip")
end

Instance Method Details

#fetch(selector) ⇒ Result

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:

Since:

  • 5.5.0



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/ldclient-rb/impl/data_system/polling.rb', line 244

def fetch(selector)
  query_params = []
  query_params << ["filter", @config.payload_filter_key] unless @config.payload_filter_key.nil?

  if selector && selector.defined?
    query_params << ["selector", selector.state]
  end

  uri = @poll_uri
  if query_params.any?
    filter_query = URI.encode_www_form(query_params)
    uri = "#{uri}?#{filter_query}"
  end

  headers = {}
  Impl::Util.default_http_headers(@sdk_key, @config).each { |k, v| headers[k] = v }
  headers["If-None-Match"] = @etag unless @etag.nil?

  begin
    response = @http_client.request("GET", uri, headers: headers)
    status = response.status.code
    response_headers = response.headers.to_h.transform_keys(&:downcase)

    if status >= 400
      return LaunchDarkly::Result.fail(
        "HTTP error #{status}",
        LaunchDarkly::Impl::DataSource::UnexpectedResponseError.new(status),
        response_headers
      )
    end

    if status == 304
      return LaunchDarkly::Result.success([LaunchDarkly::Interfaces::DataSystem::ChangeSetBuilder.no_changes, response_headers])
    end

    body = response.to_s
    data = JSON.parse(body, symbolize_names: true)
    etag = response_headers["etag"]
    @etag = etag unless etag.nil?

    @config.logger.debug { "[LDClient] #{uri} response status:[#{status}] ETag:[#{etag}]" }

    changeset_result = LaunchDarkly::Impl::DataSystem.polling_payload_to_changeset(data)
    if changeset_result.success?
      LaunchDarkly::Result.success([changeset_result.value, response_headers])
    else
      LaunchDarkly::Result.fail(changeset_result.error, changeset_result.exception, response_headers)
    end
  rescue JSON::ParserError => e
    LaunchDarkly::Result.fail("Failed to parse JSON: #{e.message}", e, response_headers)
  rescue => e
    LaunchDarkly::Result.fail("Network error: #{e.message}", e)
  end
end

#stopObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Closes the HTTP client and releases any persistent connections.

Since:

  • 5.5.0



302
303
304
305
306
307
# File 'lib/ldclient-rb/impl/data_system/polling.rb', line 302

def stop
  begin
    @http_client.close if @http_client
  rescue
  end
end