fetch method

Future<bool> fetch({
  1. Map<String, Object?>? attributes,
})

Fetch the active template and activate it. Returns true when the values changed since the last fetch. attributes are merged (for this fetch only) over the persistent ones set via setAttributes. Best-effort — never throws.

Implementation

Future<bool> fetch({Map<String, Object?>? attributes}) async {
  final res = await _http.post(
    '/partner/remote-config/fetch',
    _buildContext(attributes),
    extraHeaders: _etag != null ? {'if-none-match': _etag!} : null,
  );
  if (res == null) return false; // request failed — keep current values

  // Un-enveloped partner response; `data` fallback for safety.
  final body =
      (res['parameters'] != null ||
          res['notModified'] != null ||
          res['throttled'] != null)
      ? res
      : (res['data'] as Map<String, dynamic>? ?? res);

  if (body['notModified'] == true || body['throttled'] == true) {
    _lastFetch = DateTime.now();
    return false;
  }

  final params = body['parameters'] as Map<String, dynamic>? ?? {};
  _values.clear();
  _sources.clear();
  params.forEach((key, raw) {
    final entry = raw as Map<String, dynamic>;
    _values[key] = entry['value'];
    _sources[key] = entry['source'] as String?;
  });
  _etag = body['etag'] as String? ?? _etag;
  _version = (body['version'] as num?)?.toInt() ?? _version;
  _lastFetch = DateTime.now();
  NexusLog.debug(
    'remote-config: fetched v$_version (${_values.length} params)',
  );
  if (!_changes.isClosed) _changes.add(null);
  return true;
}