Debian Codegraph @

plugins/gnostic-go-generator/examples/v3.0/urlshortener/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "fmt"
5 "log"
6
7 "github.com/docopt/docopt-go"
8 "github.com/googleapis/gnostic/plugins/gnostic-go-generator/examples/googleauth"
9 "github.com/googleapis/gnostic/plugins/gnostic-go-generator/examples/v3.0/urlshortener/urlshortener"
10 )
11
12 func main() {
13 usage := `
14 Usage:
15 urlshortener get <url>
16 urlshortener list
17 urlshortener insert <url>
18 `
19 arguments, err := docopt.Parse(usage, nil, false, "URL Shortener 1.0", false)
20 if err != nil {
21 log.Fatalf("%+v", err)
22 }
23
24 path := "https://www.googleapis.com/urlshortener/v1" // this should be generated
25
26 client, err := googleauth.NewOAuth2Client("https://www.googleapis.com/auth/urlshortener")
27 if err != nil {
28 log.Fatalf("Error building OAuth client: %v", err)
29 }
30 c := urlshortener.NewClient(path, client)
31
32 // get
33 if arguments["get"].(bool) {
34 response, err := c.Urlshortener_Url_Get("FULL", arguments["<url>"].(string))
35 if err != nil {
36 log.Fatalf("%+v", err)
37 }
38 fmt.Println(response.Default.LongUrl)
39 }
40
41 // list
42 if arguments["list"].(bool) {
43 response, err := c.Urlshortener_Url_List("", "")
44 if err != nil {
45 log.Fatalf("%+v", err)
46 }
47 for _, item := range response.Default.Items {
48 fmt.Printf("%-40s %s\n", item.Id, item.LongUrl)
49 }
50 }
51
52 // insert
53 if arguments["insert"].(bool) {
54 var url urlshortener.Url
55 url.LongUrl = arguments["<url>"].(string)
56 response, err := c.Urlshortener_Url_Insert(url)
57 if err != nil {
58 log.Fatalf("%+v", err)
59 }
60 fmt.Printf("%+v\n", response.Default.Id)
61 }
62 }