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 92 93 94 95 96 97
|
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
mirror "google.golang.org/api/mirror/v1"
)
const mirrorLayout = "Jan 2, 2006 at 3:04pm"
func init() {
scopes := []string{
mirror.GlassLocationScope,
mirror.GlassTimelineScope,
}
registerDemo("mirror", strings.Join(scopes, " "), mirrorMain)
}
// mirrorMain is an example that demonstrates calling the Mirror API.
//
// Example usage:
// go build -o go-api-demo *.go
// go-api-demo -clientid="my-clientid" -secret="my-secret" mirror
func mirrorMain(client *http.Client, argv []string) {
if len(argv) != 0 {
fmt.Fprintln(os.Stderr, "Usage: mirror")
return
}
svc, err := mirror.New(client)
if err != nil {
log.Fatalf("Unable to create Mirror service: %v", err)
}
cs, err := svc.Contacts.List().Do()
if err != nil {
log.Fatalf("Unable to retrieve glass contacts: %v", err)
}
if len(cs.Items) == 0 {
log.Printf("You have no glass contacts. Let's add one.")
mom := &mirror.Contact{
DisplayName: "Mom",
Id: "mom",
PhoneNumber: "123-456-7890",
SpeakableName: "mom",
}
_, err := svc.Contacts.Insert(mom).Do()
if err != nil {
log.Fatalf("Unable to add %v to glass contacts: %v", mom.DisplayName, err)
}
}
for _, c := range cs.Items {
log.Printf("Found glass contact %q, phone number: %v", c.DisplayName, c.PhoneNumber)
}
ls, err := svc.Locations.List().Do()
if err != nil {
log.Fatalf("Unable to retrieve glass locations: %v", err)
}
if len(ls.Items) == 0 {
log.Printf("You have no glass locations.")
}
for _, loc := range ls.Items {
t, err := time.Parse(time.RFC3339, loc.Timestamp)
if err != nil {
log.Printf("unable to parse time %q: %v", loc.Timestamp, err)
}
log.Printf("Found glass location: %q at %v, address: %v (lat=%v, lon=%v)", loc.DisplayName, t.Format(mirrorLayout), loc.Address, loc.Latitude, loc.Longitude)
}
ts, err := svc.Timeline.List().Do()
if err != nil {
log.Fatalf("Unable to retrieve glass timeline: %v", err)
}
if len(ts.Items) == 0 {
log.Printf("You have no glass timeline items.")
}
for _, v := range ts.Items {
t, err := time.Parse(time.RFC3339, v.Updated)
if err != nil {
log.Printf("unable to parse time %q: %v", v.Updated, err)
}
log.Printf("Found glass timeline item: %q at %v", v.Text, t.Format(mirrorLayout))
}
}
|