8784: Fix test for latest firefox.
[arvados.git] / doc / _includes / _example_sdk_go.liquid
1 package main
2
3
4 // *******************
5 // Import the modules.
6 //
7 // Our examples don't use keepclient, but they do use fmt and log to
8 // display output.
9
10 import (
11         "fmt"
12         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
13         "log"
14 )
15
16 func main() {
17
18
19         // ********************************
20         // Set up an API client user agent.
21         //
22
23         arv, err := arvadosclient.MakeArvadosClient()
24         if err != nil {
25                 log.Fatalf("Error setting up arvados client %s", err.Error())
26         }
27
28
29         // *****************************************
30         // Print the full name of the current user.
31         //
32
33         type user struct {
34                 // Remember to start each field name with a capital letter,
35                 // otherwise it won't get populated by the arvados client because
36                 // the field will be invisible to it.
37                 Uuid     string `json:"uuid"`
38                 FullName string `json:"full_name"`
39         }
40
41         var u user
42         err = arv.Call("GET", "users", "", "current", nil, &u)
43
44         if err != nil {
45                 log.Fatalf("error querying current user", err.Error())
46         }
47
48         log.Printf("Logged in as %s (uuid %s)", u.FullName, u.Uuid)
49
50
51         // ********************************************************
52         // Print all fields from the first five collections returned.
53         //
54         // Note that some fields, are not returned by default and have to be
55         // requested. See below for an example.
56
57         var results map[string]interface{}
58
59         params := arvadosclient.Dict{"limit": 5}
60
61         err = arv.List("collections", params, &results)
62         if err != nil {
63                 log.Fatalf("error querying collections", err.Error())
64         }
65
66         printArvadosResults(results)
67
68
69         // *********************************************************
70         // Print some fields from the first two collections returned.
71         //
72         // We also print manifest_test, which has to be explicitly requested.
73         //
74
75         collection_fields_wanted := []string{"manifest_text", "owner_uuid", "uuid"}
76         params = arvadosclient.Dict{"limit": 2, "select": collection_fields_wanted}
77
78         err = arv.List("collections", params, &results)
79         if err != nil {
80                 log.Fatalf("error querying collections", err.Error())
81         }
82
83         printArvadosResults(results)
84 }
85
86
87 // A helper method which will print out a result map returned by
88 // arvadosclient.
89 func printArvadosResults(results map[string]interface{}) {
90         for key, value := range results {
91                 // "items", if it exists, holds a map.
92                 // So we print it prettily below.
93                 if key != "items" {
94                         fmt.Println(key, ":", value)
95                 }
96         }
97
98         if value, ok := results["items"]; ok {
99                 items := value.([]interface{})
100                 for index, item := range items {
101                         fmt.Println("===========  ", index, "  ===========")
102                         item_map := item.(map[string]interface{})
103                         if len(item_map) == 0 {
104                                 fmt.Println("item", index, ": empty map")
105                         } else {
106                                 for k, v := range item_map {
107                                         fmt.Println(index, k, ":", v)
108                                 }
109                         }
110                 }
111         }
112 }