-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ざっくりメールのPush通知を受け取れる雰囲気にしてみた refs #1
- Loading branch information
Showing
7 changed files
with
244 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# ironhead | ||
|
||
Gmail Webhock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"cloud.google.com/go/pubsub" | ||
"github.com/k0kubun/pp" | ||
) | ||
|
||
func TestPubSubPull(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
pubsubClient, err := pubsub.NewClient(ctx, "sinmetal-ironhead") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
debugSub := pubsubClient.Subscription("gmail-debug") | ||
err = debugSub.Receive(ctx, func(ctx context.Context, message *pubsub.Message) { | ||
fmt.Printf("%+v\n", message.Attributes) | ||
fmt.Printf("%s\n", string(message.Data)) | ||
var d NotifyData | ||
if err := json.Unmarshal(message.Data, &d); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
// 試しにErrorReportingのメッセージを取得してみる | ||
func TestGmailService_GetMessage(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
s := newGmailService(ctx) | ||
msg, err := s.GetMessage(ctx, "[email protected]", 12158234, "Label_6698128523804588152") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
pp.Print(msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"google.golang.org/api/gmail/v1" | ||
) | ||
|
||
// GmailWatchHandler is gmail.Watch を実行する | ||
// 1日に1回cronで実行する | ||
func GmailWatchHandler(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
|
||
resp, err := gmailService.Watch(ctx, userID, &gmail.WatchRequest{ | ||
TopicName: "projects/sinmetal-ironhead/topics/gmail", | ||
LabelIds: []string{tbfErrorReportingLabelID}, | ||
}) | ||
if err != nil { | ||
fmt.Printf("failed GmailService.Watch. err=%+v", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
fmt.Printf("HistoryID:%d,Expiration:%d\n", resp.HistoryID, resp.Expiration) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"cloud.google.com/go/storage" | ||
) | ||
|
||
const goldenBucket = "sinmetal-ironhead-golden" | ||
|
||
func TestCreateGolden(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
s := newGmailService(t) | ||
|
||
const userID = "[email protected]" | ||
const startHistoryID = 12158234 | ||
const labelID = "Label_6698128523804588152" | ||
|
||
res, err := s.gus.History.List(userID).StartHistoryId(uint64(startHistoryID)).LabelId(labelID).Context(ctx).Do() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
//pp.Print(res) | ||
msg, err := s.gus.Messages.Get(userID, res.History[0].Messages[0].Id).Context(ctx).Do() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
writeGoldenFile(ctx, "error-reporting-mail-message.json", msg, t) | ||
} | ||
|
||
func writeGoldenFile(ctx context.Context, path string, body interface{}, t *testing.T) { | ||
gcs, err := storage.NewClient(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
if err := gcs.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
|
||
w := gcs.Bucket(goldenBucket).Object(path).NewWriter(ctx) | ||
if err := json.NewEncoder(w).Encode(body); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := w.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func readGoldenFile(ctx context.Context, path string, t *testing.T) []byte { | ||
gcs, err := storage.NewClient(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
if err := gcs.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
|
||
r, err := gcs.Bucket(goldenBucket).Object(path).NewReader(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
if err := r.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
b, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,13 @@ import ( | |
"google.golang.org/api/option" | ||
) | ||
|
||
const ( | ||
userID = "[email protected]" | ||
tbfErrorReportingLabelID = "Label_6698128523804588152" | ||
) | ||
|
||
var gmailService *GmailService | ||
|
||
func main() { | ||
var ( | ||
cmd = flag.String("cmd", "default", "command") | ||
|
@@ -26,12 +33,15 @@ func main() { | |
os.Exit(0) | ||
} | ||
|
||
watchGmail() | ||
ctx := context.Background() | ||
|
||
gmailService = newGmailService(ctx) | ||
|
||
const addr = ":8080" | ||
fmt.Printf("Start Listen %s\n", addr) | ||
|
||
http.HandleFunc("/notify/gmail", GmailNotifyPubSubHandler) | ||
http.HandleFunc("/gmail/watch", GmailWatchHandler) | ||
http.HandleFunc("/", helloWorldHandler) | ||
log.Fatal(http.ListenAndServe(addr, nil)) | ||
} | ||
|
@@ -58,14 +68,12 @@ func saveToken() { | |
} | ||
}() | ||
|
||
if err := ts.SaveToken(ctx, gmail.GmailMetadataScope); err != nil { | ||
if err := ts.SaveToken(ctx, GmailServiceScope...); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func watchGmail() { | ||
ctx := context.Background() | ||
|
||
func newGmailService(ctx context.Context) *GmailService { | ||
gcs, err := storage.NewClient(ctx) | ||
if err != nil { | ||
log.Fatalf("failed storage.NewClient err=%#v", err) | ||
|
@@ -80,7 +88,7 @@ func watchGmail() { | |
} | ||
}() | ||
|
||
client, err := ts.CreateHTTPClient(ctx, gmail.GmailMetadataScope) | ||
client, err := ts.CreateHTTPClient(ctx, GmailServiceScope...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
@@ -94,13 +102,5 @@ func watchGmail() { | |
if err != nil { | ||
log.Fatalf("failed NewGmailService err=%+v", err) | ||
} | ||
|
||
resp, err := s.Watch(ctx, "[email protected]", &gmail.WatchRequest{ | ||
TopicName: "projects/sinmetal-ironhead/topics/gmail", | ||
LabelIds: []string{"Label_6698128523804588152"}, | ||
}) | ||
if err != nil { | ||
log.Fatalf("failed Watch err=%+v", err) | ||
} | ||
fmt.Printf("%#v", resp) | ||
return s | ||
} |