package com.orgsync.api.examples;

import java.util.List;
import java.util.concurrent.ExecutionException;

import com.orgsync.api.ApiClient;
import com.orgsync.api.ApiResponse;
import com.orgsync.api.Modules;
import com.orgsync.api.OrgSync;
import com.orgsync.api.OrgsModule;
import com.orgsync.api.Version;
import com.orgsync.api.messages.Org;

public class SimpleRequest {

	public static void main(final String[] args) throws InterruptedException,
			ExecutionException {
		String apiKey = "dd6b9d2beb614611c5eb9f56c34b743d1d86f385";
		String host = "http://localhost/api";
		ApiClient client = OrgSync.newApiClient(apiKey, Version.V2, host);

		System.out.println("Requesting orgs");
		OrgsModule module = client.getModule(Modules.ORGS);
		ApiResponse<List<Org>> result = module.getOrgs().get();

		if (result.isSuccess()) {
			System.out.println("Recieved following orgs:");
			System.out.println(joinList(result.getResult(), "\n"));
		} else {
			System.err.println("Error attempting to retrieve orgs!");
			System.err.println(result.getError());
		}

		System.out.println("Cleanup client");
		client.destroy();

		System.out.println("Exiting...");
	}

	private static <T> String joinList(final List<T> list, final String sep) {
		StringBuilder str = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			str.append(list.get(i).toString());
			if (i < list.size() - 1) {
				str.append(sep);
			}
		}

		return str.toString();
	}

}

@@ -567,12 +567,18 @@
 host
+:8080
 /api
+/
 %22;%0A%09
@@ -642,24 +642,33 @@
 V2, host);%0A%0A
+%09%09try %7B%0A%09
 %09%09System.out
@@ -696,16 +696,17 @@
 orgs%22);%0A
+%09
 %09%09OrgsMo
@@ -753,16 +753,17 @@
 RGS);%0A%09%09
+%09
 ApiRespo
@@ -810,17 +810,17 @@
 .get();%0A
-%0A
+%09
 %09%09if (re
@@ -838,24 +838,25 @@
 ess()) %7B%0A%09%09%09
+%09
 System.out.p
@@ -887,24 +887,25 @@
 ng orgs:%22);%0A
+%09
 %09%09%09System.ou
@@ -953,16 +953,17 @@
 n%22));%0A%09%09
+%09
 %7D else %7B
@@ -963,16 +963,17 @@
  else %7B%0A
+%09
 %09%09%09Syste
@@ -1024,24 +1024,25 @@
 orgs!%22);%0A%09%09%09
+%09
 System.err.p
@@ -1070,21 +1070,22 @@
 r());%0A%09%09
+%09
 %7D
-%0A
 %0A%09%09
+%09
 System.o
@@ -1114,16 +1114,31 @@
 ient%22);%0A
+%09%09%7D finally %7B%0A%09
 %09%09client
@@ -1148,16 +1148,20 @@
 stroy();
+%0A%09%09%7D
 %0A%0A%09%09Syst

Fix the poor example client which I badly broke... Now exits cleanly.