Merge branch '8784-dir-listings'
[arvados.git] / sdk / java / ArvadosSDKJavaExample.java
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 /**
6  * This Sample test program is useful in getting started with working with Arvados Java SDK.
7  * @author radhika
8  *
9  */
10
11 import org.arvados.sdk.Arvados;
12
13 import java.io.File;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.Set;
19
20 public class ArvadosSDKJavaExample {
21   /** Make sure the following environment variables are set before using Arvados:
22    *      ARVADOS_API_TOKEN, ARVADOS_API_HOST and ARVADOS_API_HOST_INSECURE 
23    *      Set ARVADOS_API_HOST_INSECURE to true if you are using self-singed
24    *      certificates in development and want to bypass certificate validations.
25    *
26    *  If you are not using env variables, you can pass them to Arvados constructor.
27    *
28    *  Please refer to http://doc.arvados.org/api/index.html for a complete list
29    *      of the available API methods.
30    */
31   public static void main(String[] args) throws Exception {
32     String apiName = "arvados";
33     String apiVersion = "v1";
34
35     Arvados arv = new Arvados(apiName, apiVersion);
36
37     // Make a users list call. Here list on users is the method being invoked.
38     // Expect a Map containing the list of users as the response.
39     System.out.println("Making an arvados users.list api call");
40
41     Map<String, Object> params = new HashMap<String, Object>();
42
43     Map response = arv.call("users", "list", params);
44     System.out.println("Arvados users.list:\n");
45     printResponse(response);
46     
47     // get uuid of the first user from the response
48     List items = (List)response.get("items");
49
50     Map firstUser = (Map)items.get(0);
51     String userUuid = (String)firstUser.get("uuid");
52     
53     // Make a users get call on the uuid obtained above
54     System.out.println("\n\n\nMaking a users.get call for " + userUuid);
55     params = new HashMap<String, Object>();
56     params.put("uuid", userUuid);
57     response = arv.call("users", "get", params);
58     System.out.println("Arvados users.get:\n");
59     printResponse(response);
60
61     // Make a pipeline_templates list call
62     System.out.println("\n\n\nMaking a pipeline_templates.list call.");
63
64     params = new HashMap<String, Object>();
65     response = arv.call("pipeline_templates", "list", params);
66
67     System.out.println("Arvados pipelinetempates.list:\n");
68     printResponse(response);
69   }
70   
71   private static void printResponse(Map response){
72     Set<Entry<String,Object>> entrySet = (Set<Entry<String,Object>>)response.entrySet();
73     for (Map.Entry<String, Object> entry : entrySet) {
74       if ("items".equals(entry.getKey())) {
75         List items = (List)entry.getValue();
76         for (Object item : items) {
77           System.out.println("    " + item);
78         }            
79       } else {
80         System.out.println(entry.getKey() + " = " + entry.getValue());
81       }
82     }
83   }
84 }