[android][functions] finalise + cleanup
This commit is contained in:
parent
d06fbec40a
commit
d5abd6a304
|
@ -2,17 +2,14 @@ package io.invertase.firebase.functions;
|
|||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.bridge.Promise;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
import com.facebook.react.bridge.ReadableArray;
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
|
||||
import com.facebook.react.bridge.ReadableNativeArray;
|
||||
import com.facebook.react.bridge.WritableMap;
|
||||
import com.google.android.gms.tasks.OnFailureListener;
|
||||
import com.google.android.gms.tasks.OnSuccessListener;
|
||||
|
@ -21,45 +18,31 @@ import com.google.firebase.functions.FirebaseFunctionsException;
|
|||
import com.google.firebase.functions.HttpsCallableReference;
|
||||
import com.google.firebase.functions.HttpsCallableResult;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import io.invertase.firebase.Utils;
|
||||
|
||||
public class RNFirebaseFunctions extends ReactContextBaseJavaModule {
|
||||
|
||||
private static final String DATA_KEY = "data";
|
||||
private static final String CODE_KEY = "code";
|
||||
private static final String MSG_KEY = "message";
|
||||
private static final String ERROR_KEY = "__error";
|
||||
private static final String DETAILS_KEY = "details";
|
||||
private static final String TAG = "RNFirebaseFunctions";
|
||||
|
||||
public RNFirebaseFunctions(ReactApplicationContext reactContext) {
|
||||
RNFirebaseFunctions(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
Log.d(TAG, "New instance");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String getName() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void httpsCallable(final String name, @Nullable final Object data, final Promise promise) {
|
||||
Object input;
|
||||
if (data == null
|
||||
|| data instanceof String
|
||||
|| data instanceof Boolean
|
||||
|| data instanceof Integer
|
||||
|| data instanceof Long
|
||||
|| data instanceof Float) {
|
||||
input = data;
|
||||
} else if (data instanceof ReadableArray) {
|
||||
input = ((ReadableArray) data).toArrayList();
|
||||
} else if (data instanceof ReadableMap) {
|
||||
input = ((ReadableMap) data).toHashMap();
|
||||
} else {
|
||||
input = null;
|
||||
}
|
||||
public void httpsCallable(final String name, ReadableMap wrapper, final Promise promise) {
|
||||
Object input = wrapper.toHashMap().get(DATA_KEY);
|
||||
Log.d(TAG, "function:call:input:" + name + ":" + input.toString());
|
||||
|
||||
HttpsCallableReference httpsCallableReference = FirebaseFunctions
|
||||
.getInstance()
|
||||
|
@ -70,26 +53,23 @@ public class RNFirebaseFunctions extends ReactContextBaseJavaModule {
|
|||
.addOnSuccessListener(new OnSuccessListener<HttpsCallableResult>() {
|
||||
@Override
|
||||
public void onSuccess(HttpsCallableResult httpsCallableResult) {
|
||||
Log.d(TAG, "function:call:onSuccess:" + name);
|
||||
|
||||
WritableMap map = Arguments.createMap();
|
||||
Object result = httpsCallableResult.getData();
|
||||
if (result == null
|
||||
|| result instanceof String
|
||||
|| result instanceof Boolean
|
||||
|| result instanceof Integer
|
||||
|| result instanceof Long
|
||||
|| result instanceof Float) {
|
||||
Utils.mapPutValue("data", result, map);
|
||||
} else if (result instanceof List) {
|
||||
map.putArray("data", Arguments.makeNativeArray((List<Object>) result));
|
||||
} else if (result instanceof Map) {
|
||||
map.putMap("data", Arguments.makeNativeMap((Map<String, Object>) result));
|
||||
} else {
|
||||
// TODO check for other instance types e.g. ArrayList ?
|
||||
map.putNull("data");
|
||||
}
|
||||
|
||||
Log.d(
|
||||
TAG,
|
||||
"function:call:onSuccess:" + name
|
||||
);
|
||||
Log.d(
|
||||
TAG,
|
||||
"function:call:onSuccess:result:type:" + name + ":" + (result != null ? result.getClass().getName() : "null")
|
||||
);
|
||||
Log.d(
|
||||
TAG,
|
||||
"function:call:onSuccess:result:data:" + name + ":" + (result != null ? result.toString() : "null")
|
||||
);
|
||||
|
||||
Utils.mapPutValue(DATA_KEY, result, map);
|
||||
promise.resolve(map);
|
||||
|
||||
}
|
||||
|
@ -98,32 +78,26 @@ public class RNFirebaseFunctions extends ReactContextBaseJavaModule {
|
|||
@Override
|
||||
public void onFailure(@NonNull Exception exception) {
|
||||
Log.d(TAG, "function:call:onFailure:" + name, exception);
|
||||
|
||||
String message;
|
||||
Object details = null;
|
||||
String code = "UNKNOWN";
|
||||
WritableMap map = Arguments.createMap();
|
||||
|
||||
if (exception instanceof FirebaseFunctionsException) {
|
||||
FirebaseFunctionsException ffe = (FirebaseFunctionsException) exception;
|
||||
/*
|
||||
OK(0),
|
||||
CANCELLED(1),
|
||||
UNKNOWN(2),
|
||||
INVALID_ARGUMENT(3),
|
||||
DEADLINE_EXCEEDED(4),
|
||||
NOT_FOUND(5),
|
||||
ALREADY_EXISTS(6),
|
||||
PERMISSION_DENIED(7),
|
||||
RESOURCE_EXHAUSTED(8),
|
||||
FAILED_PRECONDITION(9),
|
||||
ABORTED(10),
|
||||
OUT_OF_RANGE(11),
|
||||
UNIMPLEMENTED(12),
|
||||
INTERNAL(13),
|
||||
UNAVAILABLE(14),
|
||||
DATA_LOSS(15),
|
||||
UNAUTHENTICATED(16);
|
||||
*/
|
||||
FirebaseFunctionsException.Code code = ffe.getCode();
|
||||
String message = ffe.getMessage();
|
||||
Object details = ffe.getDetails();
|
||||
// TODO promise resolve so we can send details
|
||||
details = ffe.getDetails();
|
||||
code = ffe.getCode().name();
|
||||
message = ffe.getLocalizedMessage();
|
||||
} else {
|
||||
message = exception.getLocalizedMessage();
|
||||
}
|
||||
|
||||
Utils.mapPutValue(CODE_KEY, code, map);
|
||||
Utils.mapPutValue(MSG_KEY, message, map);
|
||||
Utils.mapPutValue(ERROR_KEY, true, map);
|
||||
Utils.mapPutValue(DETAILS_KEY, details, map);
|
||||
promise.resolve(map);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue