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