diff --git a/firebase-functions/api.txt b/firebase-functions/api.txt index 25df5fab60a..4b07ac34ce6 100644 --- a/firebase-functions/api.txt +++ b/firebase-functions/api.txt @@ -3,6 +3,7 @@ package com.google.firebase.functions { public class FirebaseFunctions { method @NonNull public com.google.firebase.functions.HttpsCallableReference getHttpsCallable(@NonNull String); + method @NonNull public com.google.firebase.functions.HttpsCallableReference getHttpsCallableFromUrl(@NonNull java.net.URL); method @NonNull public static com.google.firebase.functions.FirebaseFunctions getInstance(@NonNull com.google.firebase.FirebaseApp, @NonNull String); method @NonNull public static com.google.firebase.functions.FirebaseFunctions getInstance(@NonNull com.google.firebase.FirebaseApp); method @NonNull public static com.google.firebase.functions.FirebaseFunctions getInstance(@NonNull String); diff --git a/firebase-functions/src/main/java/com/google/firebase/functions/FirebaseFunctions.java b/firebase-functions/src/main/java/com/google/firebase/functions/FirebaseFunctions.java index 100e6929f8e..ffc453bba15 100644 --- a/firebase-functions/src/main/java/com/google/firebase/functions/FirebaseFunctions.java +++ b/firebase-functions/src/main/java/com/google/firebase/functions/FirebaseFunctions.java @@ -204,6 +204,12 @@ public HttpsCallableReference getHttpsCallable(@NonNull String name) { return new HttpsCallableReference(this, name); } + /** Returns a reference to the Callable HTTPS trigger with the provided url. */ + @NonNull + public HttpsCallableReference getHttpsCallableFromUrl(@NonNull URL url) { + return new HttpsCallableReference(this, url); + } + /** * Returns the URL for a particular function. * @@ -270,7 +276,29 @@ Task call(String name, @Nullable Object data, HttpsCallOpti return Tasks.forException(task.getException()); } HttpsCallableContext context = task.getResult(); - return call(name, data, context, options); + URL url = getURL(name); + return call(url, data, context, options); + }); + } + + /** + * Calls a Callable HTTPS trigger endpoint. + * + * @param url The url of the HTTPS trigger + * @param data Parameters to pass to the function. Can be anything encodable as JSON. + * @return A Task that will be completed when the request is complete. + */ + Task call(URL url, @Nullable Object data, HttpsCallOptions options) { + return providerInstalled + .getTask() + .continueWithTask(task -> contextProvider.getContext()) + .continueWithTask( + task -> { + if (!task.isSuccessful()) { + return Tasks.forException(task.getException()); + } + HttpsCallableContext context = task.getResult(); + return call(url, data, context, options); }); } @@ -283,12 +311,11 @@ Task call(String name, @Nullable Object data, HttpsCallOpti * @return A Task that will be completed when the request is complete. */ private Task call( - @NonNull String name, + @NonNull URL url, @Nullable Object data, HttpsCallableContext context, HttpsCallOptions options) { - Preconditions.checkNotNull(name, "name cannot be null"); - URL url = getURL(name); + Preconditions.checkNotNull(url, "url cannot be null"); Map body = new HashMap<>(); diff --git a/firebase-functions/src/main/java/com/google/firebase/functions/HttpsCallableReference.java b/firebase-functions/src/main/java/com/google/firebase/functions/HttpsCallableReference.java index d212775200f..26dfa4c498c 100644 --- a/firebase-functions/src/main/java/com/google/firebase/functions/HttpsCallableReference.java +++ b/firebase-functions/src/main/java/com/google/firebase/functions/HttpsCallableReference.java @@ -17,6 +17,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.google.android.gms.tasks.Task; +import java.net.URL; import java.util.concurrent.TimeUnit; /** A reference to a particular Callable HTTPS trigger in Cloud Functions. */ @@ -26,8 +27,13 @@ public class HttpsCallableReference { private final FirebaseFunctions functionsClient; // The name of the HTTPS endpoint this reference refers to. + // Is null if url is set. private final String name; + // The url of the HTTPS endpoint this reference refers to. + // Is null if name is set. + private final URL url; + // Options for how to do the HTTPS call. HttpsCallOptions options = new HttpsCallOptions(); @@ -35,6 +41,14 @@ public class HttpsCallableReference { HttpsCallableReference(FirebaseFunctions functionsClient, String name) { this.functionsClient = functionsClient; this.name = name; + this.url = null; + } + + /** Creates a new reference with the given options. */ + HttpsCallableReference(FirebaseFunctions functionsClient, URL url) { + this.functionsClient = functionsClient; + this.name = null; + this.url = url; } /** @@ -80,7 +94,11 @@ public class HttpsCallableReference { */ @NonNull public Task call(@Nullable Object data) { - return functionsClient.call(name, data, options); + if (name != null) { + return functionsClient.call(name, data, options); + } else { + return functionsClient.call(url, data, options); + } } /** @@ -99,7 +117,11 @@ public Task call(@Nullable Object data) { */ @NonNull public Task call() { - return functionsClient.call(name, null, options); + if (name != null) { + return functionsClient.call(name, null, options); + } else { + return functionsClient.call(url, null, options); + } } /**