callExtension static method

Future<Map<String, dynamic>?> callExtension(
  1. String slug,
  2. String requestType,
  3. String endPoint,
  4. Map<String, dynamic>? body, {
  5. required dynamic onSuccess(
    1. Map<String, dynamic> map
    )?,
  6. required dynamic onError(
    1. CometChatException excep
    )?,
})

can call any extension

can be used in extensions like Pin message, Save message,Tiny Url, Bitly etc

method could throw PlatformException with error codes specifying the cause Calls a CometChat extension.

Migration Note: Migrated from platform channels to native Dart implementation. Uses ExtensionRepository for calling extensions. Behavior and signature remain identical for backward compatibility.

Android Reference: CometChat.callExtension(String slug, String type, String endpoint, JSONObject body, CallbackListener<JSONObject>)

Implementation

static Future<Map<String, dynamic>?> callExtension(String slug,
    String requestType, String endPoint, Map<String, dynamic>? body,
    {required Function(Map<String, dynamic> map)? onSuccess,
    required Function(CometChatException excep)? onError}) async {
  try {
    // Validate parameters
    if (slug.isEmpty) {
      onError?.call(CometChatException(
          ErrorCode.errorEmptyExtensionSlug,
          ErrorMessage.errorMessageEmptyExtensionSlug,
          ErrorMessage.errorMessageEmptyExtensionSlug));
      return null;
    }
    if (requestType.isEmpty) {
      onError?.call(CometChatException(
          ErrorCode.errorEmptyExtensionMethod,
          ErrorMessage.errorMessageEmptyExtensionMethod,
          ErrorMessage.errorMessageEmptyExtensionMethod));
      return null;
    }
    if (endPoint.isEmpty) {
      onError?.call(CometChatException(
          ErrorCode.errorEmptyExtensionEndpoint,
          ErrorMessage.errorMessageEmptyExtensionEndpoint,
          ErrorMessage.errorMessageEmptyExtensionEndpoint));
      return null;
    }

    // Normalize endpoint (remove leading slash for consistency)
    if (endPoint.isNotEmpty && endPoint[0] == '/') {
      // Keep the slash for consistency with Android SDK
    }

    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // Call native Dart extension repository
    final result = await sdk.extensions.callExtension(
      slug,
      requestType,
      endPoint,
      body: body,
    );

    // Wrap result in 'data' key for backward compatibility
    final res = {'data': result};

    // Call success callback
    if (onSuccess != null) onSuccess(res);
    return res;
  } on SdkException catch (sdkEx) {
    // Convert SdkException to CometChatException
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );
    _errorCallbackHandler(cometChatEx, null, null, onError);
  } catch (e) {
    _errorCallbackHandler(null, null, e, onError);
  }
  return null;
}