-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.go
More file actions
65 lines (58 loc) · 1.91 KB
/
utility.go
File metadata and controls
65 lines (58 loc) · 1.91 KB
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
package main
import (
"context"
"google.golang.org/appengine/datastore"
)
// Return webhook datastore key.
func webhookKey(context context.Context, handler string) *datastore.Key {
return datastore.NewKey(context, "Webhook", handler, 0, nil)
}
// Return AccessToken datastore key.
func accessTokenKey(context context.Context, email string) *datastore.Key {
return datastore.NewKey(context, "AccessTokens", email, 0, nil)
}
// Return access token for provided email address.
func getAccessToken(context context.Context, email string) string {
userAccessToken := datastore.NewQuery("AccessTokens").Ancestor(
accessTokenKey(context, email)).Filter("Email =", email).Limit(1)
aTokens := make([]AccessTokens, 0, 1)
userAccessToken.GetAll(context, &aTokens)
if len(aTokens) > 0 {
return aTokens[0].AccessToken
}
return ""
}
// Return list of webhooks (datastore entities) for given email.
func getWebhooks(context context.Context, email string) []Webhook {
query := datastore.NewQuery("Webhook").Filter("User =", email).Limit(50)
webhooks := make([]Webhook, 0, 50)
query.GetAll(context, &webhooks)
return webhooks
}
// Return list of webhooks (datastore entities) from given handler.
func getWebhookFromHandler(
context context.Context, handler string) *Webhook {
query := datastore.NewQuery("Webhook").Ancestor(
webhookKey(context, handler)).Limit(1)
webhook := make([]Webhook, 0, 1)
keys, _ := query.GetAll(context, &webhook)
if len(webhook) > 0 {
webhook[0].Count += 1
datastore.Put(context, keys[0], &webhook[0])
return &webhook[0]
}
return nil
}
// Delete handler.
func deleteWebhookFromHandler(
context context.Context, handler string) *Webhook {
query := datastore.NewQuery("Webhook").Ancestor(
webhookKey(context, handler)).Limit(1)
webhook := make([]Webhook, 0, 1)
keys, _ := query.GetAll(context, &webhook)
if len(webhook) > 0 {
datastore.Delete(context, keys[0])
return &webhook[0]
}
return nil
}