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
13	calendar "google.golang.org/api/calendar/v3"
14)
15
16func init() {
17	registerDemo("calendar", calendar.CalendarScope, calendarMain)
18}
19
20// calendarMain is an example that demonstrates calling the Calendar API.
21// Its purpose is to test out the ability to get maps of struct objects.
22//
23// Example usage:
24//   go build -o go-api-demo *.go
25//   go-api-demo -clientid="my-clientid" -secret="my-secret" calendar
26func calendarMain(client *http.Client, argv []string) {
27	if len(argv) != 0 {
28		fmt.Fprintln(os.Stderr, "Usage: calendar")
29		return
30	}
31
32	svc, err := calendar.New(client)
33	if err != nil {
34		log.Fatalf("Unable to create Calendar service: %v", err)
35	}
36
37	c, err := svc.Colors.Get().Do()
38	if err != nil {
39		log.Fatalf("Unable to retrieve calendar colors: %v", err)
40	}
41
42	log.Printf("Kind of colors: %v", c.Kind)
43	log.Printf("Colors last updated: %v", c.Updated)
44
45	for k, v := range c.Calendar {
46		log.Printf("Calendar[%v]: Background=%v, Foreground=%v", k, v.Background, v.Foreground)
47	}
48
49	for k, v := range c.Event {
50		log.Printf("Event[%v]: Background=%v, Foreground=%v", k, v.Background, v.Foreground)
51	}
52
53	listRes, err := svc.CalendarList.List().Fields("items/id").Do()
54	if err != nil {
55		log.Fatalf("Unable to retrieve list of calendars: %v", err)
56	}
57	for _, v := range listRes.Items {
58		log.Printf("Calendar ID: %v\n", v.Id)
59	}
60
61	if len(listRes.Items) > 0 {
62		id := listRes.Items[0].Id
63		res, err := svc.Events.List(id).Fields("items(updated,summary)", "summary", "nextPageToken").Do()
64		if err != nil {
65			log.Fatalf("Unable to retrieve calendar events list: %v", err)
66		}
67		for _, v := range res.Items {
68			log.Printf("Calendar ID %q event: %v: %q\n", id, v.Updated, v.Summary)
69		}
70		log.Printf("Calendar ID %q Summary: %v\n", id, res.Summary)
71		log.Printf("Calendar ID %q next page token: %v\n", id, res.NextPageToken)
72	}
73}
74