Skip to content

[cloud_functions] Prepare for web implementation #1826

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

Merged
merged 8 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ build/
.flutter-plugins

ios/Classes/UserAgent.h

.flutter-plugins-dependencies

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# the new lints that are already failing on this plugin, for this plugin. It
# should be deleted and the failing lints addressed as soon as possible.

include: ../../analysis_options.yaml
include: ../../../analysis_options.yaml

analyzer:
errors:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

Initial release
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# cloud_functions_platform_interface

A common platform interface for the [`cloud_functions`][1] plugin.

This interface allows platform-specific implementations of the `cloud_functions`
plugin, as well as the plugin itself, to ensure they are supporting the
same interface.

# Usage

To implement a new platform-specific implementation of `cloud_functions`, extend
[`CloudFunctionsPlatform`][2] with an implementation that performs the
platform-specific behavior, and when you register your plugin, set the default
`CloudFunctionsPlatform` by calling
`CloudFunctionsPlatform.instance = MyCloudFunctions()`.

# Note on breaking changes

Strongly prefer non-breaking changes (such as adding a method to the interface)
over breaking changes for this package.

See https://flutter.dev/go/platform-interface-breaking-changes for a discussion
on why a less-clean interface is preferable to a breaking change.

[1]: ../cloud_functions
[2]: lib/cloud_functions_platform_interface.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2020 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.

library cloud_functions_platform_interface;

import 'dart:async';

import 'package:flutter/services.dart';
import 'package:meta/meta.dart' show required, visibleForTesting;
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

part 'src/method_channel_cloud_functions.dart';

