Skip to content

Commit

Permalink
Extend MailChimpClient to avoid HttpClient
Browse files Browse the repository at this point in the history
Make execute() protected, and make MailChimpGAEClient extend
MailChimpClient
  • Loading branch information
jbroberg committed Sep 24, 2012
1 parent 7aee238 commit 88c1d93
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/ecwid/mailchimp/MailChimpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
56 changes: 3 additions & 53 deletions src/main/java/com/ecwid/mailchimp/MailChimpGAEClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @author James Broberg <[email protected]> (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);
}
Expand Down Expand Up @@ -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> R execute(MailChimpMethod<R> 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.
*/
Expand Down

0 comments on commit 88c1d93

Please sign in to comment.