Skip to content

Commit

Permalink
Merge pull request #41 from flipkart-incubator/v2
Browse files Browse the repository at this point in the history
Method Removal + Support for Response Body in RequestStats
  • Loading branch information
yasirmhd authored Nov 26, 2016
2 parents 5c44117 + 358280a commit 022f0e9
Show file tree
Hide file tree
Showing 28 changed files with 208 additions and 316 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Add it in your root build.gradle at the end of repositories:
Add the dependency:

dependencies {
compile 'com.github.flipkart-incubator:okhttp-stats:1.0'
compile 'com.github.flipkart-incubator:okhttp-stats:1.1.0'
}


Expand All @@ -32,27 +32,27 @@ private class OnResponseReceived implements OnResponseListener {
@Override
public void onResponseSuccess(NetworkInfo info, RequestStats requestStats) {
Log.d(MainActivity.class.getName(), "onResponseSuccessReceived : "
+ "\nId : " + requestStats.getId()
+ "\nUrl : " + requestStats.getUrl()
+ "\nMethod : " + requestStats.getMethodType()
+ "\nHost : " + requestStats.getHostName()
+ "\nRequest Size : " + requestStats.getRequestSize()
+ "\nResponse Size : " + requestStats.getResponseSize()
+ "\nTime Taken: " + (requestStats.getEndTime() - requestStats.getStartTime())
+ "\nStatus Code : " + requestStats.getStatusCode());
+ "\nId : " + requestStats.id
+ "\nUrl : " + requestStats.url
+ "\nMethod : " + requestStats.methodType
+ "\nHost : " + requestStats.hostName
+ "\nRequest Size : " + requestStats.requestSize
+ "\nResponse Size : " + requestStats.responseSize
+ "\nTime Taken: " + (requestStats.endTime - requestStats.startTime)
+ "\nStatus Code : " + requestStats.statusCode);
}

