Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 019db8e

Browse files
DavoBRcollinjackson
authored andcommitted
[cloud_functions] Support for cloud functions emulators (flutter#1887)
* Allowing use emulator functions Co-Authored-By: Collin Jackson <jackson@google.com>
1 parent f212178 commit 019db8e

7 files changed

Lines changed: 50 additions & 9 deletions

File tree

packages/cloud_functions/CHANGELOG.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.4.1
2+
3+
* Support for cloud functions emulators.
4+
15
## 0.4.0+3
26

37
* Update google-services Android gradle plugin to 4.3.0 in documentation and examples.
@@ -93,7 +97,7 @@
9397
[Callable functions](https://firebase.google.com/docs/functions/callable)
9498
are similar to other HTTP functions, with these additional features:
9599

96-
- With callables, Firebase Authentication and FCM tokens are
97-
automatically included in requests.
98-
- The functions.https.onCall trigger automatically deserializes
99-
the request body and validates auth tokens.
100+
- With callables, Firebase Authentication and FCM tokens are
101+
automatically included in requests.
102+
- The functions.https.onCall trigger automatically deserializes
103+
the request body and validates auth tokens.

packages/cloud_functions/android/src/main/java/io/flutter/plugins/firebase/cloudfunctions/CloudFunctionsPlugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ public void onMethodCall(MethodCall call, final Result result) {
3838
String appName = call.argument("app");
3939
FirebaseApp app = FirebaseApp.getInstance(appName);
4040
String region = call.argument("region");
41+
String origin = call.argument("origin");
4142
FirebaseFunctions functions;
4243
if (region != null) {
4344
functions = FirebaseFunctions.getInstance(app, region);
4445
} else {
4546
functions = FirebaseFunctions.getInstance(app);
4647
}
48+
if (origin != null) {
49+
functions.useFunctionsEmulator(origin);
50+
}
4751
HttpsCallableReference httpsCallableReference = functions.getHttpsCallable(functionName);
4852
Number timeoutMilliseconds = call.argument("timeoutMilliseconds");
4953
if (timeoutMilliseconds != null) {

packages/cloud_functions/ios/Classes/CloudFunctionsPlugin.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
4444
NSObject *parameters = call.arguments[@"parameters"];
4545
NSString *appName = call.arguments[@"app"];
4646
NSString *region = call.arguments[@"region"];
47+
NSString *origin = call.arguments[@"origin"];
4748
NSNumber *timeoutMicroseconds = call.arguments[@"timeoutMicroseconds"];
4849
FIRApp *app = [FIRApp appNamed:appName];
4950
FIRFunctions *functions;
@@ -52,6 +53,9 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
5253
} else {
5354
functions = [FIRFunctions functionsForApp:app];
5455
}
56+
if (origin != nil && origin != (id)[NSNull null]) {
57+
[functions useFunctionsEmulatorOrigin:origin];
58+
}
5559
FIRHTTPSCallable *function = [functions HTTPSCallableWithName:functionName];
5660
if (timeoutMicroseconds != nil && timeoutMicroseconds != [NSNull null]) {
5761
[function setTimeoutInterval:(NSTimeInterval)timeoutMicroseconds.doubleValue / 1000000];

packages/cloud_functions/lib/src/cloud_functions.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ class CloudFunctionsException implements Exception {
1616
///
1717
/// You can get an instance by calling [CloudFunctions.instance].
1818
class CloudFunctions {
19-
CloudFunctions({FirebaseApp app, String region})
19+
CloudFunctions({FirebaseApp app, String region, String origin})
2020
: _app = app ?? FirebaseApp.instance,
21-
_region = region;
21+
_region = region,
22+
_origin = origin;
2223

2324
@visibleForTesting
2425
static const MethodChannel channel = MethodChannel('cloud_functions');
@@ -31,6 +32,8 @@ class CloudFunctions {
3132

3233
final String _region;
3334

35+
String _origin;
36+
3437
/// Gets an instance of a Callable HTTPS trigger in Cloud Functions.
3538
///
3639
/// Can then be executed by calling `call()` on it.
@@ -39,4 +42,12 @@ class CloudFunctions {
3942
HttpsCallable getHttpsCallable({@required String functionName}) {
4043
return HttpsCallable._(this, functionName);
4144
}
45+
46+
/// Changes this instance to point to a Cloud Functions emulator running locally.
47+
///
48+
/// @param origin The origin of the local emulator, such as "//10.0.2.2:5005".
49+
CloudFunctions useFunctionsEmulator({@required String origin}) {
50+
_origin = origin;
51+
return this;
52+
}
4253
}

packages/cloud_functions/lib/src/https_callable.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class HttpsCallable {
3434
.invokeMethod<dynamic>('CloudFunctions#call', <String, dynamic>{
3535
'app': _cloudFunctions._app.name,
3636
'region': _cloudFunctions._region,
37+
'origin': _cloudFunctions._origin,
3738
'timeoutMicroseconds': timeout?.inMicroseconds,
3839
'functionName': _functionName,
3940
'parameters': parameters,

packages/cloud_functions/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: cloud_functions
22
description: Flutter plugin for Cloud Functions.
3-
version: 0.4.0+3
3+
version: 0.4.1
44
author: Flutter Team <flutter-dev@googlegroups.com>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/cloud_functions
66

@@ -23,5 +23,5 @@ dev_dependencies:
2323
test: any
2424

2525
environment:
26-
sdk: ">=2.0.0-dev.28.0 <3.0.0"
27-
flutter: ">=0.2.4 <2.0.0"
26+
sdk: '>=2.0.0-dev.28.0 <3.0.0'
27+
flutter: '>=0.2.4 <2.0.0'

packages/cloud_functions/test/cloud_functions_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ void main() {
3838
await callable.call(<String, dynamic>{
3939
'quux': 'quuz',
4040
});
41+
await CloudFunctions.instance
42+
.useFunctionsEmulator(origin: 'http://localhost:5001')
43+
.getHttpsCallable(functionName: 'bez')
44+
.call();
4145
expect(
4246
log,
4347
<Matcher>[
@@ -46,6 +50,7 @@ void main() {
4650
arguments: <String, dynamic>{
4751
'app': '[DEFAULT]',
4852
'region': null,
53+
'origin': null,
4954
'functionName': 'baz',
5055
'timeoutMicroseconds': null,
5156
'parameters': null,
@@ -56,11 +61,23 @@ void main() {
5661
arguments: <String, dynamic>{
5762
'app': '1337',
5863
'region': 'space',
64+
'origin': null,
5965
'functionName': 'qux',
6066
'timeoutMicroseconds': (const Duration(days: 300)).inMicroseconds,
6167
'parameters': <String, dynamic>{'quux': 'quuz'},
6268
},
6369
),
70+
isMethodCall(
71+
'CloudFunctions#call',
72+
arguments: <String, dynamic>{
73+
'app': '[DEFAULT]',
74+
'region': null,
75+
'origin': 'http://localhost:5001',
76+
'functionName': 'bez',
77+
'timeoutMicroseconds': null,
78+
'parameters': null,
79+
},
80+
),
6481
],
6582
);
6683
});

0 commit comments

Comments
 (0)