Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasily Karyaev committed Sep 24, 2012
1 parent e2c1db4 commit addbdaf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 30 deletions.
42 changes: 14 additions & 28 deletions src/main/java/com/ecwid/mailchimp/MailChimpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.ecwid.mailchimp;

import com.ecwid.mailchimp.connection.DefaultConnectionManager;
import com.ecwid.mailchimp.connection.MailChimpConnectionManager;
import com.ecwid.mailchimp.internal.gson.MailChimpGsonFactory;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
Expand All @@ -25,11 +27,6 @@
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

/**
* MailChimp API wrapper.
Expand All @@ -39,38 +36,27 @@
public class MailChimpClient implements Closeable {
private static final Logger log = Logger.getLogger(MailChimpClient.class.getName());

private final MailChimpAPIConnection connection;
private final MailChimpConnectionManager connection;

/**
* Construct a {@code MailChimpClient} object accessing MailChimp API service point
* through the specified connection manager.
*
* @param connection connection manager to be used to access the service point
* through the {@linkplain DefaultConnectionManager default connection manager}.
*/
public MailChimpClient(MailChimpAPIConnection connection) {
this.connection = connection;
public MailChimpClient() {
this(new DefaultConnectionManager());
}

/**
* Construct a {@code MailChimpClient} object accessing MailChimp API service point
* through the default connection manager (based on Apache HttpClient library).
* through the specified connection manager.
* <p>
* Use this constructor if the {@linkplain DefaultConnectionManager default connection manager}
* is not suitable (for instance, in GAE environment).
*
* @param connection connection manager to be used to access the service point
*/
public MailChimpClient() {
this(new MailChimpAPIConnection() {
final HttpClient http = new DefaultHttpClient();

@Override
public String post(String url, String payload) throws IOException {
HttpPost post = new HttpPost(url);
post.setEntity(new StringEntity(URLEncoder.encode(payload, "UTF-8")));
return http.execute(post, new BasicResponseHandler());
}

@Override
public void close() {
http.getConnectionManager().shutdown();
}
});
public MailChimpClient(MailChimpConnectionManager connection) {
this.connection = connection;
}

private String execute(String url, String request) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2012 Ecwid, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ecwid.mailchimp.connection;

import java.io.IOException;
import java.net.URLEncoder;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

/**
* Default implementation of {@link MailChimpConnectionManager}
* which uses Apache HttpClient library to access MailChimp API service point.
*
* @author Vasily Karyaev <[email protected]>
*/
public class DefaultConnectionManager implements MailChimpConnectionManager {
private final HttpClient http = new DefaultHttpClient();

@Override
public String post(String url, String payload) throws IOException {
HttpPost post = new HttpPost(url);
post.setEntity(new StringEntity(URLEncoder.encode(payload, "UTF-8")));
return http.execute(post, new BasicResponseHandler());
}

@Override
public void close() {
http.getConnectionManager().shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ecwid.mailchimp;
package com.ecwid.mailchimp.connection;

import java.io.Closeable;
import java.io.IOException;
Expand All @@ -23,7 +23,7 @@
*
* @author Vasily Karyaev <[email protected]>
*/
public interface MailChimpAPIConnection extends Closeable {
public interface MailChimpConnectionManager extends Closeable {

/**
* Make a POST request to MailChimp API service point.
Expand Down

0 comments on commit addbdaf

Please sign in to comment.