Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Add Cloud Functions support #592

Merged
merged 7 commits into from
Jun 21, 2018
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
33 changes: 33 additions & 0 deletions packages/cloud_functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.DS_Store
Copy link
Contributor

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 #588

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

.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
13 changes: 13 additions & 0 deletions packages/cloud_functions/CHANGELOG.md
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.
26 changes: 26 additions & 0 deletions packages/cloud_functions/LICENSE
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.
40 changes: 40 additions & 0 deletions packages/cloud_functions/README.md
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)

[![pub package](https://img.shields.io/pub/v/cloud_functions.svg)](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.
37 changes: 37 additions & 0 deletions packages/cloud_functions/android/build.gradle
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.+'
}
}
1 change: 1 addition & 0 deletions packages/cloud_functions/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.jvmargs=-Xmx1536M
1 change: 1 addition & 0 deletions packages/cloud_functions/android/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'cloud_functions'
3 changes: 3 additions & 0 deletions packages/cloud_functions/android/src/main/AndroidManifest.xml
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>
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();
}
}
}
8 changes: 8 additions & 0 deletions packages/cloud_functions/example/.metadata
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
26 changes: 26 additions & 0 deletions packages/cloud_functions/example/README.md
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/).
63 changes: 63 additions & 0 deletions packages/cloud_functions/example/android/app/build.gradle
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'
Loading