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