From a74ae790b5a0682fa00423626103482c145e1b91 Mon Sep 17 00:00:00 2001 From: radhika Date: Wed, 30 Apr 2014 21:13:00 -0400 Subject: [PATCH] 2525: add apiVersion to constructor args --- sdk/java/README | 23 +++- .../java/org/arvados/sdk/java/Arvados.java | 120 ++++-------------- .../org/arvados/sdk/java/ArvadosTest.java | 51 ++------ 3 files changed, 52 insertions(+), 142 deletions(-) diff --git a/sdk/java/README b/sdk/java/README index d270ccbfb7..423cf97eaa 100644 --- a/sdk/java/README +++ b/sdk/java/README @@ -18,10 +18,25 @@ == Setting up the environment - - The following three environment variables are required by the SDK - - ARVADOS_API_TOKEN, ARVADOS_API_HOST, ARVADOS_API_HOST_INSECURE - + - The SDK requires a running Arvados API server. The information about the + API server needs to be passed to the SDK using environment variables. + + - The following two environment variables are required by the SDK + + ARVADOS_API_TOKEN, ARVADOS_API_HOST + + Below are examples using a .bashrc file: + + export ARVADOS_API_TOKEN=z40gplmla6i58rsg96jhg5u41ewdl5rj4g1py2xg6s6e2lsc3 + export ARVADOS_API_HOST=localhost:3001 + + - You can also set ARVADOS_API_HOST_INSECURE to true if you are using + self-signed certificates and want to bypass certificate validations. + + Below is an example using a .bashrc file: + + export ARVADOS_API_HOST_INSECURE=true + == Building the Arvados SDK diff --git a/sdk/java/src/main/java/org/arvados/sdk/java/Arvados.java b/sdk/java/src/main/java/org/arvados/sdk/java/Arvados.java index f8a7dadd9e..260b68d69b 100644 --- a/sdk/java/src/main/java/org/arvados/sdk/java/Arvados.java +++ b/sdk/java/src/main/java/org/arvados/sdk/java/Arvados.java @@ -32,7 +32,7 @@ import java.util.regex.Pattern; import org.apache.log4j.Logger; public class Arvados { - // HttpTransport and JsonFactory are thread-safe. So, use global instances. + // HttpTransport and JsonFactory are thread-safe. So, use global instances. private static HttpTransport HTTP_TRANSPORT; private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); @@ -40,7 +40,7 @@ public class Arvados { private static String ARVADOS_API_TOKEN; private static String ARVADOS_API_HOST; - private static String ARVADOS_API_HOST_INSECURE; + private static boolean ARVADOS_API_HOST_INSECURE; private static String ARVADOS_ROOT_URL; @@ -48,88 +48,14 @@ public class Arvados { // Get it on a discover call and reuse on the call requests RestDescription restDescription = null; + String apiName = null; + String apiVersion = null; - public static void main(String[] args) throws Exception { - if (args.length == 0) { - showMainHelp(); - error(null, "Missing input args"); - } else { - String command = args[0]; - if (command.equals("help")) { - help(args); - } else if (command.equals("call")) { - List params = Arrays.asList(args); - - if (args.length == 1) { - error("call", "missing api name"); - } - - Arvados arv = new Arvados(args[1]); - String response = arv.call(params); - System.out.println (response); - } else if (command.equals("discover")) { - List params = Arrays.asList(args); - - if (args.length == 1) { - error("call", "missing api name"); - } - - Arvados arv = new Arvados(args[1]); - RestDescription restDescription = arv.discover(params); - System.out.println(restDescription); - } else { - error(null, "unknown command: " + command); - } - } - } - - protected static void help(String[] args) throws Exception { - if (args.length == 1) { - showMainHelp(); - } else { - String helpCommand = args[1]; - if (helpCommand.equals("call")) { - StringBuffer buffer = new StringBuffer(); - buffer.append("\nUsage: Arvados call methodName [parameters]"); - buffer.append("\nExamples:"); - buffer.append("\n Arvados call arvados v1 users.list"); - buffer.append("\n Arvados call arvados v1 users.get "); - buffer.append("\n Arvados call arvados v1 pipeline_instances.list"); - logger.debug(buffer.toString()); - System.out.println(buffer.toString()); - } else if (helpCommand.equals("discover")) { - StringBuffer buffer = new StringBuffer(); - buffer.append("\nUsage"); - buffer.append("\nExamples:"); - buffer.append("\n Arvados discover arvados v1"); - logger.debug(buffer.toString()); - System.out.println(buffer.toString()); - } else { - error(null, "unknown command: " + helpCommand); - } - } - } - - protected static void showMainHelp() { - StringBuffer buffer = new StringBuffer(); - buffer.append("\narvados"); - buffer.append("\nFor more help on a specific command, type one of:"); - buffer.append("\n Arvados help call"); - buffer.append("\n Arvados help discover"); - logger.debug(buffer.toString()); - System.out.println(buffer.toString()); - } - - private static void error(String command, String detail) throws Exception { - String errorDetail = "ERROR: " + detail + - "For help, type: Arvados" + (command == null ? "" : " help " + command); - - logger.debug(errorDetail); - throw new Exception(errorDetail); - } - - public Arvados (String apiName){ + public Arvados (String apiName, String apiVersion){ try { + this.apiName = apiName; + this.apiVersion = apiVersion; + // Read needed environmental variables ARVADOS_API_TOKEN = System.getenv().get("ARVADOS_API_TOKEN"); if (ARVADOS_API_TOKEN == null) { @@ -144,14 +70,13 @@ public class Arvados { ARVADOS_ROOT_URL = "https://" + ARVADOS_API_HOST; ARVADOS_ROOT_URL += (ARVADOS_API_HOST.endsWith("/")) ? "" : "/"; - ARVADOS_API_HOST_INSECURE = System.getenv().get("ARVADOS_API_HOST_INSECURE"); - if (ARVADOS_API_HOST_INSECURE == null) { - throw new Exception("Missing environment variable: ARVADOS_API_HOST_INSECURE"); - } + ARVADOS_API_HOST_INSECURE = "true".equals(System.getenv().get("ARVADOS_API_HOST_INSECURE")) ? true : false; // Create HTTP_TRANSPORT object NetHttpTransport.Builder builder = new NetHttpTransport.Builder(); - builder.doNotValidateCertificate(); + if (ARVADOS_API_HOST_INSECURE) { + builder.doNotValidateCertificate(); + } HTTP_TRANSPORT = builder.build(); } catch (Throwable t) { t.printStackTrace(); @@ -164,14 +89,8 @@ public class Arvados { * @return * @throws Exception */ - public RestDescription discover(List params) throws Exception { - if (params.size() == 1) { - error("call", "missing api name"); - } else if (params.size() == 2) { - error("call", "missing api version"); - } - - restDescription = loadArvadosApi(params.get(1), params.get(2)); + public RestDescription discover() throws Exception { + restDescription = loadArvadosApi(apiName, apiVersion); // compute method details ArrayList result = Lists.newArrayList(); @@ -182,7 +101,7 @@ public class Arvados { Collections.sort(result); StringBuffer buffer = new StringBuffer(); for (MethodDetails methodDetail : result) { - buffer.append("\nArvados call " + params.get(1) + " " + params.get(2) + " " + methodDetail.name); + buffer.append("\nArvados call " + apiName + " " + apiVersion + " " + methodDetail.name); for (String param : methodDetail.requiredParameters) { buffer.append(" <" + param + ">"); } @@ -429,5 +348,14 @@ public class Arvados { error("call", "duplicate parameter: " + argName); } } + + private static void error(String command, String detail) throws Exception { + String errorDetail = "ERROR: " + detail + + "For help, type: Arvados" + (command == null ? "" : " help " + command); + + logger.debug(errorDetail); + throw new Exception(errorDetail); + } + } diff --git a/sdk/java/src/test/java/org/arvados/sdk/java/ArvadosTest.java b/sdk/java/src/test/java/org/arvados/sdk/java/ArvadosTest.java index e2dbc19ce1..77157e5c46 100644 --- a/sdk/java/src/test/java/org/arvados/sdk/java/ArvadosTest.java +++ b/sdk/java/src/test/java/org/arvados/sdk/java/ArvadosTest.java @@ -20,48 +20,15 @@ import org.json.simple.parser.JSONParser; */ public class ArvadosTest { - @Test(expected=Exception.class) - public void testMainWithNoParams() throws Exception { - String[] args = new String[0]; - Arvados.main(args); - } - - @Test(expected=Exception.class) - public void testHelp() throws Exception { - String[] args = new String[1]; - - args[0] = "help"; - Arvados.help(args); // expect this to succeed with no problems - - args = new String[2]; - - args[0] = "help"; - args[1] = "call"; - Arvados.main(args); // call via main - - args[0] = "help"; - args[1] = "discover"; - Arvados.help(args); // call help directly - - args[0] = "help"; - args[1] = "unknown"; - Arvados.help(args); // expect exception - } - /** * test discover method * @throws Exception */ @Test public void testDiscover() throws Exception { - Arvados arv = new Arvados("arvados"); - - List params = new ArrayList(); - params.add("discover"); - params.add("arvados"); - params.add("v1"); + Arvados arv = new Arvados("arvados", "v1"); - RestDescription restDescription = arv.discover(params); + RestDescription restDescription = arv.discover(); // The discover method returns the supported methods Map resources = restDescription.getResources(); @@ -81,7 +48,7 @@ public class ArvadosTest { */ @Test public void testCallUsersList() throws Exception { - Arvados arv = new Arvados("arvados"); + Arvados arv = new Arvados("arvados", "v1"); List params = new ArrayList(); params.add("call"); @@ -116,7 +83,7 @@ public class ArvadosTest { */ @Test public void testCallUsersGet() throws Exception { - Arvados arv = new Arvados("arvados"); + Arvados arv = new Arvados("arvados", "v1"); // call user.system and get uuid of this user List params = new ArrayList(); @@ -158,7 +125,7 @@ public class ArvadosTest { */ @Test public void testCreateUser() throws Exception { - Arvados arv = new Arvados("arvados"); + Arvados arv = new Arvados("arvados", "v1"); File file = new File(getClass().getResource( "/create_user.json" ).toURI()); String filePath = file.getPath(); @@ -183,7 +150,7 @@ public class ArvadosTest { */ @Test public void testUnsupportedApiName() throws Exception { - Arvados arv = new Arvados("not_arvados"); + Arvados arv = new Arvados("not_arvados", "v1"); List params = new ArrayList(); params.add("call"); @@ -208,7 +175,7 @@ public class ArvadosTest { */ @Test public void testUnsupportedVersion() throws Exception { - Arvados arv = new Arvados("arvados"); + Arvados arv = new Arvados("arvados", "v1"); List params = new ArrayList(); params.add("call"); @@ -233,7 +200,7 @@ public class ArvadosTest { */ @Test public void testCallWithTooFewParams() throws Exception { - Arvados arv = new Arvados("arvados"); + Arvados arv = new Arvados("arvados", "v1"); List params = new ArrayList(); params.add("call"); @@ -257,7 +224,7 @@ public class ArvadosTest { */ @Test public void testCreateAndGetPipelineTemplate() throws Exception { - Arvados arv = new Arvados("arvados"); + Arvados arv = new Arvados("arvados", "v1"); File file = new File(getClass().getResource( "/first_pipeline.json" ).toURI()); String filePath = file.getPath(); -- 2.30.2