Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[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                 Uuid     string `json:"uuid"`
35                 FullName string `json:"full_name"`
36         }
37
38         var u user
39         err = arv.Call("GET", "users", "", "current", nil, &u)
40
41         if err != nil {
42                 log.Fatalf("error querying current user", err.Error())
43         }
44
45         log.Printf("Logged in as %s (uuid %s)", u.FullName, u.Uuid)
46
47
48         // ********************************************************
49         // Print all fields from the first five collections returned.
50         //
51         // Note that some fields, are not returned by default and have to be
52         // requested. See below for an example.
53
54         var results map[string]interface{}
55
56         params := arvadosclient.Dict{"limit": 5}
57
58         err = arv.List("collections", params, &results)
59         if err != nil {
60                 log.Fatalf("error querying collections", err.Error())
61         }
62
63         printArvadosResults(results)
64
65
66         // *********************************************************
67         // Print some fields from the first two collections returned.
68         //
69         // We also print manifest_test, which has to be explicitly requested.
70         //
71
72         collection_fields_wanted := []string{"manifest_text", "owner_uuid", "uuid"}
73         params = arvadosclient.Dict{"limit": 2, "select": collection_fields_wanted}
74
75         err = arv.List("collections", params, &results)
76         if err != nil {
77                 log.Fatalf("error querying collections", err.Error())
78         }
79
80         printArvadosResults(results)
81 }
82
83
84 // A helper method which will print out a result map returned by
85 // arvadosclient.
86 func printArvadosResults(results map[string]interface{}) {
87         for key, value := range results {
88                 // "items", if it exists, holds a map.
89                 // So we print it prettily below.
90                 if key != "items" {
91                         fmt.Println(key, ":", value)
92                 }
93         }
94
95         if value, ok := results["items"]; ok {
96                 items := value.([]interface{})
97                 for index, item := range items {
98                         fmt.Println("===========  ", index, "  ===========")
99                         item_map := item.(map[string]interface{})
100                         if len(item_map) == 0 {
101                                 fmt.Println("item", index, ": empty map")
102                         } else {
103                                 for k, v := range item_map {
104                                         fmt.Println(index, k, ":", v)
105                                 }
106                         }
107                 }
108         }
109 }