generateCallToken static method

void generateCallToken(
  1. String sessionId, {
  2. required dynamic onSuccess(
    1. CallToken
    ),
  3. required dynamic onError(
    1. CometChatCallsException
    ),
})

Generates a call token for the given sessionId.

Uses the internally cached auth token from login/loginWithAuthToken. The onSuccess callback receives a CallToken instance.

CometChatCalls.generateCallToken('session-123',
  onSuccess: (CallToken token) { ... },
  onError: (e) { ... },
);

Implementation

static void generateCallToken(String sessionId,
    {required Function(CallToken) onSuccess,
    required Function(CometChatCallsException) onError}) async {
  try {
    if (!isInitialized) {
      onError(CometChatCallsException.sdkNotInitialized());
      return;
    }

    final sessionIdError = InputValidator.validateSessionId(sessionId);
    if (sessionIdError != null) {
      onError(sessionIdError);
      return;
    }

    final authToken = await getUserAuthToken();
    if (authToken == null || authToken.isEmpty) {
      onError(CometChatCallsException.userAuthTokenNull());
      return;
    }

    final CallToken callToken = CallToken();
    callToken.sessionID = sessionId;
    ApiConnection().generateToken(
        callToken: callToken,
        authToken: authToken,
        onSuccess: (success) {
          try {
            final Map<String, dynamic> result = jsonDecode(success);
            final CallToken token =
                CallToken.fromJson(result[ResponseKeyConstants.data]);
            onSuccess(token);
          } catch (e) {
            onError(CometChatCallsException.jsonParseError(e.toString()));
          }
        },
        onError: onError);
  } catch (e) {
    onError(CometChatCallsException.wrapUnhandled(e));
  }
}