/// The interface that implementations of `cloud_functions` must extend.
///
/// Platform implementations should extend this class rather than implement it
/// as `cloud_functions` does not consider newly added methods to be breaking
/// changes. Extending this class (using `extends`) ensures that the subclass
/// will get the default implementation, while platform implementations that
/// `implements` this interface will be broken by newly added
/// [CloudFunctionsPlatform] methods.
abstract class CloudFunctionsPlatform extends PlatformInterface {
static final Object _token = Object();

/// Constructs a CloudFunctionsPlatform.
CloudFunctionsPlatform() : super(token: _token);

/// The default instance of [CloudFunctionsPlatform] to use.
///
/// Platform-specific plugins should override this with their own class
/// that extends [CloudFunctionsPlatform] when they register themselves.
///
/// Defaults to [MethodChannelCloudFunctions].
static CloudFunctionsPlatform get instance => _instance;

static CloudFunctionsPlatform _instance = MethodChannelCloudFunctions();

/// Platform-specific plugins should set this with their own platform-specific
/// class that extends [CloudFunctionsPlatform] when they register themselves.
// TODO(amirh): Extract common platform interface logic.
// https://github.com/flutter/flutter/issues/43368
static set instance(CloudFunctionsPlatform instance) {
PlatformInterface.verifyToken(instance, _token);
_instance = instance;
}

/// Invokes the specified cloud function.
///
/// The required parameters, [appName] and [functionName], specify which
/// cloud function will be called.
///
/// The rest of the parameters are optional and used to invoke the function
/// with something other than the defaults. [region] defaults to `us-central1`
/// and [timeout] defaults to 60 seconds.
///
/// The [origin] parameter may be used to provide the base URL for the function.
/// This can be used to send requests to a local emulator.
///
/// The data passed into the cloud function via [parameters] can be any of the following types:
///
/// `null`
/// `String`
/// `num`
/// [List], where the contained objects are also one of these types.
/// [Map], where the values are also one of these types.
Future<dynamic> callCloudFunction({
@required String appName,
@required String functionName,
String region,
String origin,
Duration timeout,
dynamic parameters,
}) {
throw UnimplementedError('callCloudFunction() has not been implemented');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2020 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.

part of cloud_functions_platform_interface;

/// [CloudFunctionsPlatform] implementation that delegates to a [MethodChannel].
class MethodChannelCloudFunctions extends CloudFunctionsPlatform {
/// The [MethodChannel] to which calls will be delegated.
@visibleForTesting
static const MethodChannel channel = MethodChannel(
'plugins.flutter.io/cloud_functions',
);

/// Invokes the specified cloud function.
///
/// The required parameters, [appName] and [functionName], specify which
/// cloud function will be called.
///
/// The rest of the parameters are optional and used to invoke the function
/// with something other than the defaults. [region] defaults to `us-central1`
/// and [timeout] defaults to 60 seconds.
///
/// The [origin] parameter may be used to provide the base URL for the function.
/// This can be used to send requests to a local emulator.
///
/// The data passed into the cloud function via [parameters] can be any of the following types:
///
/// `null`
/// `String`
/// `num`
/// [List], where the contained objects are also one of these types.
/// [Map], where the values are also one of these types.
@override
Future<dynamic> callCloudFunction({
@required String appName,
@required String functionName,
String region,
String origin,
Duration timeout,
dynamic parameters,
}) =>
channel.invokeMethod<dynamic>('CloudFunctions#call', <String, dynamic>{
'app': appName,
'region': region,
'origin': origin,
'timeoutMicroseconds': timeout?.inMicroseconds,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that mean that the existing implementation is a bug?
https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_functions/lib/src/https_callable.dart#L38

I mean, I'm happy to make the change if it's the right thing to do, but I don't want to break something that works, currently.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the Android code is the odd one out. iOS is also using microseconds:

NSNumber *timeoutMicroseconds = call.arguments[@"timeoutMicroseconds"];

https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_functions/ios/Classes/CloudFunctionsPlugin.m#L48

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it looks like a bug in the Android implementation

'functionName': functionName,
'parameters': parameters,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: cloud_functions_platform_interface
description: A common platform interface for the cloud_functions plugin.
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/cloud_functions/cloud_functions_platform_interface
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 1.0.0

dependencies:
flutter:
sdk: flutter
meta: ^1.0.5
firebase_core: ^0.4.0
plugin_platform_interface: ^1.0.1

dev_dependencies:
flutter_test:
sdk: flutter
mockito: ^4.1.1

environment:
sdk: ">=2.0.0-dev.28.0 <3.0.0"
flutter: ">=1.9.1+hotfix.5 <2.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2020 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.

import 'package:cloud_functions_platform_interface/cloud_functions_platform_interface.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group('$CloudFunctionsPlatform()', () {
test('$MethodChannelCloudFunctions is the default instance', () {
expect(
CloudFunctionsPlatform.instance, isA<MethodChannelCloudFunctions>());
});

test('Cannot be implemented with `implements`', () {
expect(() {
CloudFunctionsPlatform.instance = ImplementsCloudFunctionsPlatform();
}, throwsAssertionError);
});

test('Can be extended', () {
CloudFunctionsPlatform.instance = ExtendsCloudFunctionsPlatform();
});

test('Can be mocked with `implements`', () {
final CloudFunctionsPlatform mock = MocksCloudFunctionsPlatform();
CloudFunctionsPlatform.instance = mock;
});
});
}

class ImplementsCloudFunctionsPlatform extends Mock
implements CloudFunctionsPlatform {}

class MocksCloudFunctionsPlatform extends Mock
with MockPlatformInterfaceMixin
implements CloudFunctionsPlatform {}

class ExtendsCloudFunctionsPlatform extends CloudFunctionsPlatform {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2018-2020 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.

import 'package:cloud_functions_platform_interface/cloud_functions_platform_interface.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group('$CloudFunctionsPlatform', () {
final List<MethodCall> log = <MethodCall>[];

setUp(() async {
MethodChannelCloudFunctions.channel
.setMockMethodCallHandler((MethodCall methodCall) async {
log.add(methodCall);
switch (methodCall.method) {
case 'FirebaseFunctions#call':
return <String, dynamic>{
'foo': 'bar',
};
default:
return true;
}
});
log.clear();
});

test('call', () async {
final String appName = FirebaseApp.instance.name;
await CloudFunctionsPlatform.instance
.callCloudFunction(appName: appName, functionName: 'baz');

Map<String, String> params = {'quux': 'quuz'};
await CloudFunctionsPlatform.instance.callCloudFunction(
appName: '1337',
functionName: 'qux',
region: 'space',
timeout: Duration(days: 300),
parameters: params,
);

await CloudFunctionsPlatform.instance.callCloudFunction(
appName: appName,
functionName: 'bez',
origin: 'http://localhost:5001',
);

expect(
log,
<Matcher>[
isMethodCall(
'CloudFunctions#call',
arguments: <String, dynamic>{
'app': '[DEFAULT]',
'region': null,
'origin': null,
'functionName': 'baz',
'timeoutMicroseconds': null,
'parameters': null,
},
),
isMethodCall(
'CloudFunctions#call',
arguments: <String, dynamic>{
'app': '1337',
'region': 'space',
'origin': null,
'functionName': 'qux',
'timeoutMicroseconds': (const Duration(days: 300)).inMicroseconds,
'parameters': <String, dynamic>{'quux': 'quuz'},
},
),
isMethodCall(
'CloudFunctions#call',
arguments: <String, dynamic>{
'app': '[DEFAULT]',
'region': null,
'origin': 'http://localhost:5001',
'functionName': 'bez',
'timeoutMicroseconds': null,
'parameters': null,
},
),
],
);
});
});
}

This file was deleted.

This file was deleted.