Skip to content

Commit 09b7445

Browse files
authored
Add FirebaseFunctions.getHttpsCallableFromURL (#3659)
* Initial commit; don't know how to run formatter or integration tests * Add api.txt * Generate api.txt file * Fix bad formatting * Finally fix style issues
1 parent eb7bb1e commit 09b7445

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

firebase-functions/api.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.google.firebase.functions {
33

44
public class FirebaseFunctions {
55
method @NonNull public com.google.firebase.functions.HttpsCallableReference getHttpsCallable(@NonNull String);
6+
method @NonNull public com.google.firebase.functions.HttpsCallableReference getHttpsCallableFromUrl(@NonNull java.net.URL);
67
method @NonNull public static com.google.firebase.functions.FirebaseFunctions getInstance(@NonNull com.google.firebase.FirebaseApp, @NonNull String);
78
method @NonNull public static com.google.firebase.functions.FirebaseFunctions getInstance(@NonNull com.google.firebase.FirebaseApp);
89
method @NonNull public static com.google.firebase.functions.FirebaseFunctions getInstance(@NonNull String);

firebase-functions/src/main/java/com/google/firebase/functions/FirebaseFunctions.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ public HttpsCallableReference getHttpsCallable(@NonNull String name) {
204204
return new HttpsCallableReference(this, name);
205205
}
206206

207+
/** Returns a reference to the Callable HTTPS trigger with the provided url. */
208+
@NonNull
209+
public HttpsCallableReference getHttpsCallableFromUrl(@NonNull URL url) {
210+
return new HttpsCallableReference(this, url);
211+
}
212+
207213
/**
208214
* Returns the URL for a particular function.
209215
*
@@ -270,7 +276,29 @@ Task<HttpsCallableResult> call(String name, @Nullable Object data, HttpsCallOpti
270276
return Tasks.forException(task.getException());
271277
}
272278
HttpsCallableContext context = task.getResult();
273-
return call(name, data, context, options);
279+
URL url = getURL(name);
280+
return call(url, data, context, options);
281+
});
282+
}
283+
284+
/**
285+
* Calls a Callable HTTPS trigger endpoint.
286+
*
287+
* @param url The url of the HTTPS trigger
288+
* @param data Parameters to pass to the function. Can be anything encodable as JSON.
289+
* @return A Task that will be completed when the request is complete.
290+
*/
291+
Task<HttpsCallableResult> call(URL url, @Nullable Object data, HttpsCallOptions options) {
292+
return providerInstalled
293+
.getTask()
294+
.continueWithTask(task -> contextProvider.getContext())
295+
.continueWithTask(
296+
task -> {
297+
if (!task.isSuccessful()) {
298+
return Tasks.forException(task.getException());
299+
}
300+
HttpsCallableContext context = task.getResult();
301+
return call(url, data, context, options);
274302
});
275303
}
276304

@@ -283,12 +311,11 @@ Task<HttpsCallableResult> call(String name, @Nullable Object data, HttpsCallOpti
283311
* @return A Task that will be completed when the request is complete.
284312
*/
285313
private Task<HttpsCallableResult> call(
286-
@NonNull String name,
314+
@NonNull URL url,
287315
@Nullable Object data,
288316
HttpsCallableContext context,
289317
HttpsCallOptions options) {
290-
Preconditions.checkNotNull(name, "name cannot be null");
291-
URL url = getURL(name);
318+
Preconditions.checkNotNull(url, "url cannot be null");
292319

293320
Map<String, Object> body = new HashMap<>();
294321

firebase-functions/src/main/java/com/google/firebase/functions/HttpsCallableReference.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import androidx.annotation.NonNull;
1818
import androidx.annotation.Nullable;
1919
import com.google.android.gms.tasks.Task;
20+
import java.net.URL;
2021
import java.util.concurrent.TimeUnit;
2122

2223
/** A reference to a particular Callable HTTPS trigger in Cloud Functions. */
@@ -26,15 +27,28 @@ public class HttpsCallableReference {
2627
private final FirebaseFunctions functionsClient;
2728

2829
// The name of the HTTPS endpoint this reference refers to.
30+
// Is null if url is set.
2931
private final String name;
3032

33+
// The url of the HTTPS endpoint this reference refers to.
34+
// Is null if name is set.
35+
private final URL url;
36+
3137
// Options for how to do the HTTPS call.
3238
HttpsCallOptions options = new HttpsCallOptions();
3339

3440
/** Creates a new reference with the given options. */
3541
HttpsCallableReference(FirebaseFunctions functionsClient, String name) {
3642
this.functionsClient = functionsClient;
3743
this.name = name;
44+
this.url = null;
45+
}
46+
47+
/** Creates a new reference with the given options. */
48+
HttpsCallableReference(FirebaseFunctions functionsClient, URL url) {
49+
this.functionsClient = functionsClient;
50+
this.name = null;
51+
this.url = url;
3852
}
3953

4054
/**
@@ -80,7 +94,11 @@ public class HttpsCallableReference {
8094
*/
8195
@NonNull
8296
public Task<HttpsCallableResult> call(@Nullable Object data) {
83-
return functionsClient.call(name, data, options);
97+
if (name != null) {
98+
return functionsClient.call(name, data, options);
99+
} else {
100+
return functionsClient.call(url, data, options);
101+
}
84102
}
85103

86104
/**
@@ -99,7 +117,11 @@ public Task<HttpsCallableResult> call(@Nullable Object data) {
99117
*/
100118
@NonNull
101119
public Task<HttpsCallableResult> call() {
102-
return functionsClient.call(name, null, options);
120+
if (name != null) {
121+
return functionsClient.call(name, null, options);
122+
} else {
123+
return functionsClient.call(url, null, options);
124+
}
103125
}
104126

105127
/**

0 commit comments

Comments
 (0)