2525: improve response printing in the examples for readability.
[arvados.git] / sdk / java / ArvadosSDKJavaExampleWithPrompt.java
1 /**
2  * This Sample test program is useful in getting started with using Arvados Java SDK.
3  * This program creates an Arvados instance using the configured environment variables.
4  * It then provides a prompt to input method name and input parameters. 
5  * The program them invokes the API server to execute the specified method.  
6  * 
7  * @author radhika
8  */
9
10 import org.arvados.sdk.java.Arvados;
11
12 import java.io.File;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
18 import java.io.BufferedReader;
19 import java.io.InputStreamReader;
20
21 public class ArvadosSDKJavaExampleWithPrompt {
22   /**
23    * Make sure the following environment variables are set before using Arvados:
24    * ARVADOS_API_TOKEN, ARVADOS_API_HOST and ARVADOS_API_HOST_INSECURE Set
25    * ARVADOS_API_HOST_INSECURE to true if you are using self-singed certificates
26    * in development and want to bypass certificate validations.
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     System.out.print("Welcome to Arvados Java SDK");
36     System.out.println("You can use this example to call API methods");
37     System.out.println("Enter ^C when you want to quit");
38
39     // use configured env variables for API TOKEN, HOST and HOST_INSECURE
40     Arvados arv = new Arvados(apiName, apiVersion);
41
42     while (true) {
43       // prompt for resource
44       System.out.println("\n\nEnter Resource name (for example users): ");
45
46       // read resource name
47       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
48       String resourceName = in.readLine().trim();
49
50       // read method name
51       System.out.println("\nEnter method name (for example get): ");
52       String methodName = in.readLine().trim();
53
54       // read method parameters
55       System.out
56           .println("\nEnter parameter name, value (for example uuid, uuid-value): ");
57       Map paramsMap = new HashMap();
58       String param = "";
59       try {
60         do {
61           param = in.readLine();
62           if (param.isEmpty())
63             break;
64
65           String[] params = param.split(",");
66           paramsMap.put(params[0].trim(), params[1].trim());
67
68           System.out.println("\nEnter parameter name, value (for example uuid, uuid-value): ");
69         } while (!param.isEmpty());
70       } catch (Exception e) {
71         System.out.println (e.getMessage());
72         System.out.println ("\nStart over");
73         continue;
74       }
75
76       // Make a "call" for the given resource name and method name
77       try {
78         System.out.println ("Making a call for " + resourceName + " " + methodName);
79         Map response = arv.call(resourceName, methodName, paramsMap);
80         
81         Set<Entry<String,Object>> entrySet = (Set<Entry<String,Object>>)response.entrySet();
82         for (Map.Entry<String, Object> entry : entrySet) {
83           if ("items".equals(entry.getKey())) {
84             List items = (List)entry.getValue();
85             for (Object item : items) {
86               System.out.println("    " + item);
87             }            
88           } else {
89             System.out.println(entry.getKey() + " = " + entry.getValue());
90           }
91         }
92       } catch (Exception e){
93         System.out.println (e.getMessage());
94         System.out.println ("\nStart over");
95       }
96     }
97   }
98 }