2525: more testing. also "=" works in place of "is_a" for a filter.
[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("\nYou can use this example to call API methods interactively.");
37     System.out.println("\nTo make the calls, enter input data at the prompt.");
38     System.out.println("When entering parameters, you may enter a simple string or a well-formed json.");
39     System.out.println("For example to get a user you may enter:  user, zzzzz-12345-67890");
40     System.out.println("Or to filter links, you may enter:  filters, [[ \"name\", \"=\", \"can_manage\"]]");
41     
42     System.out.println("\nEnter ^C when you want to quit");
43
44     // use configured env variables for API TOKEN, HOST and HOST_INSECURE
45     Arvados arv = new Arvados(apiName, apiVersion);
46
47     while (true) {
48       // prompt for resource
49       System.out.println("\n\nEnter Resource name (for example users)");
50       System.out.print("\n>>> ");
51
52       // read resource name
53       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
54       String resourceName = in.readLine().trim();
55
56       // read method name
57       System.out.println("\nEnter method name (for example get)");
58       System.out.print("\n>>> ");
59       String methodName = in.readLine().trim();
60
61       // read method parameters
62       System.out.println("\nEnter parameter name, value (for example uuid, uuid-value)");
63       System.out.print("\n>>> ");
64       Map paramsMap = new HashMap();
65       String param = "";
66       try {
67         do {
68           param = in.readLine();
69           if (param.isEmpty())
70             break;
71           int index = param.indexOf(","); // first comma
72           String paramName = param.substring(0, index);
73           String paramValue = param.substring(index+1);
74           paramsMap.put(paramName.trim(), paramValue.trim());
75
76           System.out.println("\nEnter parameter name, value (for example uuid, uuid-value)");
77           System.out.print("\n>>> ");
78         } while (!param.isEmpty());
79       } catch (Exception e) {
80         System.out.println (e.getMessage());
81         System.out.println ("\nSet up a new call");
82         continue;
83       }
84
85       // Make a "call" for the given resource name and method name
86       try {
87         System.out.println ("Making a call for " + resourceName + " " + methodName);
88         Map response = arv.call(resourceName, methodName, paramsMap);
89         
90         Set<Entry<String,Object>> entrySet = (Set<Entry<String,Object>>)response.entrySet();
91         for (Map.Entry<String, Object> entry : entrySet) {
92           if ("items".equals(entry.getKey())) {
93             List items = (List)entry.getValue();
94             for (Object item : items) {
95               System.out.println("    " + item);
96             }            
97           } else {
98             System.out.println(entry.getKey() + " = " + entry.getValue());
99           }
100         }
101       } catch (Exception e){
102         System.out.println (e.getMessage());
103         System.out.println ("\nSet up a new call");
104       }
105     }
106   }
107 }