Skip to content
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

feat(functions): add streamAsFlow() for convenience #6774

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

thatfiredev
Copy link
Member

@thatfiredev thatfiredev commented Mar 14, 2025

For developers using Kotlin, a simple call to a streaming function (eg. functions.getHttpsCallable("streaming").stream().asFlow()) requires them to add the following dependencies to their build.gradle(.kts) file:

implementation("org.reactivestreams:reactive-streams:1.0.4")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactive:1.8.1")

This is an extra step that I think we should avoid, considering we're moving to a Kotlin-first SDK.

This PR addresses that by introducing a new streamAsFlow() function that Kotlin developers can call without the for the extra dependencies.

Alternative solution

An alternative would be to expose these libraries in our SDK as transitive dependencies so that developers don't have to add them to their apps:

api("org.reactivestreams:reactive-streams:1.0.4")
api("org.jetbrains.kotlinx:kotlinx-coroutines-reactive:1.8.1")

Copy link
Contributor

github-actions bot commented Mar 14, 2025

Javadoc Changes:
--- /home/runner/diff/original/firebase-kotlindoc/android/com/google/firebase/functions/HttpsCallableReference.html	2025-03-14 14:17:09.174318372 +0000
+++ /home/runner/diff/modified/firebase-kotlindoc/android/com/google/firebase/functions/HttpsCallableReference.html	2025-03-14 14:14:15.214219952 +0000
@@ -75,6 +75,13 @@
             </td>
           </tr>
           <tr>
