fetch method

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

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.

token is the remote access token.

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? token,
}) 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,
        token: token,
      );
    } 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,
      ),
    );
  }
}