1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
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.
14 import org.arvados.sdk.Arvados;
17 import java.util.HashMap;
18 import java.util.List;
20 import java.util.Map.Entry;
22 import java.io.BufferedReader;
23 import java.io.InputStreamReader;
25 public class ArvadosSDKJavaExampleWithPrompt {
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.
32 * Please refer to http://doc.arvados.org/api/index.html for a complete list
33 * of the available API methods.
35 public static void main(String[] args) throws Exception {
36 String apiName = "arvados";
37 String apiVersion = "v1";
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\"]]");
47 System.out.println("\nEnter ^C when you want to quit");
49 // use configured env variables for API TOKEN, HOST and HOST_INSECURE
50 Arvados arv = new Arvados(apiName, apiVersion);
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>>> ");
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"));
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"));
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));
79 System.out.print("\n>>> ");
80 Map paramsMap = new HashMap();
84 param = in.readLine();
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());
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");
101 // Make a "call" for the given resource name and method name
103 System.out.println ("Making a call for " + resourceName + " " + methodName);
104 Map response = arv.call(resourceName, methodName, paramsMap);
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);
114 System.out.println(entry.getKey() + " = " + entry.getValue());
117 } catch (Exception e){
118 System.out.println (e.getMessage());
119 System.out.println ("\nSet up a new call");
121 } catch (Exception e) {
122 System.out.println (e.getMessage());
123 System.out.println ("\nSet up a new call");