...

Source file src/google.golang.org/api/examples/mirror.go

Documentation: google.golang.org/api/examples

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

View as plain text