-
Notifications
You must be signed in to change notification settings - Fork 4k
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(cloud_functions): add support for cloud functions stream #17214
Open
SelaseKay
wants to merge
26
commits into
main
Choose a base branch
from
feat/cloud_functions_stream_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
5bbf13d
chore: add platform interface and method channel implementation for C…
SelaseKay 24acdda
chore: add `httpsCallableStreamFromUrl` and `httpsStreamCallableWithUri`
SelaseKay fa00dd9
chore: resolve comments
SelaseKay fa750ed
chore: add Android implementation for Cloud Functions stream
SelaseKay 3e8ac29
chore: resolve formatting issues
SelaseKay c248cd6
chore: correct variable name
SelaseKay 1ac4533
chore: add support for Cloud Functions Stream(Android)
SelaseKay d8e0fce
Merge branch 'main' into feat/cloud_functions_stream_support
SelaseKay 95236c8
chore: create dedicated StreamHandler class
SelaseKay 785e019
Merge branch 'main' into feat/cloud_functions_stream_support
SelaseKay 6161988
chore: add streamhandler implementation for ios
SelaseKay debdc46
Merge branch 'main' into feat/cloud_functions_stream_support
SelaseKay 4d0c10e
chore: add iOS implementation for Cloud Functions stream
SelaseKay 1190fde
chore: add license header to stream handler files
SelaseKay 6bbde2f
chore: web Cloud Functions stream wip
SelaseKay 66be89b
chore: push all
SelaseKay 4804ab9
chore: update functions based on API Doc modification
SelaseKay 4f83c36
chore: clean up code
SelaseKay 213e283
chore: add web package
SelaseKay de10bcc
Merge branch 'main' into feat/cloud_functions_stream_support
SelaseKay 794d441
chore: add streaming example
SelaseKay 560e3eb
Merge branch 'feat/cloud_functions_stream_support' of github.com:fire…
SelaseKay 6ad0820
chore: fix ci issues
SelaseKay a9819db
chore: fix ci
SelaseKay 295c6c6
chore: fix cloud function test
SelaseKay 7a5ad4e
chore: add missing doc
SelaseKay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2,923 changes: 971 additions & 1,952 deletions
2,923
.github/workflows/scripts/functions/package-lock.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...d/src/main/java/io/flutter/plugins/firebase/functions/FirebaseFunctionsStreamHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.firebase.functions; | ||
|
||
import android.net.Uri; | ||
import com.google.firebase.functions.FirebaseFunctions; | ||
import com.google.firebase.functions.HttpsCallableOptions; | ||
import com.google.firebase.functions.HttpsCallableReference; | ||
import com.google.firebase.functions.StreamResponse; | ||
import io.flutter.plugin.common.EventChannel; | ||
import io.flutter.plugin.common.EventChannel.StreamHandler; | ||
import java.net.URL; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.TimeUnit; | ||
import org.reactivestreams.Publisher; | ||
|
||
public class FirebaseFunctionsStreamHandler implements StreamHandler { | ||
|
||
private final FirebaseFunctions firebaseFunctions; | ||
|
||
private StreamResponseSubscriber subscriber; | ||
|
||
public FirebaseFunctionsStreamHandler(FirebaseFunctions functions) { | ||
this.firebaseFunctions = functions; | ||
} | ||
|
||
@Override | ||
public void onListen(Object arguments, EventChannel.EventSink events) { | ||
@SuppressWarnings("unchecked") | ||
Map<String, Object> argumentsMap = (Map<String, Object>) arguments; | ||
httpsStreamCall(argumentsMap, events); | ||
} | ||
|
||
@Override | ||
public void onCancel(Object arguments) { | ||
subscriber.cancel(); | ||
} | ||
|
||
private void httpsStreamCall(Map<String, Object> arguments, EventChannel.EventSink events) { | ||
try { | ||
|
||
String functionName = (String) arguments.get("functionName"); | ||
String functionUri = (String) arguments.get("functionUri"); | ||
String origin = (String) arguments.get("origin"); | ||
Integer timeout = (Integer) arguments.get("timeout"); | ||
Object parameters = arguments.get("parameters"); | ||
boolean limitedUseAppCheckToken = | ||
(boolean) Objects.requireNonNull(arguments.get("limitedUseAppCheckToken")); | ||
|
||
if (origin != null) { | ||
Uri originUri = Uri.parse(origin); | ||
firebaseFunctions.useEmulator(originUri.getHost(), originUri.getPort()); | ||
} | ||
|
||
HttpsCallableReference httpsCallableReference; | ||
HttpsCallableOptions options = | ||
new HttpsCallableOptions.Builder() | ||
.setLimitedUseAppCheckTokens(limitedUseAppCheckToken) | ||
.build(); | ||
|
||
Publisher<StreamResponse> publisher; | ||
if (functionName != null) { | ||
httpsCallableReference = firebaseFunctions.getHttpsCallable(functionName, options); | ||
publisher = httpsCallableReference.stream(parameters); | ||
} else if (functionUri != null) { | ||
httpsCallableReference = | ||
firebaseFunctions.getHttpsCallableFromUrl(new URL(functionUri), options); | ||
publisher = httpsCallableReference.stream(); | ||
} else { | ||
throw new IllegalArgumentException("Either functionName or functionUri must be set"); | ||
} | ||
|
||
if (timeout != null) { | ||
httpsCallableReference.setTimeout(timeout.longValue(), TimeUnit.MILLISECONDS); | ||
} | ||
subscriber = new StreamResponseSubscriber(events); | ||
publisher.subscribe(subscriber); | ||
} catch (Exception e) { | ||
events.error("firebase_functions", e.getMessage(), null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...android/src/main/java/io/flutter/plugins/firebase/functions/StreamResponseSubscriber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.firebase.functions; | ||
|
||
import android.os.Handler; | ||
import android.os.Looper; | ||
import com.google.firebase.functions.StreamResponse; | ||
import io.flutter.plugin.common.EventChannel; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
|
||
public class StreamResponseSubscriber implements Subscriber<StreamResponse> { | ||
private Subscription subscription; | ||
private final EventChannel.EventSink eventSink; | ||
|
||
private final Handler mainThreadHandler = new Handler(Looper.getMainLooper()); | ||
|
||
public StreamResponseSubscriber(EventChannel.EventSink eventSink) { | ||
this.eventSink = eventSink; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription s) { | ||
this.subscription = s; | ||
subscription.request(Long.MAX_VALUE); | ||
} | ||
|
||
@Override | ||
public void onNext(StreamResponse streamResponse) { | ||
Map<String, Object> responseMap = new HashMap<>(); | ||
if (streamResponse instanceof StreamResponse.Message) { | ||
Object message = ((StreamResponse.Message) streamResponse).getMessage().getData(); | ||
responseMap.put("message", message); | ||
mainThreadHandler.post(() -> eventSink.success(responseMap)); | ||
} else { | ||
Object result = ((StreamResponse.Result) streamResponse).getResult().getData(); | ||
responseMap.put("result", result); | ||
mainThreadHandler.post(() -> eventSink.success(responseMap)); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
if (eventSink != null) { | ||
eventSink.error("firebase_functions", t.getMessage(), null); | ||
} | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
if (eventSink != null) { | ||
eventSink.endOfStream(); | ||
} | ||
} | ||
|
||
public void cancel() { | ||
if (subscription != null) { | ||
subscription.cancel(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why we would need an external library to handle this? We managed to do all the other implementations without it AFAIK/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. The other ones provided a way to add listeners directly (eg. addOnConfigUpdatedListener) whereas this one doesn't. It returns a
Publisher
so this is the only way around it AFAIK.Reference: https://firebase.google.com/docs/reference/kotlin/com/google/firebase/functions/HttpsCallableReference#summary