[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.support.annotation.NonNull;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.support.annotation.Nullable;
|
|
||||||
|
|
||||||
import com.facebook.react.bridge.Arguments;
|
import com.facebook.react.bridge.Arguments;
|
||||||
import com.facebook.react.bridge.Promise;
|
import com.facebook.react.bridge.Promise;
|
||||||
import com.facebook.react.bridge.ReactMethod;
|
import com.facebook.react.bridge.ReactMethod;
|
||||||
import com.facebook.react.bridge.ReadableArray;
|
|
||||||
import com.facebook.react.bridge.ReadableMap;
|
import com.facebook.react.bridge.ReadableMap;
|
||||||
import com.facebook.react.bridge.ReactApplicationContext;
|
import com.facebook.react.bridge.ReactApplicationContext;
|
||||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||||
|
|
||||||
import com.facebook.react.bridge.ReadableNativeArray;
|
|
||||||
import com.facebook.react.bridge.WritableMap;
|
import com.facebook.react.bridge.WritableMap;
|
||||||
import com.google.android.gms.tasks.OnFailureListener;
|
import com.google.android.gms.tasks.OnFailureListener;
|
||||||
import com.google.android.gms.tasks.OnSuccessListener;
|
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.HttpsCallableReference;
|
||||||
import com.google.firebase.functions.HttpsCallableResult;
|
import com.google.firebase.functions.HttpsCallableResult;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import io.invertase.firebase.Utils;
|
import io.invertase.firebase.Utils;
|
||||||
|
|
||||||
public class RNFirebaseFunctions extends ReactContextBaseJavaModule {
|
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";
|
private static final String TAG = "RNFirebaseFunctions";
|
||||||
|
|
||||||
public RNFirebaseFunctions(ReactApplicationContext reactContext) {
|
RNFirebaseFunctions(ReactApplicationContext reactContext) {
|
||||||
super(reactContext);
|
super(reactContext);
|
||||||
Log.d(TAG, "New instance");
|
Log.d(TAG, "New instance");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return TAG;
|
return TAG;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ReactMethod
|
@ReactMethod
|
||||||
public void httpsCallable(final String name, @Nullable final Object data, final Promise promise) {
|
public void httpsCallable(final String name, ReadableMap wrapper, final Promise promise) {
|
||||||
Object input;
|
Object input = wrapper.toHashMap().get(DATA_KEY);
|
||||||
if (data == null
|
Log.d(TAG, "function:call:input:" + name + ":" + input.toString());
|
||||||
|| 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpsCallableReference httpsCallableReference = FirebaseFunctions
|
HttpsCallableReference httpsCallableReference = FirebaseFunctions
|
||||||
.getInstance()
|
.getInstance()
|
||||||
|
@ -70,26 +53,23 @@ public class RNFirebaseFunctions extends ReactContextBaseJavaModule {
|
||||||
.addOnSuccessListener(new OnSuccessListener<HttpsCallableResult>() {
|
.addOnSuccessListener(new OnSuccessListener<HttpsCallableResult>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(HttpsCallableResult httpsCallableResult) {
|
public void onSuccess(HttpsCallableResult httpsCallableResult) {
|
||||||
Log.d(TAG, "function:call:onSuccess:" + name);
|
|
||||||
|
|
||||||
WritableMap map = Arguments.createMap();
|
WritableMap map = Arguments.createMap();
|
||||||
Object result = httpsCallableResult.getData();
|
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);
|
promise.resolve(map);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -98,32 +78,26 @@ public class RNFirebaseFunctions extends ReactContextBaseJavaModule {
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(@NonNull Exception exception) {
|
public void onFailure(@NonNull Exception exception) {
|
||||||
Log.d(TAG, "function:call:onFailure:" + name, 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) {
|
if (exception instanceof FirebaseFunctionsException) {
|
||||||
FirebaseFunctionsException ffe = (FirebaseFunctionsException) exception;
|
FirebaseFunctionsException ffe = (FirebaseFunctionsException) exception;
|
||||||
/*
|
details = ffe.getDetails();
|
||||||
OK(0),
|
code = ffe.getCode().name();
|
||||||
CANCELLED(1),
|
message = ffe.getLocalizedMessage();
|
||||||
UNKNOWN(2),
|
} else {
|
||||||
INVALID_ARGUMENT(3),
|
message = exception.getLocalizedMessage();
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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