Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,72 @@
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;

/** CloudFunctionsPlugin */
/**
* CloudFunctionsPlugin
*/
public class CloudFunctionsPlugin implements MethodCallHandler {
/** Plugin registration. */
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "cloud_functions");
channel.setMethodCallHandler(new CloudFunctionsPlugin());
}
/**
* Plugin registration.
*/
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "cloud_functions");
channel.setMethodCallHandler(new CloudFunctionsPlugin());
}

@Override
public void onMethodCall(MethodCall call, final Result result) {
switch (call.method) {
case "CloudFunctions#call":
String functionName = call.argument("functionName");
HttpsCallableReference httpsCallableReference =
FirebaseFunctions.getInstance().getHttpsCallable(functionName);
Map<String, Object> parameters = call.argument("parameters");
httpsCallableReference
.call(parameters)
.addOnCompleteListener(
new OnCompleteListener<HttpsCallableResult>() {
@Override
public void onComplete(@NonNull Task<HttpsCallableResult> task) {
if (task.isSuccessful()) {
result.success(task.getResult().getData());
} else {
if (task.getException() instanceof FirebaseFunctionsException) {
FirebaseFunctionsException exception =
(FirebaseFunctionsException) task.getException();
Map<String, Object> exceptionMap = new HashMap<>();
exceptionMap.put("code", exception.getCode().name());
exceptionMap.put("message", exception.getMessage());
exceptionMap.put("details", exception.getDetails());
result.error(
"functionsError",
"Cloud function failed with exception.",
exceptionMap);
} else {
Exception exception = task.getException();
result.error(null, exception.getMessage(), null);
}
}
}
});
break;
default:
result.notImplemented();
}
@Override
public void onMethodCall(MethodCall call, final Result result) {
switch (call.method) {
case "CloudFunctions#call":
String functionName = call.argument("functionName");
HttpsCallableReference httpsCallableReference =
FirebaseFunctions.getInstance().getHttpsCallable(functionName);
Map < String, Object > parameters = call.argument("parameters");
httpsCallableReference
.call(parameters)
.addOnCompleteListener(
new OnCompleteListener < HttpsCallableResult > () {
@Override
public void onComplete(@NonNull Task < HttpsCallableResult > task) {
if (task.isSuccessful()) {
result.success(task.getResult().getData());
} else {
if (task.getException() instanceof FirebaseFunctionsException) {
FirebaseFunctionsException exception =
(FirebaseFunctionsException) task.getException();
Map < String, Object > exceptionMap = new HashMap < > ();
exceptionMap.put("code", exception.getCode().name());
exceptionMap.put("message", exception.getMessage());
exceptionMap.put("details", exception.getDetails());
result.error(
"functionsError",
"Cloud function failed with exception.",
exceptionMap);
} else if (task.getException() instanceof SocketTimeoutException) {
SocketTimeoutException exception =
(SocketTimeoutException) task.getException();
Map < String, Object > exceptionMap = new HashMap < > ();
exceptionMap.put("code", FirebaseFunctionsException.Code.DEADLINE_EXCEEDED.name());
exceptionMap.put("message", exception.getMessage());
exceptionMap.put("details", "SocketTimeoutException");
result.error(
"functionsError",
"Cloud function failed with exception.",
exceptionMap);
} else {
Exception exception = task.getException();
result.error(null, exception.getMessage(), null);
}
}
}
});
break;
default:
result.notImplemented();
}
}
}