+            <td><code>final @<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> <a href="https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html">Flow</a>&lt;@<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> <a href="/docs/reference/android/com/google/firebase/functions/StreamResponse.html">StreamResponse</a>&gt;</code></td>
+            <td>
+              <div><code><a href="/docs/reference/android/com/google/firebase/functions/HttpsCallableReference.html#streamAsFlow(kotlin.Any)">streamAsFlow</a>(<a href="https://developer.android.com/reference/kotlin/java/lang/Object.html">Object</a>&nbsp;data)</code></div>
+              <p>Streams data to the specified HTTPS endpoint.</p>
+            </td>
+          </tr>
+          <tr>
             <td><code>final @<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> <a href="/docs/reference/android/com/google/firebase/functions/HttpsCallableReference.html">HttpsCallableReference</a></code></td>
             <td>
               <div><code><a href="/docs/reference/android/com/google/firebase/functions/HttpsCallableReference.html#withTimeout(kotlin.Long,java.util.concurrent.TimeUnit)">withTimeout</a>(long&nbsp;timeout,&nbsp;@<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/kotlin/java/util/concurrent/TimeUnit.html">TimeUnit</a>&nbsp;units)</code></div>
@@ -369,6 +376,131 @@
                 </td>
               </tr>
             </tbody>
+          </table>
+        </div>
+        <div class="devsite-table-wrapper">
+          <table class="responsive">
+            <colgroup>
+              <col width="40%">
+              <col>
+            </colgroup>
+            <thead>
+              <tr>
+                <th colspan="100%">See also</th>
+              </tr>
+            </thead>
+            <tbody class="list">
+              <tr>
+                <td><code><a href="https://developer.android.com/reference/kotlin/org/json/JSONArray.html">JSONArray</a></code></td>
+                <td></td>
+              </tr>
+              <tr>
+                <td><code><a href="https://developer.android.com/reference/kotlin/org/json/JSONObject.html">JSONObject</a></code></td>
+                <td></td>
+              </tr>
+              <tr>
+                <td><code><a href="https://developer.android.com/reference/kotlin/java/io/IOException.html">IOException</a></code></td>
+                <td></td>
+              </tr>
+              <tr>
+                <td><code><a href="/docs/reference/android/com/google/firebase/functions/FirebaseFunctionsException.html">FirebaseFunctionsException</a></code></td>
+                <td></td>
+              </tr>
+            </tbody>
+          </table>
+        </div>
+      </div>
+      <div class="api-item"><a name="streamAsFlow-kotlin.Any-"></a><a name="streamasflow"></a>
+        <h3 class="api-name" id="streamAsFlow(kotlin.Any)">streamAsFlow</h3>
+        <pre class="api-signature no-pretty-print">public&nbsp;final&nbsp;@<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> <a href="https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html">Flow</a>&lt;@<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> <a href="/docs/reference/android/com/google/firebase/functions/StreamResponse.html">StreamResponse</a>&gt;&nbsp;<a href="/docs/reference/android/com/google/firebase/functions/HttpsCallableReference.html#streamAsFlow(kotlin.Any)">streamAsFlow</a>(<a href="https://developer.android.com/reference/kotlin/java/lang/Object.html">Object</a>&nbsp;data)</pre>
+        <p>Streams data to the specified HTTPS endpoint.</p>
+        <p>The data passed into the trigger can be any of the following types:</p>
+        <ul>
+          <li>
+            <p>Any primitive type, including null, int, long, float, and boolean.</p>
+          </li>
+          <li>
+            <p><code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html">String</a></code></p>
+          </li>
+          <li>
+            <p><code><a href="https://developer.android.com/reference/kotlin/java/util/List.html">List</a></code>, where the contained objects are also one of these types.</p>
+          </li>
+          <li>
+            <p><code><a href="https://developer.android.com/reference/kotlin/java/util/Map.html">Map</a></code>, where the values are also one of these types.</p>
+          </li>
+          <li>
+            <p><code><a href="https://developer.android.com/reference/kotlin/org/json/JSONArray.html">org.json.JSONArray</a></code></p>
+          </li>
+          <li>
+            <p><code><a href="https://developer.android.com/reference/kotlin/org/json/JSONObject.html">org.json.JSONObject</a></code></p>
+          </li>
+          <li>
+            <p><code><a href="https://developer.android.com/reference/kotlin/org/json/JSONObject.html#NULL--">org.json.JSONObject.NULL</a></code></p>
+          </li>
+        </ul>
+        <p>If the returned streamResponse fails, the exception will be one of the following types:</p>
+        <ul>
+          <li>
+            <p><code><a href="https://developer.android.com/reference/kotlin/java/io/IOException.html">java.io.IOException</a></code></p>
+          </li>
+        </ul>
+        <ul>
+          <li>
+            <p>if the HTTPS request failed to connect.</p>
+          </li>
+        </ul>
+        <ul>
+          <li>
+            <p><code><a href="/docs/reference/android/com/google/firebase/functions/FirebaseFunctionsException.html">FirebaseFunctionsException</a></code></p>
+          </li>
+        </ul>
+        <ul>
+          <li>
+            <p>if the request connected, but the function returned an error.</p>
+          </li>
+        </ul>
+        <p>The request to the Cloud Functions backend made by this method automatically includes a Firebase Instance ID token to identify the app instance. If a user is logged in with Firebase Auth, an auth token for the user will also be automatically included.</p>
+        <p>Firebase Instance ID sends data to the Firebase backend periodically to collect information regarding the app instance. To stop this, see <code><a href="/docs/reference/android/com/google/firebase/iid/FirebaseInstanceId.html#deleteInstanceId()">com.google.firebase.iid.FirebaseInstanceId.deleteInstanceId</a></code>. It will resume with a new Instance ID the next time you call this method.</p>
+        <div class="devsite-table-wrapper">
+          <table class="responsive">
+            <colgroup>
+              <col width="40%">
+              <col>
+            </colgroup>
+            <thead>
+              <tr>
+                <th colspan="100%">Parameters</th>
+              </tr>
+            </thead>
+            <tbody class="list">
+              <tr>
+                <td><code><a href="https://developer.android.com/reference/kotlin/java/lang/Object.html">Object</a>&nbsp;data</code></td>
+                <td>
+                  <p>Parameters to pass to the endpoint. Defaults to <code>null</code> if not provided.</p>
+                </td>
+              </tr>
+            </tbody>
+          </table>
+        </div>
+        <div class="devsite-table-wrapper">
+          <table class="responsive">
+            <colgroup>
+              <col width="40%">
+              <col>
+            </colgroup>
+            <thead>
+              <tr>
+                <th colspan="100%">Returns</th>
+              </tr>
+            </thead>
+            <tbody class="list">
+              <tr>
+                <td><code>@<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> <a href="https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html">Flow</a>&lt;@<a href="https://developer.android.com/reference/kotlin/androidx/annotation/NonNull.html">NonNull</a> <a href="/docs/reference/android/com/google/firebase/functions/StreamResponse.html">StreamResponse</a>&gt;</code></td>
+                <td>
+                  <p><code><a href="https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html">Flow</a></code> that will emit intermediate data, and the final result, as it is generated by the function.</p>
+                </td>
+              </tr>
+            </tbody>
           </table>
         </div>
         <div class="devsite-table-wrapper">
--- /home/runner/diff/original/firebase-kotlindoc/kotlin/com/google/firebase/functions/HttpsCallableReference.html	2025-03-14 14:17:09.165318367 +0000
+++ /home/runner/diff/modified/firebase-kotlindoc/kotlin/com/google/firebase/functions/HttpsCallableReference.html	2025-03-14 14:14:15.199219945 +0000
@@ -54,6 +54,13 @@
             </td>
           </tr>
           <tr>
+            <td><code><a href="https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html">Flow</a>&lt;<a href="/docs/reference/kotlin/com/google/firebase/functions/StreamResponse.html">StreamResponse</a>&gt;</code></td>
+            <td>
+              <div><code><a href="/docs/reference/kotlin/com/google/firebase/functions/HttpsCallableReference.html#streamAsFlow(kotlin.Any)">streamAsFlow</a>(data:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?)</code></div>
+              <p>Streams data to the specified HTTPS endpoint.</p>
+            </td>
+          </tr>
+          <tr>
             <td><code><a href="/docs/reference/kotlin/com/google/firebase/functions/HttpsCallableReference.html">HttpsCallableReference</a></code></td>
             <td>
               <div><code><a href="/docs/reference/kotlin/com/google/firebase/functions/HttpsCallableReference.html#withTimeout(kotlin.Long,java.util.concurrent.TimeUnit)">withTimeout</a>(timeout:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html">Long</a>,&nbsp;units:&nbsp;<a href="https://developer.android.com/reference/kotlin/java/util/concurrent/TimeUnit.html">TimeUnit</a>)</code></div>
@@ -362,6 +369,131 @@
                 </td>
               </tr>
             </tbody>
+          </table>
+        </div>
+        <div class="devsite-table-wrapper">
+          <table class="responsive">
+            <colgroup>
+              <col width="40%">
+              <col>
+            </colgroup>
+            <thead>
+              <tr>
+                <th colspan="100%">See also</th>
+              </tr>
+            </thead>
+            <tbody class="list">
+              <tr>
+                <td><code><a href="https://developer.android.com/reference/kotlin/org/json/JSONArray.html">JSONArray</a></code></td>
+                <td></td>
+              </tr>
+              <tr>
+                <td><code><a href="https://developer.android.com/reference/kotlin/org/json/JSONObject.html">JSONObject</a></code></td>
+                <td></td>
+              </tr>
+              <tr>
+                <td><code><a href="https://developer.android.com/reference/kotlin/java/io/IOException.html">IOException</a></code></td>
+                <td></td>
+              </tr>
+              <tr>
+                <td><code><a href="/docs/reference/kotlin/com/google/firebase/functions/FirebaseFunctionsException.html">FirebaseFunctionsException</a></code></td>
+                <td></td>
+              </tr>
+            </tbody>
+          </table>
+        </div>
+      </div>
+      <div class="api-item"><a name="streamAsFlow-kotlin.Any-"></a><a name="streamasflow"></a>
+        <h3 class="api-name" id="streamAsFlow(kotlin.Any)">streamAsFlow</h3>
+        <pre class="api-signature no-pretty-print">fun&nbsp;<a href="/docs/reference/kotlin/com/google/firebase/functions/HttpsCallableReference.html#streamAsFlow(kotlin.Any)">streamAsFlow</a>(data:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>? = null):&nbsp;<a href="https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html">Flow</a>&lt;<a href="/docs/reference/kotlin/com/google/firebase/functions/StreamResponse.html">StreamResponse</a>&gt;</pre>
+        <p>Streams data to the specified HTTPS endpoint.</p>
+        <p>The data passed into the trigger can be any of the following types:</p>
+        <ul>
+          <li>
+            <p>Any primitive type, including null, int, long, float, and boolean.</p>
+          </li>
+          <li>
+            <p><code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html">String</a></code></p>
+          </li>
+          <li>
+            <p><code><a href="https://developer.android.com/reference/kotlin/java/util/List.html">List</a></code>, where the contained objects are also one of these types.</p>
+          </li>
+          <li>
+            <p><code><a href="https://developer.android.com/reference/kotlin/java/util/Map.html">Map</a></code>, where the values are also one of these types.</p>
+          </li>
+          <li>
+            <p><code><a href="https://developer.android.com/reference/kotlin/org/json/JSONArray.html">org.json.JSONArray</a></code></p>
+          </li>
+          <li>
+            <p><code><a href="https://developer.android.com/reference/kotlin/org/json/JSONObject.html">org.json.JSONObject</a></code></p>
+          </li>
+          <li>
+            <p><code><a href="https://developer.android.com/reference/kotlin/org/json/JSONObject.html#NULL--">org.json.JSONObject.NULL</a></code></p>
+          </li>
+        </ul>
+        <p>If the returned streamResponse fails, the exception will be one of the following types:</p>
+        <ul>
+          <li>
+            <p><code><a href="https://developer.android.com/reference/kotlin/java/io/IOException.html">java.io.IOException</a></code></p>
+          </li>
+        </ul>
+        <ul>
+          <li>
+            <p>if the HTTPS request failed to connect.</p>
+          </li>
+        </ul>
+        <ul>
+          <li>
+            <p><code><a href="/docs/reference/kotlin/com/google/firebase/functions/FirebaseFunctionsException.html">FirebaseFunctionsException</a></code></p>
+          </li>
+        </ul>
+        <ul>
+          <li>
+            <p>if the request connected, but the function returned an error.</p>
+          </li>
+        </ul>
+        <p>The request to the Cloud Functions backend made by this method automatically includes a Firebase Instance ID token to identify the app instance. If a user is logged in with Firebase Auth, an auth token for the user will also be automatically included.</p>
+        <p>Firebase Instance ID sends data to the Firebase backend periodically to collect information regarding the app instance. To stop this, see <code><a href="/docs/reference/kotlin/com/google/firebase/iid/FirebaseInstanceId.html#deleteInstanceId()">com.google.firebase.iid.FirebaseInstanceId.deleteInstanceId</a></code>. It will resume with a new Instance ID the next time you call this method.</p>
+        <div class="devsite-table-wrapper">
+          <table class="responsive">
+            <colgroup>
+              <col width="40%">
+              <col>
+            </colgroup>
+            <thead>
+              <tr>
+                <th colspan="100%">Parameters</th>
+              </tr>
+            </thead>
+            <tbody class="list">
+              <tr>
+                <td><code>data:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>? = null</code></td>
+                <td>
+                  <p>Parameters to pass to the endpoint. Defaults to <code>null</code> if not provided.</p>
+                </td>
+              </tr>
+            </tbody>
+          </table>
+        </div>
+        <div class="devsite-table-wrapper">
+          <table class="responsive">
+            <colgroup>
+              <col width="40%">
+              <col>
+            </colgroup>
+            <thead>
+              <tr>
+                <th colspan="100%">Returns</th>
+              </tr>
+            </thead>
+            <tbody class="list">
+              <tr>
+                <td><code><a href="https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html">Flow</a>&lt;<a href="/docs/reference/kotlin/com/google/firebase/functions/StreamResponse.html">StreamResponse</a>&gt;</code></td>
+                <td>
+                  <p><code><a href="https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html">Flow</a></code> that will emit intermediate data, and the final result, as it is generated by the function.</p>
+                </td>
+              </tr>
+            </tbody>
           </table>
         </div>
         <div class="devsite-table-wrapper">

@google-oss-bot
Copy link
Contributor

1 Warning
⚠️ Did you forget to add a changelog entry? (Add the 'no-changelog' label to the PR to silence this warning.)

Generated by 🚫 Danger

Copy link
Contributor

github-actions bot commented Mar 14, 2025

Test Results

14 files  ±0  14 suites  ±0   12s ⏱️ ±0s
23 tests ±0  23 ✅ ±0  0 💤 ±0  0 ❌ ±0 
46 runs  ±0  46 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit eec98b2. ± Comparison against base commit b9be9f1.

♻️ This comment has been updated with latest results.

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Mar 14, 2025

Coverage Report 1

Affected Products

  • firebase-functions

    Overall coverage changed from 24.90% (b9be9f1) to 24.86% (8443f4e) by -0.03%.

    FilenameBase (b9be9f1)Merge (8443f4e)Diff
    HttpsCallableReference.kt44.83%43.33%-1.49%

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/id93QLiqiO.html

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Mar 14, 2025

Size Report 1

Affected Products

  • firebase-functions

    TypeBase (b9be9f1)Merge (8443f4e)Diff
    aar80.4 kB80.6 kB+208 B (+0.3%)
    apk (aggressive)439 kB440 kB+170 B (+0.0%)
    apk (release)5.46 MB5.55 MB+91.7 kB (+1.7%)

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/E38in4dnSj.html

@rlazo
Copy link
Collaborator

rlazo commented Mar 14, 2025

The alternative solution sounds good too. We can probably mix them by exporting reactive streams as api, as it should, and using the asflow with the library underneath

rlazo pushed a commit that referenced this pull request Mar 14, 2025
…6775)

Alternative to #6774 - we'll keep that PR for a follow-up release.

For now, we're exposing the `reactive-streams` dependency to make sure
it works out of the box for Java developers.
gubatron pushed a commit to gubatron/firebase-android-sdk that referenced this pull request Mar 24, 2025
…irebase#6775)

Alternative to firebase#6774 - we'll keep that PR for a follow-up release.

For now, we're exposing the `reactive-streams` dependency to make sure
it works out of the box for Java developers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants