Merge branch '2411-check-copyright'
[arvados.git] / sdk / java / ArvadosSDKJavaExample.java
index 8a28e259c31420262d90aede01ce4b986727cb7b..ded6ce99dde1fb049ff3b96afbdcd40b6baac97e 100644 (file)
@@ -1,26 +1,32 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: Apache-2.0
+
 /**
  * This Sample test program is useful in getting started with working with Arvados Java SDK.
- * Please also see arvadso 
  * @author radhika
  *
  */
 
+import org.arvados.sdk.Arvados;
+
 import java.io.File;
-import java.io.BufferedWriter;
-import java.io.FileWriter;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-
-import org.arvados.sdk.java.Arvados;
-import org.json.simple.JSONObject;
-import org.json.simple.parser.JSONParser;
-
-import com.google.api.services.discovery.model.RestDescription;
+import java.util.Map.Entry;
+import java.util.Set;
 
 public class ArvadosSDKJavaExample {
   /** Make sure the following environment variables are set before using Arvados:
-   *      ARVADOS_API_TOKEN, ARVADOS_API_HOST, ARVADOS_API_HOST_INSECURE
+   *      ARVADOS_API_TOKEN, ARVADOS_API_HOST and ARVADOS_API_HOST_INSECURE 
+   *      Set ARVADOS_API_HOST_INSECURE to true if you are using self-singed
+   *      certificates in development and want to bypass certificate validations.
+   *
+   *  If you are not using env variables, you can pass them to Arvados constructor.
+   *
+   *  Please refer to http://doc.arvados.org/api/index.html for a complete list
+   *      of the available API methods.
    */
   public static void main(String[] args) throws Exception {
     String apiName = "arvados";
@@ -28,58 +34,51 @@ public class ArvadosSDKJavaExample {
 
     Arvados arv = new Arvados(apiName, apiVersion);
 
-    // Make a discover request. 
-    System.out.println("Making an arvados discovery api request");
-    RestDescription restDescription = arv.discover();
-    System.out.println("Arvados discovery docuemnt:\n" + restDescription);
-    
-    // Make a users.list call
+    // Make a users list call. Here list on users is the method being invoked.
+    // Expect a Map containing the list of users as the response.
     System.out.println("Making an arvados users.list api call");
 
     Map<String, Object> params = new HashMap<String, Object>();
 
-    String response = arv.call("users", "list", params);
-    System.out.println("Arvados users.list:\n" + response);
-
+    Map response = arv.call("users", "list", params);
+    System.out.println("Arvados users.list:\n");
+    printResponse(response);
+    
     // get uuid of the first user from the response
-    JSONParser parser = new JSONParser();
-    Object obj = parser.parse(response);
-    JSONObject jsonObject = (JSONObject) obj;
-    List items = (List)jsonObject.get("items");
+    List items = (List)response.get("items");
 
-    JSONObject firstUser = (JSONObject)items.get(0);
+    Map firstUser = (Map)items.get(0);
     String userUuid = (String)firstUser.get("uuid");
     
-    // Make a users.get call on the uuid obtained above
-    System.out.println("Making a users.get call for " + userUuid);
+    // Make a users get call on the uuid obtained above
+    System.out.println("\n\n\nMaking a users.get call for " + userUuid);
     params = new HashMap<String, Object>();
     params.put("uuid", userUuid);
     response = arv.call("users", "get", params);
-    System.out.println("Arvados users.get:\n" + response);
+    System.out.println("Arvados users.get:\n");
+    printResponse(response);
 
-    // Make a users.create call
-    System.out.println("Making a users.create call.");
-    
-    params = new HashMap<String, Object>();
-    params.put("user", "{}");
-    response = arv.call("users", "create", params);
-    System.out.println("Arvados users.create:\n" + response);
-
-    // delete the newly created user
-    parser = new JSONParser();
-    obj = parser.parse(response);
-    jsonObject = (JSONObject) obj;
-    userUuid = (String)jsonObject.get("uuid");
-    params = new HashMap<String, Object>();
-    params.put("uuid", userUuid);
-    response = arv.call("users", "delete", params);
-
-    // Make a pipeline_templates.list call
-    System.out.println("Making a pipeline_templates.list call.");
+    // Make a pipeline_templates list call
+    System.out.println("\n\n\nMaking a pipeline_templates.list call.");
 
     params = new HashMap<String, Object>();
     response = arv.call("pipeline_templates", "list", params);
 
-    System.out.println("Arvados pipelinetempates.list:\n" + response);
+    System.out.println("Arvados pipelinetempates.list:\n");
+    printResponse(response);
+  }
+  
+  private static void printResponse(Map response){
+    Set<Entry<String,Object>> entrySet = (Set<Entry<String,Object>>)response.entrySet();
+    for (Map.Entry<String, Object> entry : entrySet) {
+      if ("items".equals(entry.getKey())) {
+        List items = (List)entry.getValue();
+        for (Object item : items) {
+          System.out.println("    " + item);
+        }            
+      } else {
+        System.out.println(entry.getKey() + " = " + entry.getValue());
+      }
+    }
   }
 }