1// Copyright 2014 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package main
6
7import (
8	"fmt"
9	"log"
10	"net/http"
11	"os"
12	"strings"
13	"time"
14
15	mirror "google.golang.org/api/mirror/v1"
16)
17
18const mirrorLayout = "Jan 2, 2006 at 3:04pm"
19
20func init() {
21	scopes := []string{
22		mirror.GlassLocationScope,
23		mirror.GlassTimelineScope,
24	}
25	registerDemo("mirror", strings.Join(scopes, " "), mirrorMain)
26}
27
28// mirrorMain is an example that demonstrates calling the Mirror API.
29//
30// Example usage:
31//   go build -o go-api-demo *.go
32//   go-api-demo -clientid="my-clientid" -secret="my-secret" mirror
33func mirrorMain(client *http.Client, argv []string) {
34	if len(argv) != 0 {
35		fmt.Fprintln(os.Stderr, "Usage: mirror")
36		return
37	}
38
39	svc, err := mirror.New(client)
40	if err != nil {
41		log.Fatalf("Unable to create Mirror service: %v", err)
42	}
43
44	cs, err := svc.Contacts.List().Do()
45	if err != nil {
46		log.Fatalf("Unable to retrieve glass contacts: %v", err)
47	}
48
49	if len(cs.Items) == 0 {
50		log.Printf("You have no glass contacts.  Let's add one.")
51		mom := &mirror.Contact{
52			DisplayName:   "Mom",
53			Id:            "mom",
54			PhoneNumber:   "123-456-7890",
55			SpeakableName: "mom",
56		}
57		_, err := svc.Contacts.Insert(mom).Do()
58		if err != nil {
59			log.Fatalf("Unable to add %v to glass contacts: %v", mom.DisplayName, err)
60		}
61	}
62	for _, c := range cs.Items {
63		log.Printf("Found glass contact %q, phone number: %v", c.DisplayName, c.PhoneNumber)
64	}
65
66	ls, err := svc.Locations.List().Do()
67	if err != nil {
68		log.Fatalf("Unable to retrieve glass locations: %v", err)
69	}
70
71	if len(ls.Items) == 0 {
72		log.Printf("You have no glass locations.")
73	}
74	for _, loc := range ls.Items {
75		t, err := time.Parse(time.RFC3339, loc.Timestamp)
76		if err != nil {
77			log.Printf("unable to parse time %q: %v", loc.Timestamp, err)
78		}
79		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)
80	}
81
82	ts, err := svc.Timeline.List().Do()
83	if err != nil {
84		log.Fatalf("Unable to retrieve glass timeline: %v", err)
85	}
86
87	if len(ts.Items) == 0 {
88		log.Printf("You have no glass timeline items.")
89	}
90	for _, v := range ts.Items {
91		t, err := time.Parse(time.RFC3339, v.Updated)
92		if err != nil {
93			log.Printf("unable to parse time %q: %v", v.Updated, err)
94		}
95		log.Printf("Found glass timeline item: %q at %v", v.Text, t.Format(mirrorLayout))
96	}
97}
98