This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Add Cloud Functions support #592
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
54e0f57
Draft firebase function support
kroikie 5ff33a3
Update plugin to cloud_functions
kroikie ba74c93
Update example
kroikie 4e90423
Fix tests
kroikie 0d5d92c
Fix example test
kroikie 1ee0ca0
Use Java 1.7 compat syntax
kroikie 7635ec1
Add description to CHANGELOG
kroikie 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
This file contains hidden or 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,33 @@ | ||
.DS_Store | ||
.atom/ | ||
.idea/ | ||
.vscode/ | ||
|
||
.packages | ||
.pub/ | ||
.dart_tool/ | ||
pubspec.lock | ||
|
||
Podfile | ||
Podfile.lock | ||
Pods/ | ||
.symlinks/ | ||
**/Flutter/App.framework/ | ||
**/Flutter/Flutter.framework/ | ||
**/Flutter/Generated.xcconfig | ||
**/Flutter/flutter_assets/ | ||
ServiceDefinitions.json | ||
xcuserdata/ | ||
|
||
local.properties | ||
.gradle/ | ||
gradlew | ||
gradlew.bat | ||
gradle-wrapper.jar | ||
*.iml | ||
|
||
GeneratedPluginRegistrant.h | ||
GeneratedPluginRegistrant.m | ||
GeneratedPluginRegistrant.java | ||
build/ | ||
.flutter-plugins |
This file contains hidden or 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,13 @@ | ||
## 0.0.1 | ||
|
||
* The Cloud Functions for Firebase client SDKs let you call functions | ||
directly from a Firebase app. This plugin exposes this ability to | ||
Flutter apps. | ||
|
||
[Callable functions](https://firebase.google.com/docs/functions/callable) | ||
are similar to other HTTP functions, with these additional features: | ||
|
||
- With callables, Firebase Authentication and FCM tokens are | ||
automatically included in requests. | ||
- The functions.https.onCall trigger automatically deserializes | ||
the request body and validates auth tokens. |
This file contains hidden or 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,26 @@ | ||
Copyright 2018, the Chromium project 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. |
This file contains hidden or 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,40 @@ | ||
# Cloud Functions Plugin for Flutter | ||
|
||
A Flutter plugin to use the [Cloud Functions for Firebase API](https://firebase.google.com/docs/functions/callable) | ||
|
||
[](https://pub.dartlang.org/packages/cloud_functions) | ||
|
||
For Flutter plugins for other Firebase products, see [FlutterFire.md](https://github.com/flutter/plugins/blob/master/FlutterFire.md). | ||
|
||
*Note*: This plugin is still under development, and some APIs might not be available yet. [Feedback](https://github.com/flutter/flutter/issues) and [Pull Requests](https://github.com/flutter/plugins/pulls) are most welcome! | ||
|
||
## Setup | ||
|
||
To use this plugin: | ||
|
||
1. Using the [Firebase Console](http://console.firebase.google.com/), add an Android app to your project: | ||
Follow the assistant, download the generated google-services.json file and place it inside android/app. Next, | ||
modify the android/build.gradle file and the android/app/build.gradle file to add the Google services plugin | ||
as described by the Firebase assistant. Ensure that your `android/build.gradle` file contains the | ||
`maven.google.com` as [described here](https://firebase.google.com/docs/android/setup#add_the_sdk). | ||
1. Using the [Firebase Console](http://console.firebase.google.com/), add an iOS app to your project: | ||
Follow the assistant, download the generated GoogleService-Info.plist file, open ios/Runner.xcworkspace | ||
with Xcode, and within Xcode place the file inside ios/Runner. Don't follow the steps named | ||
"Add Firebase SDK" and "Add initialization code" in the Firebase assistant. | ||
1. Add `cloud_functions` as a [dependency in your pubspec.yaml file](https://flutter.io/platform-plugins/). | ||
|
||
## Usage | ||
|
||
``` | ||
dart import 'package:cloud_functions/cloud_functions.dart'; | ||
``` | ||
|
||
Calling a function: | ||
|
||
``` | ||
dynamic resp = await FirebaseFunctions.instance.call(functionName: 'YOUR_CALLABLE_FUNCTION_NAME'); | ||
``` | ||
|
||
## Getting Started | ||
|
||
See the `example` directory for a complete sample app using Cloud Functions for Firebase. |
This file contains hidden or 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,37 @@ | ||
group 'io.flutter.plugins.firebase.cloudfunctions.cloudfunctions' | ||
version '1.0-SNAPSHOT' | ||
|
||
buildscript { | ||
repositories { | ||
google() | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
classpath 'com.android.tools.build:gradle:3.1.2' | ||
} | ||
} | ||
|
||
rootProject.allprojects { | ||
repositories { | ||
google() | ||
jcenter() | ||
} | ||
} | ||
|
||
apply plugin: 'com.android.library' | ||
|
||
android { | ||
compileSdkVersion 27 | ||
|
||
defaultConfig { | ||
minSdkVersion 16 | ||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
} | ||
lintOptions { | ||
disable 'InvalidPackage' | ||
} | ||
dependencies { | ||
api 'com.google.firebase:firebase-functions:16.+' | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
org.gradle.jvmargs=-Xmx1536M |
This file contains hidden or 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 @@ | ||
rootProject.name = 'cloud_functions' |
3 changes: 3 additions & 0 deletions
3
packages/cloud_functions/android/src/main/AndroidManifest.xml
This file contains hidden or 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,3 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="io.flutter.plugins.firebase.cloudfunctions.cloudfunctions"> | ||
</manifest> |
70 changes: 70 additions & 0 deletions
70
.../java/io/flutter/plugins/firebase/cloudfunctions/cloudfunctions/CloudFunctionsPlugin.java
This file contains hidden or 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,70 @@ | ||
// 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.cloudfunctions.cloudfunctions; | ||
|
||
import android.support.annotation.NonNull; | ||
import com.google.android.gms.tasks.OnCompleteListener; | ||
import com.google.android.gms.tasks.Task; | ||
import com.google.firebase.functions.FirebaseFunctions; | ||
import com.google.firebase.functions.FirebaseFunctionsException; | ||
import com.google.firebase.functions.HttpsCallableReference; | ||
import com.google.firebase.functions.HttpsCallableResult; | ||
import io.flutter.plugin.common.MethodCall; | ||
import io.flutter.plugin.common.MethodChannel; | ||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler; | ||
import io.flutter.plugin.common.MethodChannel.Result; | ||
import io.flutter.plugin.common.PluginRegistry.Registrar; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** CloudFunctionsPlugin */ | ||
public class CloudFunctionsPlugin implements MethodCallHandler { | ||
/** Plugin registration. */ | ||
public static void registerWith(Registrar registrar) { | ||
final MethodChannel channel = new MethodChannel(registrar.messenger(), "cloud_functions"); | ||
channel.setMethodCallHandler(new CloudFunctionsPlugin()); | ||
} | ||
|
||
@Override | ||
public void onMethodCall(MethodCall call, final Result result) { | ||
switch (call.method) { | ||
case "CloudFunctions#call": | ||
String functionName = call.argument("functionName"); | ||
HttpsCallableReference httpsCallableReference = | ||
FirebaseFunctions.getInstance().getHttpsCallable(functionName); | ||
Map<String, Object> parameters = call.argument("parameters"); | ||
httpsCallableReference | ||
.call(parameters) | ||
.addOnCompleteListener( | ||
new OnCompleteListener<HttpsCallableResult>() { | ||
@Override | ||
public void onComplete(@NonNull Task<HttpsCallableResult> task) { | ||
if (task.isSuccessful()) { | ||
result.success(task.getResult().getData()); | ||
} else { | ||
if (task.getException() instanceof FirebaseFunctionsException) { | ||
FirebaseFunctionsException exception = | ||
(FirebaseFunctionsException) task.getException(); | ||
Map<String, Object> exceptionMap = new HashMap<>(); | ||
exceptionMap.put("code", exception.getCode().name()); | ||
exceptionMap.put("message", exception.getMessage()); | ||
exceptionMap.put("details", exception.getDetails()); | ||
result.error( | ||
"functionsError", | ||
"Cloud function failed with exception.", | ||
exceptionMap); | ||
} else { | ||
Exception exception = task.getException(); | ||
result.error(null, exception.getMessage(), null); | ||
} | ||
} | ||
} | ||
}); | ||
break; | ||
default: | ||
result.notImplemented(); | ||
} | ||
} | ||
} |
This file contains hidden or 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,8 @@ | ||
# This file tracks properties of this Flutter project. | ||
# Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
# | ||
# This file should be version controlled and should not be manually edited. | ||
|
||
version: | ||
revision: b135fb3795a16ab2b884820ed7a67d650338aac3 | ||
channel: master |
This file contains hidden or 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,26 @@ | ||
# cloud_functions_example | ||
|
||
Demonstrates how to use the cloud_functions plugin. | ||
|
||
## Function | ||
|
||
This example assumes the existence of the following function: | ||
|
||
``` | ||
import * as functions from 'firebase-functions'; | ||
|
||
export const repeat = functions.https.onCall((data, context) => { | ||
return { | ||
repeat_message: data.message, | ||
repeat_count: data.count + 1, | ||
} | ||
}); | ||
``` | ||
|
||
This function accepts a message and count from the client and responds with | ||
the same message and an incremented count. | ||
|
||
## Getting Started | ||
|
||
For help getting started with Flutter, view our online | ||
[documentation](https://flutter.io/). |
This file contains hidden or 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,63 @@ | ||
def localProperties = new Properties() | ||
def localPropertiesFile = rootProject.file('local.properties') | ||
if (localPropertiesFile.exists()) { | ||
localPropertiesFile.withReader('UTF-8') { reader -> | ||
localProperties.load(reader) | ||
} | ||
} | ||
|
||
def flutterRoot = localProperties.getProperty('flutter.sdk') | ||
if (flutterRoot == null) { | ||
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") | ||
} | ||
|
||
def flutterVersionCode = localProperties.getProperty('flutter.versionCode') | ||
if (flutterVersionCode == null) { | ||
throw new GradleException("versionCode not found. Define flutter.versionCode in the local.properties file.") | ||
} | ||
|
||
def flutterVersionName = localProperties.getProperty('flutter.versionName') | ||
if (flutterVersionName == null) { | ||
throw new GradleException("versionName not found. Define flutter.versionName in the local.properties file.") | ||
} | ||
|
||
apply plugin: 'com.android.application' | ||
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" | ||
|
||
android { | ||
compileSdkVersion 27 | ||
|
||
lintOptions { | ||
disable 'InvalidPackage' | ||
} | ||
|
||
defaultConfig { | ||
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). | ||
applicationId "io.flutter.plugins.firebase.cloudfunctions.cloudfunctionsexample" | ||
minSdkVersion 16 | ||
targetSdkVersion 27 | ||
versionCode flutterVersionCode.toInteger() | ||
versionName flutterVersionName | ||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
// TODO: Add your own signing config for the release build. | ||
// Signing with the debug keys for now, so `flutter run --release` works. | ||
signingConfig signingConfigs.debug | ||
} | ||
} | ||
} | ||
|
||
flutter { | ||
source '../..' | ||
} | ||
|
||
dependencies { | ||
testImplementation 'junit:junit:4.12' | ||
androidTestImplementation 'com.android.support.test:runner:1.0.2' | ||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' | ||
} | ||
|
||
apply plugin: 'com.google.gms.google-services' |
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 think you can delete all the
.gitignore
files. I think we are trying to keep only one from now on from #588There 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.
Done