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 "strconv" 13 "strings" 14 15 books "google.golang.org/api/books/v1" 16) 17 18func init() { 19 registerDemo("books", books.BooksScope, booksMain) 20} 21 22// booksMain is an example that demonstrates calling the Books API. 23// 24// Example usage: 25// go build -o go-api-demo *.go 26// go-api-demo -clientid="my-clientid" -secret="my-secret" books 27func booksMain(client *http.Client, argv []string) { 28 if len(argv) != 0 { 29 fmt.Fprintln(os.Stderr, "Usage: books") 30 return 31 } 32 33 svc, err := books.New(client) 34 if err != nil { 35 log.Fatalf("Unable to create Books service: %v", err) 36 } 37 38 bs, err := svc.Mylibrary.Bookshelves.List().Do() 39 if err != nil { 40 log.Fatalf("Unable to retrieve mylibrary bookshelves: %v", err) 41 } 42 43 if len(bs.Items) == 0 { 44 log.Fatal("You have no bookshelves to explore.") 45 } 46 for _, b := range bs.Items { 47 // Note that sometimes VolumeCount is not populated, so it may erroneously say '0'. 48 log.Printf("You have %v books on bookshelf %q:", b.VolumeCount, b.Title) 49 50 // List the volumes on this shelf. 51 vol, err := svc.Mylibrary.Bookshelves.Volumes.List(strconv.FormatInt(b.Id, 10)).Do() 52 if err != nil { 53 log.Fatalf("Unable to retrieve mylibrary bookshelf volumes: %v", err) 54 } 55 for _, v := range vol.Items { 56 var s []string 57 if v.VolumeInfo.ReadingModes.Image { 58 s = append(s, "image") 59 } else { 60 s = append(s, "text") 61 } 62 extra := fmt.Sprintf("; formats: %v", s) 63 if v.VolumeInfo.ImageLinks != nil { 64 extra += fmt.Sprintf("; thumbnail: %v", v.VolumeInfo.ImageLinks.Thumbnail) 65 } 66 log.Printf(" %q by %v%v", v.VolumeInfo.Title, strings.Join(v.VolumeInfo.Authors, ", "), extra) 67 } 68 } 69} 70