-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests.go
91 lines (82 loc) · 2.44 KB
/
requests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package firebase
import (
"context"
"cloud.google.com/go/firestore"
"firebase.google.com/go/messaging"
)
func (f *firebaseClient) FirestoreDocExists(ctx context.Context, collection string, docID string) (bool, error) {
doc, err := f.firestoreClient.Collection(collection).Doc(docID).Get(ctx)
if err != nil {
return false, err
}
return doc.Exists(), nil
}
func (f *firebaseClient) FirestoreGet(ctx context.Context, collection string, docID string) (map[string]interface{}, error) {
if !f.firestore {
return nil, &UnexpectedUseFirestoreErr{}
}
doc, err := f.firestoreClient.Collection(collection).Doc(docID).Get(ctx)
if err != nil {
return nil, err
}
return doc.Data(), nil
}
func (f *firebaseClient) FirestoreGetType(ctx context.Context, collection string, docID string, dataTo interface{}) error {
if !f.firestore {
return &UnexpectedUseFirestoreErr{}
}
doc, err := f.firestoreClient.Collection(collection).Doc(docID).Get(ctx)
if err != nil {
return err
}
return doc.DataTo(dataTo)
}
func (f *firebaseClient) FirestoreSet(ctx context.Context, collection, docID string, data map[string]interface{}, merge bool) error {
if !f.firestore {
return &UnexpectedUseFirestoreErr{}
}
var err error
if merge {
_, err = f.firestoreClient.Collection(collection).Doc(docID).Set(ctx, data, firestore.MergeAll)
} else {
_, err = f.firestoreClient.Collection(collection).Doc(docID).Set(ctx, data)
}
return err
}
func (f *firebaseClient) FirestoreSetType(ctx context.Context, collection, docID string, dataTo interface{}, merge bool) error {
if !f.firestore {
return &UnexpectedUseFirestoreErr{}
}
var err error
if merge {
_, err = f.firestoreClient.Collection(collection).Doc(docID).Set(ctx, dataTo)
} else {
_, err = f.firestoreClient.Collection(collection).Doc(docID).Set(ctx, dataTo)
}
return err
}
func (f *firebaseClient) SendMessageTopic(ctx context.Context, topic string, data map[string]string) error {
if !f.messaging {
return &UnexpectedUseMessagingErr{}
}
message := &messaging.Message{
Data: data,
Topic: topic,
}
_, err := f.messagingClient.Send(ctx, message)
return err
}
func (f *firebaseClient) VerifyToken(ctx context.Context, token string) (string, error) {
if !f.auth {
return "", &UnexpectedUseAuthErr{}
}
tokenAuth, err := f.authClient.VerifyIDToken(ctx, token)
if err != nil {
return "", err
}
uid, ok := tokenAuth.Claims["user_id"].(string)
if !ok {
return "", &NoUIDFoundErr{}
}
return uid, nil
}