Skip to content

Add FirebaseFunctions.getHttpsCallableFromURL #3659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions firebase-functions/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -270,7 +276,29 @@ Task<HttpsCallableResult> 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<HttpsCallableResult> 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);
});
}

Expand All @@ -283,12 +311,11 @@ Task<HttpsCallableResult> call(String name, @Nullable Object data, HttpsCallOpti
* @return A Task that will be completed when the request is complete.
*/
private Task<HttpsCallableResult> 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<String, Object> body = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -26,15 +27,28 @@ 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();

/** Creates a new reference with the given options. */
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;
}

/**
Expand Down Expand Up @@ -80,7 +94,11 @@ public class HttpsCallableReference {
*/
@NonNull
public Task<HttpsCallableResult> 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);
}
}

/**
Expand All @@ -99,7 +117,11 @@ public Task<HttpsCallableResult> call(@Nullable Object data) {
*/
@NonNull
public Task<HttpsCallableResult> call() {
return functionsClient.call(name, null, options);
if (name != null) {
return functionsClient.call(name, null, options);
} else {
return functionsClient.call(url, null, options);
}
}

/**
Expand Down