fetch method

Future<void> fetch({
  1. required String resourceId,
  2. required ResourceType type,
  3. Bridge? bridge,
  4. String decrypter(
    1. String ciphertext
    )?,
})

Fetches the resource with the given resourceId and type from the network.

If you know which bridge the resource is on, you can provide the bridge parameter to speed up the process.

decrypter When the old tokens are read from local storage, they are decrypted. This parameter allows you to provide your own decryption method. This will be used in addition to the default decryption method. This will be performed after the default decryption method.

If this method fails to fetch the resource with an error, the resource will be added to the failedFetches list.

Implementation

Future<void> fetch({
  required String resourceId,
  required ResourceType type,
  Bridge? bridge,
  String Function(String ciphertext)? decrypter,
}) async {
  final List<Bridge> bridgesToCheck;
  if (bridge == null) {
    bridgesToCheck = bridges;
  } else {
    bridgesToCheck = [bridge];
  }

  bool found = false;

  for (final Bridge bridge in bridgesToCheck) {
    final Map<String, dynamic>? data;
    try {
      data = await HueHttpRepo.get(
        bridgeIpAddr: bridge.ipAddress!,
        applicationKey: bridge.applicationKey!,
        resourceType: type,
        pathToResource: resourceId,
        decrypter: decrypter,
      );
    } catch (e) {
      _addFailedFetch(
        FailedResource(
          id: resourceId,
          type: type,
          bridge: bridge,
          error: ErrorType.unknown,
          additionalInfo: e.toString(),
        ),
      );
      continue;
    }

    if (data == null) return;

    // This means the resource is not on this bridge.
    if (data.toString().contains('Not Found')) continue;

    found = true;

    _mapToObject(type, bridge, data);
  }

  if (!found) {
    _addFailedFetch(
      FailedResource(
        id: resourceId,
        type: type,
        bridge: bridge,
        error: ErrorType.notFound,
      ),
    );
  }
}