listWithState method

Future<ListServicesResult> listWithState()

Implementation

Future<ListServicesResult> listWithState() async {
  final output = await room.invoke(
    toolkit: "services",
    tool: "list",
    input: ToolContentInput(JsonContent(json: {})),
  );
  if (output is! ToolContentOutput || output.content is! JsonContent) {
    throw _unexpectedResponseError("list");
  }

  final response = output.content as JsonContent;
  final servicesRaw = response.json["services_json"];
  final serviceStatesRaw = response.json["service_states"];
  if (servicesRaw is! List || serviceStatesRaw is! List) {
    throw _unexpectedResponseError("list");
  }

  final serviceStates = <String, ServiceRuntimeState>{};
  for (final item in serviceStatesRaw) {
    if (item is! Map) {
      throw _unexpectedResponseError("list");
    }
    final state = ServiceRuntimeState.fromJson(Map<String, dynamic>.from(item));
    serviceStates[state.serviceId] = state;
  }

  return ListServicesResult(
    services: servicesRaw.map((item) {
      if (item is! String) {
        throw _unexpectedResponseError("list");
      }
      final decoded = jsonDecode(item);
      if (decoded is! Map) {
        throw _unexpectedResponseError("list");
      }
      return ServiceSpec.fromJson(Map<String, dynamic>.from(decoded));
    }).toList(),
    serviceStates: serviceStates,
  );
}