Skip to content

Commit

Permalink
Merge pull request #47 from flipkart-incubator/includeRequestBodyInStats
Browse files Browse the repository at this point in the history
Include requestBody in requestStats
  • Loading branch information
anirudhramanan authored Aug 24, 2018
2 parents c9ec583 + f148dea commit b62e8b0
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 26 deletions.
19 changes: 10 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ allprojects {
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion 27
buildToolsVersion "27.0.3"
lintOptions {
abortOnError false
}
defaultConfig {
minSdkVersion 14
targetSdkVersion 25
targetSdkVersion 27
versionCode 1
versionName "1.0"
useLibrary 'org.apache.http.legacy'
Expand All @@ -31,10 +31,11 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.android.volley:volley:1.0.0'
compile project(':library')
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation project(':library')
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public void onResponseSuccess(NetworkInfo info, RequestStats requestStats) {
+ "\nId : " + requestStats.id
+ "\nUrl : " + requestStats.url
+ "\nMethod : " + requestStats.methodType
+ "\nStatus Code : " + requestStats.statusCode
+ "\nHost : " + requestStats.hostName
+ "\nRequest Size : " + requestStats.requestSize
+ "\nResponse Size : " + requestStats.responseSize
Expand All @@ -27,6 +28,8 @@ public void onResponseError(NetworkInfo info, RequestStats requestStats, Excepti
+ "\nId : " + requestStats.id
+ "\nUrl : " + requestStats.url
+ "\nMethod : " + requestStats.methodType
+ "\nStatus Code : " + requestStats.statusCode
+ "\nRequest Body : " + requestStats.requestBody
+ "\nHost : " + requestStats.hostName
+ "\nRequest Size : " + requestStats.requestSize
+ "\nResponse Size : " + requestStats.responseSize
Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:3.1.4'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -15,6 +16,7 @@ buildscript {
allprojects {
repositories {
jcenter()
google()
}
}

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 @@
#Sat Nov 26 09:48:40 IST 2016
#Fri Aug 24 12:18:43 IST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
23 changes: 12 additions & 11 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
buildscript {
repositories {
jcenter()
google()
maven { url 'http://repo1.maven.org/maven2' }
maven { url 'https://plugins.gradle.org/m2/' }
}

dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:2.2.3'
}
Expand All @@ -39,14 +40,14 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
compileSdkVersion 27
buildToolsVersion '27.0.3'
lintOptions {
abortOnError false
}
defaultConfig {
minSdkVersion 14
targetSdkVersion 25
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
Expand All @@ -59,12 +60,12 @@ android {
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-annotations:25.1.0'
compile 'com.squareup.okhttp3:okhttp:3.4.2'
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:support-annotations:27.1.1'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'com.squareup.okhttp3:mockwebserver:3.4.2'
testCompile 'org.robolectric:robolectric:3.2.2'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.18.0'
testImplementation 'com.squareup.okhttp3:mockwebserver:3.6.0'
testImplementation 'org.robolectric:robolectric:3.3.2'
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.BufferedSource;
Expand All @@ -60,7 +61,7 @@ public DefaultInterpreter(NetworkEventReporter mEventReporter) {
public Response interpretResponseStream(int requestId, NetworkInterceptor.TimeInfo timeInfo, Request request, Response response) throws IOException {
ResponseBody responseBody = response.body();

final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request.headers()), request.url().host());
final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request.headers()), request.url().host(), request.body());
final OkHttpInspectorResponse okHttpInspectorResponse = new OkHttpInspectorResponse(requestId, response.code(), Utils.contentLength(response.headers()), timeInfo.mStartTime, timeInfo.mEndTime, responseBody);
//if response does not have content length, using CountingInputStream to read its bytes
if (response.header(CONTENT_LENGTH) == null) {
Expand Down Expand Up @@ -103,7 +104,7 @@ public void interpretError(int requestId, NetworkInterceptor.TimeInfo timeInfo,
if (Utils.isLoggingEnabled) {
Log.d("Error response: ", e.getMessage());
}
final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request.headers()), request.header(HOST_NAME));
final OkHttpInspectorRequest okHttpInspectorRequest = new OkHttpInspectorRequest(requestId, request.url().url(), request.method(), Utils.contentLength(request.headers()), request.header(HOST_NAME), request.body());
mEventReporter.httpExchangeError(okHttpInspectorRequest, e);
}

Expand All @@ -116,13 +117,16 @@ static class OkHttpInspectorRequest implements NetworkEventReporter.InspectorReq
final String mMethodType;
final long mContentLength;
final String mHostName;
final RequestBody mRequestBody;

OkHttpInspectorRequest(int requestId, URL requestUrl, String methodType, long contentLength, String hostName) {
OkHttpInspectorRequest(int requestId, URL requestUrl, String methodType, long contentLength,
String hostName, RequestBody requestBody) {
this.mRequestId = requestId;
this.mRequestUrl = requestUrl;
this.mMethodType = methodType;
this.mContentLength = contentLength;
this.mHostName = hostName;
this.mRequestBody = requestBody;
}

@Override
Expand All @@ -149,6 +153,9 @@ public long requestSize() {
public String hostName() {
return mHostName;
}

@Override
public RequestBody requestBody() { return mRequestBody; }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.net.URL;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
Expand All @@ -37,6 +38,7 @@ public class RequestStats {
public String methodType;
public long requestSize;
public long responseSize;
public RequestBody requestBody;
public ResponseBody responseBody;
public String hostName;
public int statusCode;
Expand All @@ -46,4 +48,19 @@ public class RequestStats {
public RequestStats(int requestId) {
this.id = requestId;
}

public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Id : ").append(id)
.append("\nMethod : ").append(methodType)
.append("\nHost : ").append(hostName)
.append("\nStatusCode : ").append(statusCode)
.append("\nRequest Size : ").append(requestSize)
.append("\nResponse Size : ").append(responseSize)
.append("\nTime Taken : ").append(endTime - startTime)
.append("\nUrl : ").append(url)
.append("\nRequest Body : ").append(requestBody)
.append("\nResponse Body : ").append(responseBody);
return stringBuilder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.IOException;
import java.net.URL;

import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

Expand Down Expand Up @@ -70,6 +71,9 @@ interface InspectorRequest {
long requestSize();

String hostName();

@Nullable
RequestBody requestBody();
}

interface InspectorResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void responseReceived(final InspectorRequest inspectorRequest, final Insp
requestStats.url = inspectorRequest.url();
requestStats.methodType = inspectorRequest.method();
requestStats.hostName = inspectorRequest.hostName();
requestStats.requestBody = inspectorRequest.requestBody();
requestStats.responseSize = inspectorResponse.responseSize();
requestStats.statusCode = inspectorResponse.statusCode();
requestStats.startTime = inspectorResponse.startTime();
Expand All @@ -77,6 +78,7 @@ public void httpExchangeError(final InspectorRequest inspectorRequest, final IOE
requestStats.methodType = inspectorRequest.method();
requestStats.hostName = inspectorRequest.hostName();
requestStats.requestSize = inspectorRequest.requestSize();
requestStats.requestBody = inspectorRequest.requestBody();
mNetworkRequestStatsHandler.onHttpExchangeError(requestStats, e);
}
}
Expand All @@ -90,6 +92,7 @@ public void responseInputStreamError(final InspectorRequest inspectorRequest, fi
requestStats.url = inspectorRequest.url();
requestStats.methodType = inspectorRequest.method();
requestStats.hostName = inspectorRequest.hostName();
requestStats.requestBody = inspectorRequest.requestBody();
requestStats.statusCode = inspectorResponse.statusCode();
requestStats.startTime = inspectorResponse.startTime();
requestStats.endTime = inspectorResponse.endTime();
Expand Down

0 comments on commit b62e8b0

Please sign in to comment.