Merge branch '8784-dir-listings'
[arvados.git] / sdk / java / ArvadosSDKJavaExampleWithPrompt.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 using Arvados Java SDK.
7  * This program creates an Arvados instance using the configured environment variables.
8  * It then provides a prompt to input method name and input parameters. 
9  * The program them invokes the API server to execute the specified method.  
10  * 
11  * @author radhika
12  */
13
14 import org.arvados.sdk.Arvados;
15
16 import java.io.File;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Map.Entry;
21 import java.util.Set;
22 import java.io.BufferedReader;
23 import java.io.InputStreamReader;
24
25 public class ArvadosSDKJavaExampleWithPrompt {
26   /**
27    * Make sure the following environment variables are set before using Arvados:
28    * ARVADOS_API_TOKEN, ARVADOS_API_HOST and ARVADOS_API_HOST_INSECURE Set
29    * ARVADOS_API_HOST_INSECURE to true if you are using self-singed certificates
30    * in development and want to bypass certificate validations.
31    * 
32    * Please refer to http://doc.arvados.org/api/index.html for a complete list
33    * of the available API methods.
34    */
35   public static void main(String[] args) throws Exception {
36     String apiName = "arvados";
37     String apiVersion = "v1";
38
39     System.out.print("Welcome to Arvados Java SDK.");
40     System.out.println("\nYou can use this example to call API methods interactively.");
41     System.out.println("\nPlease refer to http://doc.arvados.org/api/index.html for api documentation");
42     System.out.println("\nTo make the calls, enter input data at the prompt.");
43     System.out.println("When entering parameters, you may enter a simple string or a well-formed json.");
44     System.out.println("For example to get a user you may enter:  user, zzzzz-12345-67890");
45     System.out.println("Or to filter links, you may enter:  filters, [[ \"name\", \"=\", \"can_manage\"]]");
46
47     System.out.println("\nEnter ^C when you want to quit");
48
49     // use configured env variables for API TOKEN, HOST and HOST_INSECURE
50     Arvados arv = new Arvados(apiName, apiVersion);
51
52     while (true) {
53       try {
54         // prompt for resource
55         System.out.println("\n\nEnter Resource name (for example users)");
56         System.out.println("\nAvailable resources are: " + arv.getAvailableResourses());
57         System.out.print("\n>>> ");
58
59         // read resource name
60         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
61         String resourceName = in.readLine().trim();
62         if ("".equals(resourceName)) {
63           throw (new Exception("No resource name entered"));
64         }
65         // read method name
66         System.out.println("\nEnter method name (for example get)");
67         System.out.println("\nAvailable methods are: " + arv.getAvailableMethodsForResourse(resourceName));
68         System.out.print("\n>>> ");
69         String methodName = in.readLine().trim();
70         if ("".equals(methodName)) {
71           throw (new Exception("No method name entered"));
72         }
73
74         // read method parameters
75         System.out.println("\nEnter parameter name, value (for example uuid, uuid-value)");
76         System.out.println("\nAvailable parameters are: " + 
77               arv.getAvailableParametersForMethod(resourceName, methodName));
78         
79         System.out.print("\n>>> ");
80         Map paramsMap = new HashMap();
81         String param = "";
82         try {
83           do {
84             param = in.readLine();
85             if (param.isEmpty())
86               break;
87             int index = param.indexOf(","); // first comma
88             String paramName = param.substring(0, index);
89             String paramValue = param.substring(index+1);
90             paramsMap.put(paramName.trim(), paramValue.trim());
91
92             System.out.println("\nEnter parameter name, value (for example uuid, uuid-value)");
93             System.out.print("\n>>> ");
94           } while (!param.isEmpty());
95         } catch (Exception e) {
96           System.out.println (e.getMessage());
97           System.out.println ("\nSet up a new call");
98           continue;
99         }
100
101         // Make a "call" for the given resource name and method name
102         try {
103           System.out.println ("Making a call for " + resourceName + " " + methodName);
104           Map response = arv.call(resourceName, methodName, paramsMap);
105
106           Set<Entry<String,Object>> entrySet = (Set<Entry<String,Object>>)response.entrySet();
107           for (Map.Entry<String, Object> entry : entrySet) {
108             if ("items".equals(entry.getKey())) {
109               List items = (List)entry.getValue();
110               for (Object item : items) {
111                 System.out.println("    " + item);
112               }            
113             } else {
114               System.out.println(entry.getKey() + " = " + entry.getValue());
115             }
116           }
117         } catch (Exception e){
118           System.out.println (e.getMessage());
119           System.out.println ("\nSet up a new call");
120         }
121       } catch (Exception e) {
122         System.out.println (e.getMessage());
123         System.out.println ("\nSet up a new call");
124       }
125     }
126   }
127 }