Skip to content

Commit 448db93

Browse files
collinjacksonidealopamp
authored andcommitted
cloud functions multiple app support (flutter#1210)
1 parent 9a831de commit 448db93

6 files changed

Lines changed: 74 additions & 34 deletions

File tree

packages/cloud_functions/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.1
2+
3+
* Support for regions and multiple apps
4+
15
## 0.1.0+1
26

37
* Log a more detailed warning at build time about the previous AndroidX

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import androidx.annotation.NonNull;
88
import com.google.android.gms.tasks.OnCompleteListener;
99
import com.google.android.gms.tasks.Task;
10+
import com.google.firebase.FirebaseApp;
1011
import com.google.firebase.functions.FirebaseFunctions;
1112
import com.google.firebase.functions.FirebaseFunctionsException;
1213
import com.google.firebase.functions.HttpsCallableReference;
@@ -32,9 +33,17 @@ public void onMethodCall(MethodCall call, final Result result) {
3233
switch (call.method) {
3334
case "CloudFunctions#call":
3435
String functionName = call.argument("functionName");
35-
HttpsCallableReference httpsCallableReference =
36-
FirebaseFunctions.getInstance().getHttpsCallable(functionName);
3736
Map<String, Object> parameters = call.argument("parameters");
37+
String appName = call.argument("app");
38+
FirebaseApp app = FirebaseApp.getInstance(appName);
39+
String region = call.argument("region");
40+
FirebaseFunctions functions;
41+
if (region != null) {
42+
functions = FirebaseFunctions.getInstance(app, region);
43+
} else {
44+
functions = FirebaseFunctions.getInstance(app);
45+
}
46+
HttpsCallableReference httpsCallableReference = functions.getHttpsCallable(functionName);
3847
httpsCallableReference
3948
.call(parameters)
4049
.addOnCompleteListener(

packages/cloud_functions/ios/Classes/CloudFunctionsPlugin.m

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,46 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
3535
if ([@"CloudFunctions#call" isEqualToString:call.method]) {
3636
NSString *functionName = call.arguments[@"functionName"];
3737
NSObject *parameters = call.arguments[@"parameters"];
38-
[[FIRFunctions functions]
39-
callFunction:functionName
40-
withObject:parameters
41-
completion:^(FIRHTTPSCallableResult *callableResult, NSError *error) {
42-
if (error) {
43-
FlutterError *flutterError;
44-
if (error.domain == FIRFunctionsErrorDomain) {
45-
NSDictionary *details = [NSMutableDictionary dictionary];
46-
[details setValue:[self mapFunctionsErrorCodes:error.code] forKey:@"code"];
47-
if (error.localizedDescription != nil) {
48-
[details setValue:error.localizedDescription forKey:@"message"];
49-
}
50-
if (error.userInfo[FIRFunctionsErrorDetailsKey] != nil) {
51-
[details setValue:error.userInfo[FIRFunctionsErrorDetailsKey] forKey:@"details"];
52-
}
38+
NSString *appName = call.arguments[@"app"];
39+
NSString *region = call.arguments[@"region"];
40+
FIRApp *app = [FIRApp appNamed:appName];
41+
FIRFunctions *functions;
42+
if (region != nil) {
43+
functions = [FIRFunctions functionsForApp:app region:region];
44+
} else {
45+
functions = [FIRFunctions functionsForApp:app];
46+
}
47+
[functions callFunction:functionName
48+
withObject:parameters
49+
completion:^(FIRHTTPSCallableResult *callableResult, NSError *error) {
50+
if (error) {
51+
FlutterError *flutterError;
52+
if (error.domain == FIRFunctionsErrorDomain) {
53+
NSDictionary *details = [NSMutableDictionary dictionary];
54+
[details setValue:[self mapFunctionsErrorCodes:error.code] forKey:@"code"];
55+
if (error.localizedDescription != nil) {
56+
[details setValue:error.localizedDescription forKey:@"message"];
57+
}
58+
if (error.userInfo[FIRFunctionsErrorDetailsKey] != nil) {
59+
[details setValue:error.userInfo[FIRFunctionsErrorDetailsKey]
60+
forKey:@"details"];
61+
}
5362

54-
flutterError =
55-
[FlutterError errorWithCode:@"functionsError"
56-
message:@"Firebase function failed with exception."
57-
details:details];
58-
} else {
59-
flutterError = [FlutterError errorWithCode:nil
60-
message:error.localizedDescription
61-
details:nil];
62-
}
63-
result(flutterError);
64-
} else {
65-
result(callableResult.data);
66-
}
67-
}];
63+
flutterError =
64+
[FlutterError errorWithCode:@"functionsError"
65+
message:@"Firebase function failed with exception."
66+
details:details];
67+
} else {
68+
flutterError = [FlutterError
69+
errorWithCode:[NSString stringWithFormat:@"%ld", error.code]
70+
message:error.localizedDescription
71+
details:nil];
72+
}
73+
result(flutterError);
74+
} else {
75+
result(callableResult.data);
76+
}
77+
}];
6878
} else {
6979
result(FlutterMethodNotImplemented);
7080
}

packages/cloud_functions/lib/cloud_functions.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66

7+
import 'package:firebase_core/firebase_core.dart';
78
import 'package:flutter/services.dart';
89
import 'package:meta/meta.dart';
910

@@ -19,13 +20,21 @@ class CloudFunctionsException implements Exception {
1920
///
2021
/// You can get an instance by calling [CloudFunctions.instance].
2122
class CloudFunctions {
23+
CloudFunctions({FirebaseApp app, String region})
24+
: _app = app ?? FirebaseApp.instance,
25+
_region = region;
26+
2227
@visibleForTesting
2328
static const MethodChannel channel = MethodChannel('cloud_functions');
2429

2530
static CloudFunctions _instance = CloudFunctions();
2631

2732
static CloudFunctions get instance => _instance;
2833

34+
final FirebaseApp _app;
35+
36+
final String _region;
37+
2938
/// Executes this Callable HTTPS trigger asynchronously.
3039
///
3140
/// @param functionName The name of the callable function being triggered.
@@ -38,6 +47,8 @@ class CloudFunctions {
3847
// https://github.com/flutter/flutter/issues/26431
3948
// ignore: strong_mode_implicit_dynamic_method
4049
await channel.invokeMethod('CloudFunctions#call', <String, dynamic>{
50+
'app': _app.name,
51+
'region': _region,
4152
'functionName': functionName,
4253
'parameters': parameters,
4354
});

packages/cloud_functions/pubspec.yaml

Lines changed: 2 additions & 2 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.1.0+1
3+
version: 0.1.1
44
author: Flutter Team <[email protected]>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/cloud_functions
66

@@ -13,11 +13,11 @@ dependencies:
1313
meta: ^1.1.6
1414
flutter:
1515
sdk: flutter
16+
firebase_core: ^0.3.0
1617

1718
dev_dependencies:
1819
flutter_test:
1920
sdk: flutter
20-
firebase_core: ^0.3.0
2121

2222
environment:
2323
sdk: ">=2.0.0-dev.28.0 <3.0.0"

packages/cloud_functions/test/cloud_functions_test.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:cloud_functions/cloud_functions.dart';
6+
import 'package:firebase_core/firebase_core.dart';
67
import 'package:flutter/services.dart';
78
import 'package:flutter_test/flutter_test.dart';
89

@@ -28,7 +29,8 @@ void main() {
2829

2930
test('call', () async {
3031
await CloudFunctions.instance.call(functionName: 'baz');
31-
await CloudFunctions.instance
32+
await CloudFunctions(
33+
app: const FirebaseApp(name: '1337'), region: 'space')
3234
.call(functionName: 'qux', parameters: <String, dynamic>{
3335
'quux': 'quuz',
3436
});
@@ -38,13 +40,17 @@ void main() {
3840
isMethodCall(
3941
'CloudFunctions#call',
4042
arguments: <String, dynamic>{
43+
'app': '[DEFAULT]',
44+
'region': null,
4145
'functionName': 'baz',
4246
'parameters': null,
4347
},
4448
),
4549
isMethodCall(
4650
'CloudFunctions#call',
4751
arguments: <String, dynamic>{
52+
'app': '1337',
53+
'region': 'space',
4854
'functionName': 'qux',
4955
'parameters': <String, dynamic>{
5056
'quux': 'quuz',

0 commit comments

Comments
 (0)