Browse Source

firebase example

Aaron Torres 2 years ago
parent
commit
85f90ef5dd

+ 27 - 0
chapter12/firebase/auth.go

@@ -0,0 +1,27 @@
+package firebase
+
+import (
+	"io/ioutil"
+
+	"golang.org/x/oauth2"
+	"golang.org/x/oauth2/google"
+	"gopkg.in/zabawaba99/firego.v1"
+)
+
+// Authenticate grabs oauth scopes using a generated
+// service_account.json file from
+// https://console.firebase.google.com/project/go-cookbook/settings/serviceaccounts/adminsdk
+func Authenticate() (Client, error) {
+	d, err := ioutil.ReadFile("/tmp/service_account.json")
+	if err != nil {
+		return nil, err
+	}
+
+	conf, err := google.JWTConfigFromJSON(d, "https://www.googleapis.com/auth/userinfo.email",
+		"https://www.googleapis.com/auth/firebase.database")
+	if err != nil {
+		return nil, err
+	}
+	f := firego.New("https://go-cookbook.firebaseio.com", conf.Client(oauth2.NoContext))
+	return &firebaseClient{f}, err
+}

+ 22 - 0
chapter12/firebase/auth_test.go

@@ -0,0 +1,22 @@
+package firebase
+
+import "testing"
+
+func TestAuthenticate(t *testing.T) {
+
+	tests := []struct {
+		name    string
+		wantErr bool
+	}{
+		{"base-case", false},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			_, err := Authenticate()
+			if (err != nil) != tt.wantErr {
+				t.Errorf("Authenticate() error = %v, wantErr %v", err, tt.wantErr)
+				return
+			}
+		})
+	}
+}

+ 32 - 0
chapter12/firebase/client.go

@@ -0,0 +1,32 @@
+package firebase
+
+import (
+	"log"
+
+	"gopkg.in/zabawaba99/firego.v1"
+)
+
+// Client Interface for mocking
+type Client interface {
+	Get() (map[string]interface{}, error)
+	Set(key string, value interface{}) error
+}
+type firebaseClient struct {
+	*firego.Firebase
+}
+
+func (f *firebaseClient) Get() (map[string]interface{}, error) {
+	var v2 map[string]interface{}
+	if err := f.Value(&v2); err != nil {
+		log.Fatalf("error getting")
+	}
+	return v2, nil
+}
+
+func (f *firebaseClient) Set(key string, value interface{}) error {
+	v := map[string]interface{}{key: value}
+	if err := f.Firebase.Set(v); err != nil {
+		return err
+	}
+	return nil
+}

+ 51 - 0
chapter12/firebase/client_test.go

@@ -0,0 +1,51 @@
+package firebase
+
+import "testing"
+
+func Test_firebaseClient_Get(t *testing.T) {
+
+	tests := []struct {
+		name    string
+		wantErr bool
+	}{
+		{"base-case", false},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			f, err := Authenticate()
+			if err != nil {
+				t.Error(err)
+			}
+			_, err = f.Get()
+			if (err != nil) != tt.wantErr {
+				t.Errorf("firebaseClient.Get() error = %v, wantErr %v", err, tt.wantErr)
+				return
+			}
+		})
+	}
+}
+
+func Test_firebaseClient_Set(t *testing.T) {
+	type args struct {
+		key   string
+		value interface{}
+	}
+	tests := []struct {
+		name    string
+		args    args
+		wantErr bool
+	}{
+		{"base-case", args{"test", "case"}, false},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			f, err := Authenticate()
+			if err != nil {
+				t.Error(err)
+			}
+			if err := f.Set(tt.args.key, tt.args.value); (err != nil) != tt.wantErr {
+				t.Errorf("firebaseClient.Set() error = %v, wantErr %v", err, tt.wantErr)
+			}
+		})
+	}
+}

+ 24 - 0
chapter12/firebase/example/main.go

@@ -0,0 +1,24 @@
+package main
+
+import (
+	"fmt"
+	"log"
+
+	"github.com/agtorre/go-cookbook/chapter12/firebase"
+)
+
+func main() {
+	f, err := firebase.Authenticate()
+	if err != nil {
+		log.Fatalf("error authenticating")
+	}
+	f.Set("key", []string{"val1", "val2"})
+	res, _ := f.Get()
+	fmt.Println(res)
+
+	vals := res["key"].([]interface{})
+	vals = append(vals, map[string][]string{"key2": []string{"val3"}})
+	f.Set("key", vals)
+	res, _ = f.Get()
+	fmt.Println(res)
+}