You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

98 lines
2.4 KiB

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