@Override
public void onResponseError(NetworkInfo info, RequestStats requestStats, Exception e) {
Log.d(MainActivity.class.getName(), "onResponseErrorReceived : "
+ "\nId : " + requestStats.getId()
+ "\nUrl : " + requestStats.getUrl()
+ "\nMethod : " + requestStats.getMethodType()
+ "\nHost : " + requestStats.getHostName()
+ "\nRequest Size : " + requestStats.getRequestSize()
+ "\nResponse Size : " + requestStats.getResponseSize()
+ "\nTime Taken: " + (requestStats.getEndTime() - requestStats.getStartTime())
+ "\nStatus Code : " + requestStats.getStatusCode()
+ "\nId : " + requestStats.id
+ "\nUrl : " + requestStats.url
+ "\nMethod : " + requestStats.methodType
+ "\nHost : " + requestStats.hostName
+ "\nRequest Size : " + requestStats.requestSize
+ "\nResponse Size : " + requestStats.responseSize
+ "\nTime Taken: " + (requestStats.endTime - requestStats.startTime)
+ "\nStatus Code : " + requestStats.statusCode
+ "\nException : " + e.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ public class OnResponseReceived implements OnResponseListener {
@Override
public void onResponseSuccess(NetworkInfo info, RequestStats requestStats) {
Log.d(MainActivity.class.getName(), "onResponseSuccessReceived : "
+ "\nId : " + requestStats.getId()
+ "\nUrl : " + requestStats.getUrl()
+ "\nMethod : " + requestStats.getMethodType()
+ "\nHost : " + requestStats.getHostName()
+ "\nRequest Size : " + requestStats.getRequestSize()
+ "\nResponse Size : " + requestStats.getResponseSize()
+ "\nTime Taken: " + (requestStats.getEndTime() - requestStats.getStartTime())
+ "\nStatus Code : " + requestStats.getStatusCode());
+ "\nId : " + requestStats.id
+ "\nUrl : " + requestStats.url
+ "\nMethod : " + requestStats.methodType
+ "\nHost : " + requestStats.hostName
+ "\nRequest Size : " + requestStats.requestSize
+ "\nResponse Size : " + requestStats.responseSize
+ "\nTime Taken: " + (requestStats.endTime - requestStats.startTime)
+ "\nStatus Code : " + requestStats.statusCode);
}

@Override
public void onResponseError(NetworkInfo info, RequestStats requestStats, Exception e) {
Log.d(MainActivity.class.getName(), "onResponseErrorReceived : "
+ "\nId : " + requestStats.getId()
+ "\nUrl : " + requestStats.getUrl()
+ "\nMethod : " + requestStats.getMethodType()
+ "\nHost : " + requestStats.getHostName()
+ "\nRequest Size : " + requestStats.getRequestSize()
+ "\nResponse Size : " + requestStats.getResponseSize()
+ "\nTime Taken: " + (requestStats.getEndTime() - requestStats.getStartTime())
+ "\nStatus Code : " + requestStats.getStatusCode()
+ "\nId : " + requestStats.id
+ "\nUrl : " + requestStats.url
+ "\nMethod : " + requestStats.methodType
+ "\nHost : " + requestStats.hostName
+ "\nRequest Size : " + requestStats.requestSize
+ "\nResponse Size : " + requestStats.responseSize
+ "\nTime Taken: " + (requestStats.endTime - requestStats.startTime)
+ "\nStatus Code : " + requestStats.statusCode
+ "\nException : " + e.getMessage());
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.android.tools.build:gradle:2.2.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Sat Nov 26 09:48:40 IST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
14 changes: 7 additions & 7 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:2.2.3'
}
}

apply plugin: 'com.android.library'
apply plugin: 'android-maven'
apply plugin: 'com.github.dcendents.android-maven'

android {
compileSdkVersion 24
Expand All @@ -60,11 +60,11 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-annotations:24.2.0'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.android.support:support-annotations:25.0.1'
compile 'com.squareup.okhttp3:okhttp:3.4.2'

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'com.squareup.okhttp3:mockwebserver:3.2.0'
testCompile 'org.robolectric:robolectric:3.1'
testCompile 'com.squareup.okhttp3:mockwebserver:3.4.2'
testCompile 'org.robolectric:robolectric:3.1.4'
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private NetworkInterceptor(Builder builder) {
throw new IllegalStateException("NetworkInterpreter cannot be null");
}
mInterpreter = builder.mInterpreter;
Utils.setIsLoggingEnabled(builder.mIsLoggingEnabled);
Utils.isLoggingEnabled = builder.mIsLoggingEnabled;
}

/**
Expand Down Expand Up @@ -137,4 +137,4 @@ public NetworkInterceptor build() {
return new NetworkInterceptor(this);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ForwardingResponse(OnStatusCodeAwareResponseListener onStatusCodeAwareRes
@Override
public void onResponseSuccess(NetworkInfo info, RequestStats requestStats) {
if (requestStats != null) {
int statusCode = requestStats.getStatusCode();
int statusCode = requestStats.statusCode;
if ((statusCode >= HttpStatusCode.HTTP_2XX_START && statusCode <= HttpStatusCode.HTTP_2XX_END) ||
(statusCode >= HttpStatusCode.HTTP_3XX_START && statusCode <= HttpStatusCode.HTTP_3XX_END)) {
mOnStatusCodeAwareResponseListener.onResponseServerSuccess(info, requestStats);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public PersistentStatsHandler(Context context) {
}

@VisibleForTesting
public PersistentStatsHandler(Context context, PreferenceManager preferenceManager) {
PersistentStatsHandler(Context context, PreferenceManager preferenceManager) {
this.mPreferenceManager = preferenceManager;
this.MAX_SIZE = DEFAULT_MAX_SIZE;
this.mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Expand All @@ -86,7 +86,7 @@ public PersistentStatsHandler(Context context, PreferenceManager preferenceManag
}

@VisibleForTesting
public Set<OnResponseListener> getOnResponseListeners() {
Set<OnResponseListener> getOnResponseListeners() {
return mOnResponseListeners;
}

Expand Down Expand Up @@ -144,7 +144,7 @@ public float getAverageNetworkSpeed() {

@Override
public void onResponseReceived(final RequestStats requestStats) {
if (Utils.isLoggingEnabled()) {
if (Utils.isLoggingEnabled) {
Log.d("Response Received : ", requestStats + " ");
}

Expand Down Expand Up @@ -176,7 +176,7 @@ private float calculateNewSpeed(float currentAvgSpeed) {

@Override
public void onHttpExchangeError(RequestStats requestStats, IOException e) {
if (Utils.isLoggingEnabled()) {
if (Utils.isLoggingEnabled) {
Log.d("Response Http Error :", requestStats + "");
}

Expand All @@ -189,7 +189,7 @@ public void onHttpExchangeError(RequestStats requestStats, IOException e) {

@Override
public void onResponseInputStreamError(RequestStats requestStats, Exception e) {
if (Utils.isLoggingEnabled()) {
if (Utils.isLoggingEnabled) {
Log.d("Response InputStream : ", requestStats + "");
}

Expand All @@ -206,7 +206,7 @@ public void onResponseInputStreamError(RequestStats requestStats, Exception e) {
* @param currentAvgSpeed : float
*/
private void saveToSharedPreference(float currentAvgSpeed) {
if (Utils.isLoggingEnabled()) {
if (Utils.isLoggingEnabled) {
Log.d("avg speed", "saveToSharedPreference: " + mNetworkStat.getCurrentAvgSpeed());
}
String networkKey = getNetworkKey(getActiveNetworkInfo());
Expand All @@ -220,7 +220,7 @@ private void saveToSharedPreference(float currentAvgSpeed) {
* @return string
*/
@VisibleForTesting
public String getNetworkKey(NetworkInfo networkInfo) {
String getNetworkKey(NetworkInfo networkInfo) {
if (networkInfo != null && networkInfo.getTypeName() != null) {
if (networkInfo.getTypeName().equals(WIFI_NETWORK)) {
return WIFI_NETWORK + "_" + getWifiSSID();
Expand All @@ -233,7 +233,7 @@ public String getNetworkKey(NetworkInfo networkInfo) {
}

@VisibleForTesting
public int getWifiSSID() {
int getWifiSSID() {
WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
if (wifiInfo != null) {
String ssid = wifiInfo.getSSID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

package com.flipkart.okhttpstats.interpreter;

import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.util.Log;

Expand Down Expand Up @@ -57,18 +58,18 @@ public DefaultInterpreter(NetworkEventReporter mEventReporter) {

@Override
public Response interpretResponseStream(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, Response response) throws IOException {
final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request), request.header(HOST_NAME));
final OkHttpInspectorResponse okHttpInspectorResponse = new OkHttpInspectorResponse(requestId, response.code(), Utils.contentLength(response), timeInfo.mStartTime, timeInfo.mEndTime);
ResponseBody responseBody = response.body();

final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request), request.header(HOST_NAME));
final OkHttpInspectorResponse okHttpInspectorResponse = new OkHttpInspectorResponse(requestId, response.code(), Utils.contentLength(response), timeInfo.mStartTime, timeInfo.mEndTime, responseBody);
//if response does not have content length, using CountingInputStream to read its bytes
if (response.header(CONTENT_LENGTH) == null) {
final ResponseBody body = response.body();
InputStream responseStream = null;
if (body != null) {
if (responseBody != null) {
try {
responseStream = body.byteStream();
responseStream = responseBody.byteStream();
} catch (Exception e) {
if (Utils.isLoggingEnabled()) {
if (Utils.isLoggingEnabled) {
Log.d("Error reading IS : ", e.getMessage());
}

Expand All @@ -88,7 +89,7 @@ public void onEOF(long bytesRead) {
}));

//creating response object using the interpreted stream
response = response.newBuilder().body(new ForwardingResponseBody(body, responseStream)).build();
response = response.newBuilder().body(new ForwardingResponseBody(responseBody, responseStream)).build();
} else {
//if response has content length, notify the event reporter that response has been received.
mEventReporter.responseReceived(okHttpInspectorRequest, okHttpInspectorResponse);
Expand All @@ -98,7 +99,7 @@ public void onEOF(long bytesRead) {

@Override
public void interpretError(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, IOException e) {
if (Utils.isLoggingEnabled()) {
if (Utils.isLoggingEnabled) {
Log.d("Error response: ", e.getMessage());
}
final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request), request.header(HOST_NAME));
Expand All @@ -108,15 +109,14 @@ public void interpretError(int requestId, NetworkInterceptor.TimeInfo timeInfo,
/**
* Implementation of {@link NetworkEventReporter.InspectorRequest}
*/
@VisibleForTesting
public static class OkHttpInspectorRequest implements NetworkEventReporter.InspectorRequest {
private final int mRequestId;
private final URL mRequestUrl;
private final String mMethodType;
private final long mContentLength;
private final String mHostName;

public OkHttpInspectorRequest(int requestId, URL requestUrl, String methodType, long contentLength, String hostName) {
static class OkHttpInspectorRequest implements NetworkEventReporter.InspectorRequest {
final int mRequestId;
final URL mRequestUrl;
final String mMethodType;
final long mContentLength;
final String mHostName;

OkHttpInspectorRequest(int requestId, URL requestUrl, String methodType, long contentLength, String hostName) {
this.mRequestId = requestId;
this.mRequestUrl = requestUrl;
this.mMethodType = methodType;
Expand Down Expand Up @@ -153,20 +153,22 @@ public String hostName() {
/**
* Implementation of {@link NetworkEventReporter.InspectorResponse}
*/
@VisibleForTesting
public static class OkHttpInspectorResponse implements NetworkEventReporter.InspectorResponse {
private int mRequestId;
private long mStartTime;
private long mEndTime;
private int mStatusCode;
private long mResponseSize;

public OkHttpInspectorResponse(int requestId, int statusCode, long responseSize, long startTime, long endTime) {
static class OkHttpInspectorResponse implements NetworkEventReporter.InspectorResponse {
int mRequestId;
long mStartTime;
long mEndTime;
int mStatusCode;
long mResponseSize;
@Nullable
ResponseBody responseBody;

OkHttpInspectorResponse(int requestId, int statusCode, long responseSize, long startTime, long endTime, @Nullable ResponseBody responseBody) {
this.mRequestId = requestId;
this.mStatusCode = statusCode;
this.mResponseSize = responseSize;
this.mStartTime = startTime;
this.mEndTime = endTime;
this.responseBody = responseBody;
}

@Override
Expand All @@ -193,18 +195,24 @@ public long startTime() {
public long endTime() {
return mEndTime;
}

@Override
@Nullable
public ResponseBody responseBody() {
return responseBody;
}
}

/**
* Wrapper for {@link ResponseBody}
* Will only be used in case the response does not have the content-length
*/
@VisibleForTesting
public static class ForwardingResponseBody extends ResponseBody {
private final ResponseBody mBody;
private final BufferedSource mInterceptedSource;
static class ForwardingResponseBody extends ResponseBody {
final ResponseBody mBody;
final BufferedSource mInterceptedSource;

public ForwardingResponseBody(ResponseBody body, InputStream interceptedStream) {
ForwardingResponseBody(ResponseBody body, InputStream interceptedStream) {
mBody = body;
mInterceptedSource = Okio.buffer(Okio.source(interceptedStream));
}
Expand All @@ -224,4 +232,4 @@ public BufferedSource source() {
return mInterceptedSource;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ public interface NetworkInterpreter {
* @param e e
*/
void interpretError(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, IOException e);
}
}
Loading

0 comments on commit 022f0e9

Please sign in to comment.