From 31b8788351b4a2b561c54737da7ba61a374fdc59 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Mon, 3 Feb 2020 14:10:23 +0700 Subject: [PATCH 1/3] feat: add functions_firebase_auth --- functions/snippets/pom.xml | 8 +++++ .../snippets/src/main/java/FirebaseAuth.java | 32 +++++++++++++++++++ .../snippets/src/test/java/SnippetsTests.java | 5 +++ 3 files changed, 45 insertions(+) create mode 100644 functions/snippets/src/main/java/FirebaseAuth.java diff --git a/functions/snippets/pom.xml b/functions/snippets/pom.xml index d5cc0fc218f..6787b9a8593 100644 --- a/functions/snippets/pom.xml +++ b/functions/snippets/pom.xml @@ -67,6 +67,14 @@ 1.19.0 test + + + + com.google.cloud.functions + functions-framework-api + 1.0.0-alpha-2-rc3 + jar + diff --git a/functions/snippets/src/main/java/FirebaseAuth.java b/functions/snippets/src/main/java/FirebaseAuth.java new file mode 100644 index 00000000000..9fdeb2a4a2e --- /dev/null +++ b/functions/snippets/src/main/java/FirebaseAuth.java @@ -0,0 +1,32 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START functions_firebase_auth] +import java.util.logging.Logger; +import com.google.cloud.functions.Context; +import com.google.cloud.functions.RawBackgroundFunction; + +public class FirebaseAuth implements RawBackgroundFunction { + + private static final Logger logger = Logger.getLogger(FirebaseAuth.class.getName()); + + @Override + public void accept(String json, Context context) { + logger.info("Function triggered by event: " + json); + } +} + +// [END functions_firebase_auth] diff --git a/functions/snippets/src/test/java/SnippetsTests.java b/functions/snippets/src/test/java/SnippetsTests.java index 3d27e2ebc64..e3d8654d862 100644 --- a/functions/snippets/src/test/java/SnippetsTests.java +++ b/functions/snippets/src/test/java/SnippetsTests.java @@ -217,4 +217,9 @@ public void helloExecutionCount() throws IOException { new Concepts().executionCount(request, response); assertThat(responseOut.toString(), containsString("Instance execution count: 1")); } + + @Test + public void firebaseAuth() throws IOException { + new FirebaseAuth().accept("{}", null); + } } From 465eddd15e462ab268a37a3e53f15a9bb248b09a Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Tue, 4 Feb 2020 08:50:39 +0800 Subject: [PATCH 2/3] Update FirebaseAuth.java --- functions/snippets/src/main/java/FirebaseAuth.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/snippets/src/main/java/FirebaseAuth.java b/functions/snippets/src/main/java/FirebaseAuth.java index 9fdeb2a4a2e..98059d26829 100644 --- a/functions/snippets/src/main/java/FirebaseAuth.java +++ b/functions/snippets/src/main/java/FirebaseAuth.java @@ -15,9 +15,9 @@ */ // [START functions_firebase_auth] -import java.util.logging.Logger; import com.google.cloud.functions.Context; import com.google.cloud.functions.RawBackgroundFunction; +import java.util.logging.Logger; public class FirebaseAuth implements RawBackgroundFunction { From d6627922db97474d656535a33b0e47f9ce2e3563 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Tue, 4 Feb 2020 09:50:01 -0800 Subject: [PATCH 3/3] feat: add json parsing logic --- functions/snippets/src/main/java/FirebaseAuth.java | 10 ++++++++-- functions/snippets/src/test/java/SnippetsTests.java | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/functions/snippets/src/main/java/FirebaseAuth.java b/functions/snippets/src/main/java/FirebaseAuth.java index 9fdeb2a4a2e..f9cb81ffa4d 100644 --- a/functions/snippets/src/main/java/FirebaseAuth.java +++ b/functions/snippets/src/main/java/FirebaseAuth.java @@ -15,17 +15,23 @@ */ // [START functions_firebase_auth] +import com.google.gson.Gson; +import com.google.gson.JsonObject; import java.util.logging.Logger; import com.google.cloud.functions.Context; import com.google.cloud.functions.RawBackgroundFunction; public class FirebaseAuth implements RawBackgroundFunction { - private static final Logger logger = Logger.getLogger(FirebaseAuth.class.getName()); + // Use GSON (https://github.com/google/gson) to parse JSON content. + private Gson gsonParser = new Gson(); + + private static final Logger LOGGER = Logger.getLogger(FirebaseAuth.class.getName()); @Override public void accept(String json, Context context) { - logger.info("Function triggered by event: " + json); + JsonObject body = gsonParser.fromJson(json, JsonObject.class); + LOGGER.info("Function triggered by event: " + json); } } diff --git a/functions/snippets/src/test/java/SnippetsTests.java b/functions/snippets/src/test/java/SnippetsTests.java index e3d8654d862..e34f9fe430b 100644 --- a/functions/snippets/src/test/java/SnippetsTests.java +++ b/functions/snippets/src/test/java/SnippetsTests.java @@ -220,6 +220,6 @@ public void helloExecutionCount() throws IOException { @Test public void firebaseAuth() throws IOException { - new FirebaseAuth().accept("{}", null); + new FirebaseAuth().accept("{\"foo\": \"bar\"}", null); } }