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