From 88c1d9367320ab663911f99ce7f5d076a75f731f Mon Sep 17 00:00:00 2001 From: James Broberg Date: Mon, 24 Sep 2012 14:15:04 +1000 Subject: [PATCH] Extend MailChimpClient to avoid HttpClient Make execute() protected, and make MailChimpGAEClient extend MailChimpClient --- .../com/ecwid/mailchimp/MailChimpClient.java | 2 +- .../ecwid/mailchimp/MailChimpGAEClient.java | 56 +------------------ 2 files changed, 4 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/ecwid/mailchimp/MailChimpClient.java b/src/main/java/com/ecwid/mailchimp/MailChimpClient.java index 979cdd0..6d650cf 100644 --- a/src/main/java/com/ecwid/mailchimp/MailChimpClient.java +++ b/src/main/java/com/ecwid/mailchimp/MailChimpClient.java @@ -39,7 +39,7 @@ public class MailChimpClient { private final HttpClient http = new DefaultHttpClient(); - private String execute(String url, String request) throws IOException { + protected String execute(String url, String request) throws IOException { if(log.isLoggable(Level.FINE)) { log.fine("Post to "+url+" : "+request); } diff --git a/src/main/java/com/ecwid/mailchimp/MailChimpGAEClient.java b/src/main/java/com/ecwid/mailchimp/MailChimpGAEClient.java index 337d5fe..84e2cfd 100644 --- a/src/main/java/com/ecwid/mailchimp/MailChimpGAEClient.java +++ b/src/main/java/com/ecwid/mailchimp/MailChimpGAEClient.java @@ -18,29 +18,24 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; -import java.net.URLEncoder; import java.util.logging.Level; import java.util.logging.Logger; -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonParser; - /** * MailChimp API wrapper. * * @author Vasily Karyaev * @author James Broberg (java.net.* client) */ -public class MailChimpGAEClient { +public class MailChimpGAEClient extends MailChimpClient { + private static final Logger log = Logger.getLogger(MailChimpGAEClient.class.getName()); HttpURLConnection conn = null; - private String execute(String url, String request) throws IOException { + public String execute(String url, String request) throws IOException { if(log.isLoggable(Level.FINE)) { log.fine("Post to "+url+" : "+request); } @@ -81,51 +76,6 @@ private String execute(String url, String request) throws IOException { return response; } - private JsonElement execute(String url, JsonElement request) throws IOException { - return new JsonParser().parse(execute(url, request.toString())); - } - - /** - * Execute MailChimp API method. - * - * @param method MailChimp API method to be executed - * @return execution result - */ - public R execute(MailChimpMethod method) throws IOException, MailChimpException { - final Gson gson = MailChimpGsonFactory.createGson(); - - JsonElement result = execute(buildUrl(method), gson.toJsonTree(method)); - if(result.isJsonObject()) { - JsonElement error = result.getAsJsonObject().get("error"); - if(error != null) { - JsonElement code = result.getAsJsonObject().get("code"); - throw new MailChimpException(code.getAsInt(), error.getAsString()); - } - } - - return gson.fromJson(result, method.getResultType()); - } - - private String buildUrl(MailChimpMethod method) throws UnsupportedEncodingException { - String apikey = method.apikey; - if(apikey == null) throw new IllegalArgumentException("apikey is not set"); - - String prefix; - int dash = apikey.lastIndexOf('-'); - if(dash > 0) { - prefix = apikey.substring(dash + 1); - } else { - throw new IllegalArgumentException("Wrong apikey: "+apikey); - } - - StringBuilder sb = new StringBuilder(); - sb.append("https://"); - sb.append(prefix); - sb.append(".api.mailchimp.com/1.3/?method="); - sb.append(URLEncoder.encode(method.getMethodName(), "UTF-8")); - return sb.toString(); - } - /** * Release resources. */