createChannel method

Future<KumulosChannel> createChannel({
  1. required String uuid,
  2. bool subscribe = false,
  3. String? name,
  4. bool showInPortal = false,
  5. Map<String, dynamic>? meta,
})

Implementation

Future<KumulosChannel> createChannel(
    {required String uuid,
    bool subscribe = false,
    String? name,
    bool showInPortal = false,
    Map<String, dynamic>? meta}) async {
  if (uuid.length == 0) {
    throw Exception('Channel uuid must be specified for channel creation');
  }

  if (showInPortal && (name == null || name.length == 0)) {
    throw Exception(
        'Channel name must be specified for channel creation if the channel should be displayed in the portal');
  }

  const url = '$crmBaseUrl/v1/channels';

  var req = {
    'uuid': uuid,
    'name': name,
    'showInPortal': showInPortal,
    'meta': meta,
  };

  if (subscribe) {
    req['userIdentifier'] = await Kumulos.currentUserIdentifier;
  }

  var res = await _makeRequest(
      method: 'POST', url: Uri.parse(url), body: jsonEncode(req));

  if (res.statusCode != 201) {
    throw Exception('Failed to create channel, status: ${res.statusCode}');
  }

  var json = await Utils.readResponse(res);
  var decoded = jsonDecode(json);
  var data = Map<String, dynamic>.from(decoded);

  return KumulosChannel.fromMap(data);
}