Skip to content

Commit

Permalink
Refactor the performRequest function so that it will support API
Browse files Browse the repository at this point in the history
23 - get rid of apache http stuff which have been removed
  • Loading branch information
Yinfei Ru committed Oct 26, 2015
1 parent a32b31e commit 948b37b
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 50 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Expand Up @@ -12,14 +12,14 @@ buildscript {
}

android {
compileSdkVersion 21
compileSdkVersion 23
buildToolsVersion "22.0.1"

defaultConfig {
// If you change minSdkVersion or targetSdkVersion below, you must also change
// the relevant attributes of the <uses-sdk> tag in AndroidManifest.xml
minSdkVersion 9
targetSdkVersion 21
targetSdkVersion 23
testApplicationId "com.mixpanel.android.mpmetrics"
testInstrumentationRunner "android.test.InstrumentationTestRunner"

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -3,4 +3,4 @@ group = com.mixpanel.android
# As long as we support building from source in Eclipse, we can't
# just use BuildConfig.MIXPANEL_VERSION in our source, so if you
# update the value of version you'll also need to update MPConfig.VERSION
version = 4.7.0-RC1
version = 4.7.0
@@ -1,20 +1,19 @@
package com.mixpanel.android.mpmetrics;

import android.content.Context;
import android.os.Bundle;
import android.test.AndroidTestCase;

import com.mixpanel.android.util.RemoteService;
import com.mixpanel.android.util.HttpService;
import com.mixpanel.android.viewcrawler.UpdatesFromMixpanel;

import org.apache.http.NameValuePair;
import org.json.JSONArray;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.net.ssl.SSLSocketFactory;

Expand Down Expand Up @@ -337,7 +336,7 @@ private byte[] bytes(String s) {

private static class MockPoster extends HttpService {
@Override
public byte[] performRequest(String url, List<NameValuePair> pairs, SSLSocketFactory socketFactory) throws IOException {
public byte[] performRequest(String url, Map<String, Object> pairs, SSLSocketFactory socketFactory) throws IOException {
assertNull(pairs);
requestedUrls.add(url);

Expand Down
Expand Up @@ -6,20 +6,18 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.test.AndroidTestCase;

import com.mixpanel.android.util.ImageStore;
import com.mixpanel.android.util.RemoteService;
import com.mixpanel.android.util.HttpService;
import com.mixpanel.android.viewcrawler.UpdatesFromMixpanel;

import org.apache.http.NameValuePair;
import org.json.JSONArray;

import java.io.ByteArrayOutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -66,7 +64,7 @@ public SharedPreferences get(final long timeout, final TimeUnit unit) throws Int
mExpectations = new Expectations();
mMockPoster = new HttpService() {
@Override
public byte[] performRequest(String endpointUrl, List<NameValuePair> nameValuePairs, SSLSocketFactory socketFactory) {
public byte[] performRequest(String endpointUrl, Map<String, Object> nameValuePairs, SSLSocketFactory socketFactory) {
return mExpectations.setExpectationsRequest(endpointUrl, nameValuePairs);
}
};
Expand Down Expand Up @@ -305,7 +303,7 @@ public void checkExpectations() {
}
}

public synchronized byte[] setExpectationsRequest(final String endpointUrl, List<NameValuePair> nameValuePairs) {
public synchronized byte[] setExpectationsRequest(final String endpointUrl, Map<String, Object> nameValuePairs) {
if (endpointUrl.equals(mExpectUrl)) {
return TestUtils.bytes(mResponse);
} else if (Pattern.matches("^http://mixpanel.com/Balok.{0,3}\\.jpg$", endpointUrl)) {
Expand All @@ -330,7 +328,7 @@ public synchronized String toString() {
private String mExpectUrl = null;
private String mResponse = null;
private String badUrl = null;
private List<NameValuePair> badNameValuePairs = null;
private Map<String, Object> badNameValuePairs = null;
private boolean mResultsFound = false;
private boolean resultsBad = false;
private byte[] imageBytes;
Expand Down
Expand Up @@ -9,7 +9,6 @@
import com.mixpanel.android.util.RemoteService;
import com.mixpanel.android.util.HttpService;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -18,6 +17,7 @@
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
Expand All @@ -39,7 +39,7 @@ public void setUp() {

final RemoteService mockPoster = new HttpService() {
@Override
public byte[] performRequest(String endpointUrl, List<NameValuePair> nameValuePairs, SSLSocketFactory socketFactory)
public byte[] performRequest(String endpointUrl, Map<String, Object> nameValuePairs, SSLSocketFactory socketFactory)
throws ServiceUnavailableException, IOException {
try {
if (null == nameValuePairs) {
Expand All @@ -61,9 +61,9 @@ public byte[] performRequest(String endpointUrl, List<NameValuePair> nameValuePa
}
// ELSE


assertEquals(nameValuePairs.get(0).getName(), "data");
final String jsonData = Base64Coder.decodeString(nameValuePairs.get(0).getValue());
JSONObject jsonPairs = new JSONObject(nameValuePairs);
assertEquals(jsonPairs.keys().next(), "data");
final String jsonData = Base64Coder.decodeString(jsonPairs.get("data").toString());
JSONArray msg = new JSONArray(jsonData);
JSONObject event = msg.getJSONObject(0);
mPerformRequestCalls.put(event.getString("event"));
Expand Down
Expand Up @@ -16,7 +16,6 @@
import com.mixpanel.android.util.RemoteService;
import com.mixpanel.android.util.HttpService;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -410,7 +409,7 @@ public int addJSON(JSONObject message, MPDbAdapter.Table table) {

final RemoteService mockPoster = new HttpService() {
@Override
public byte[] performRequest(String endpointUrl, List<NameValuePair> nameValuePairs, SSLSocketFactory socketFactory) {
public byte[] performRequest(String endpointUrl, Map<String, Object> nameValuePairs, SSLSocketFactory socketFactory) {
final boolean isIdentified = isIdentifiedRef.get();
if (null == nameValuePairs) {
if (isIdentified) {
Expand All @@ -421,14 +420,16 @@ public byte[] performRequest(String endpointUrl, List<NameValuePair> nameValuePa
return TestUtils.bytes("{}");
}

assertEquals(nameValuePairs.get(0).getName(), "data");
final String decoded = Base64Coder.decodeString(nameValuePairs.get(0).getValue());

try {
JSONObject jsonPairs = new JSONObject(nameValuePairs);
assertEquals(jsonPairs.keys().next(), "data");
final String decoded = Base64Coder.decodeString(jsonPairs.get("data").toString());
messages.put("SENT FLUSH " + endpointUrl);
messages.put(decoded);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (JSONException e) {
throw new RuntimeException(e);
}

return TestUtils.bytes("1\n");
Expand Down Expand Up @@ -916,10 +917,11 @@ public void test2XUrls() {
public void testAlias() {
final RemoteService mockPoster = new HttpService() {
@Override
public byte[] performRequest(String endpointUrl, List<NameValuePair> nameValuePairs, SSLSocketFactory socketFactory) {
public byte[] performRequest(String endpointUrl, Map<String, Object> nameValuePairs, SSLSocketFactory socketFactory) {
try {
assertEquals(nameValuePairs.get(0).getName(), "data");
final String jsonData = Base64Coder.decodeString(nameValuePairs.get(0).getValue());
JSONObject jsonPairs = new JSONObject(nameValuePairs);
assertEquals(jsonPairs.keys().next(), "data");
final String jsonData = Base64Coder.decodeString(jsonPairs.get("data").toString());
JSONArray msg = new JSONArray(jsonData);
JSONObject event = msg.getJSONObject(0);
JSONObject properties = event.getJSONObject("properties");
Expand Down
Expand Up @@ -9,10 +9,8 @@
import com.mixpanel.android.util.ImageStore;
import com.mixpanel.android.util.RemoteService;

import org.apache.http.NameValuePair;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.net.ssl.SSLSocketFactory;

Expand Down Expand Up @@ -79,7 +77,7 @@ public boolean isOnline(final Context context) {
}

@Override
public byte[] performRequest(final String endpointUrl, final List<NameValuePair> params, SSLSocketFactory socketFactory)
public byte[] performRequest(final String endpointUrl, final Map<String, Object> params, SSLSocketFactory socketFactory)
throws ServiceUnavailableException, IOException {
queries++;
return response;
Expand Down
Expand Up @@ -17,18 +17,14 @@
import com.mixpanel.android.util.RemoteService;
import com.mixpanel.android.util.HttpService;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.net.ssl.SSLSocketFactory;
Expand Down Expand Up @@ -409,10 +405,10 @@ private void sendData(MPDbAdapter dbAdapter, MPDbAdapter.Table table, String[] u
final String rawMessage = eventsData[1];

final String encodedData = Base64Coder.encodeString(rawMessage);
final List<NameValuePair> params = new ArrayList<NameValuePair>(1);
params.add(new BasicNameValuePair("data", encodedData));
final Map<String, Object> params = new HashMap<String, Object>();
params.put("data", encodedData);
if (MPConfig.DEBUG) {
params.add(new BasicNameValuePair("verbose", "1"));
params.put("verbose", "1");
}

boolean deleteEvents = true;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/mixpanel/android/mpmetrics/MPConfig.java
Expand Up @@ -88,7 +88,7 @@ public class MPConfig {
// Unfortunately, as long as we support building from source in Eclipse,
// we can't rely on BuildConfig.MIXPANEL_VERSION existing, so this must
// be hard-coded both in our gradle files and here in code.
public static final String VERSION = "4.7.0-RC1";
public static final String VERSION = "4.7.0";

public static boolean DEBUG = false;

Expand Down
21 changes: 11 additions & 10 deletions src/main/java/com/mixpanel/android/util/HttpService.java
Expand Up @@ -7,18 +7,18 @@

import com.mixpanel.android.mpmetrics.MPConfig;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.json.JSONObject;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
Expand Down Expand Up @@ -48,7 +48,7 @@ public boolean isOnline(Context context) {
}

@Override
public byte[] performRequest(String endpointUrl, List<NameValuePair> params, SSLSocketFactory socketFactory) throws ServiceUnavailableException, IOException {
public byte[] performRequest(String endpointUrl, Map<String, Object> params, SSLSocketFactory socketFactory) throws ServiceUnavailableException, IOException {
if (MPConfig.DEBUG) {
Log.v(LOGTAG, "Attempting request to " + endpointUrl);
}
Expand All @@ -63,7 +63,7 @@ public byte[] performRequest(String endpointUrl, List<NameValuePair> params, SSL
while (retries < 3 && !succeeded) {
InputStream in = null;
OutputStream out = null;
BufferedOutputStream bout = null;
BufferedWriter bout = null;
HttpURLConnection connection = null;

try {
Expand All @@ -77,12 +77,13 @@ public byte[] performRequest(String endpointUrl, List<NameValuePair> params, SSL
connection.setReadTimeout(10000);
if (null != params) {
connection.setDoOutput(true);
final UrlEncodedFormEntity form = new UrlEncodedFormEntity(params, "UTF-8");
JSONObject query = new JSONObject(params);
connection.setRequestMethod("POST");
connection.setFixedLengthStreamingMode((int)form.getContentLength());
connection.setFixedLengthStreamingMode(query.toString().getBytes("UTF-8").length);

This comment has been minimized.

Copy link
@yogurtearl

yogurtearl Oct 27, 2015

Contributor

This no longer does Form encoding... is the form encoding not necessary?

out = connection.getOutputStream();
bout = new BufferedOutputStream(out);
form.writeTo(bout);
bout = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
bout.write(query.toString());
bout.flush();
bout.close();
bout = null;
out.close();
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/mixpanel/android/util/RemoteService.java
Expand Up @@ -2,18 +2,17 @@

import android.content.Context;

import org.apache.http.NameValuePair;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.net.ssl.SSLSocketFactory;


public interface RemoteService {
boolean isOnline(Context context);

byte[] performRequest(String endpointUrl, List<NameValuePair> params, SSLSocketFactory socketFactory)
byte[] performRequest(String endpointUrl, Map<String, Object> params, SSLSocketFactory socketFactory)
throws ServiceUnavailableException, IOException;

class ServiceUnavailableException extends Exception {
Expand Down

0 comments on commit 948b37b

Please sign in to comment.