2525: Add ArvadosSDKJavaExampleWithPrompt.java to serve as an easier tool to make...
[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
17 import java.io.BufferedReader;
18 import java.io.InputStreamReader;
19
20 public class ArvadosSDKJavaExampleWithPrompt {
21   /**
22    * Make sure the following environment variables are set before using Arvados:
23    * ARVADOS_API_TOKEN, ARVADOS_API_HOST and ARVADOS_API_HOST_INSECURE Set
24    * ARVADOS_API_HOST_INSECURE to true if you are using self-singed certificates
25    * in development and want to bypass certificate validations.
26    * 
27    * Please refer to http://doc.arvados.org/api/index.html for a complete list
28    * of the available API methods.
29    */
30   public static void main(String[] args) throws Exception {
31     String apiName = "arvados";
32     String apiVersion = "v1";
33
34     System.out.print("Welcome to Arvados Java SDK");
35     System.out.println("You can use this example to call API methods");
36     System.out.println("Enter ^C when you want to quit");
37
38     // use configured env variables for API TOKEN, HOST and HOST_INSECURE
39     Arvados arv = new Arvados(apiName, apiVersion);
40
41     while (true) {
42       // prompt for resource
43       System.out.println("\n\nEnter Resource name (for example users): ");
44
45       // read resource name
46       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
47       String resourceName = in.readLine().trim();
48
49       // read method name
50       System.out.println("\nEnter method name (for example get): ");
51       String methodName = in.readLine().trim();
52
53       // read method parameters
54       System.out
55           .println("\nEnter parameter name, value (for example uuid, uuid-value): ");
56       Map paramsMap = new HashMap();
57       String param = "";
58       try {
59         do {
60           param = in.readLine();
61           if (param.isEmpty())
62             break;
63
64           String[] params = param.split(",");
65           paramsMap.put(params[0].trim(), params[1].trim());
66
67           System.out.println("\nEnter parameter name, value (for example uuid, uuid-value): ");
68         } while (!param.isEmpty());
69       } catch (Exception e) {
70         System.out.println (e.getMessage());
71         System.out.println ("\nStart over");
72         continue;
73       }
74
75       // Make a "call" for the given resource name and method name
76       try {
77         System.out.println ("Making a call for " + resourceName + " " + methodName);
78         Map response = arv.call(resourceName, methodName, paramsMap);
79         System.out.println(response);
80       } catch (Exception e){
81         System.out.println (e.getMessage());
82         System.out.println ("\nStart over");
83       }
84     }
85   }
86 }