Intercept Drive network requests

pull/820/head
Alex Baker 7 years ago
parent 7cc971e208
commit 44317d58da

@ -5,6 +5,7 @@ import com.facebook.flipper.android.AndroidFlipperClient;
import com.facebook.flipper.plugins.network.FlipperOkhttpInterceptor;
import com.facebook.flipper.plugins.network.NetworkFlipperPlugin;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponse;
import java.io.IOException;
import javax.inject.Inject;
import okhttp3.OkHttpClient;
@ -36,4 +37,12 @@ public class DebugNetworkInterceptor {
.execute();
return interceptor.getResponse();
}
public <T> T report(HttpResponse httpResponse, Class<T> responseClass, long start, long finish)
throws IOException {
FlipperHttpInterceptor<T> interceptor =
new FlipperHttpInterceptor<>(getNetworkPlugin(context), responseClass);
interceptor.report(httpResponse, start, finish);
return interceptor.getResponse();
}
}

@ -35,20 +35,37 @@ public class FlipperHttpInterceptor<T> implements HttpExecuteInterceptor, HttpRe
@Override
public void intercept(HttpRequest request) {
plugin.reportRequest(toRequestInfo(request, now()));
}
@Override
public void interceptResponse(HttpResponse response) throws IOException {
plugin.reportResponse(toResponseInfo(response, now()));
}
public void report(HttpResponse response, long start, long end) throws IOException {
plugin.reportRequest(toRequestInfo(response.getRequest(), start));
plugin.reportResponse(toResponseInfo(response, end));
}
public T getResponse() {
return body;
}
private RequestInfo toRequestInfo(HttpRequest request, long timestamp) {
RequestInfo requestInfo = new RequestInfo();
requestInfo.method = request.getRequestMethod();
requestInfo.body = bodyToByteArray(request.getContent());
requestInfo.headers = getHeaders(request.getHeaders());
requestInfo.requestId = requestId;
requestInfo.timeStamp = now();
requestInfo.timeStamp = timestamp;
requestInfo.uri = request.getUrl().toString();
plugin.reportRequest(requestInfo);
return requestInfo;
}
@Override
public void interceptResponse(HttpResponse response) throws IOException {
private ResponseInfo toResponseInfo(HttpResponse response, long timestamp) throws IOException {
ResponseInfo responseInfo = new ResponseInfo();
responseInfo.timeStamp = now();
responseInfo.timeStamp = timestamp;
responseInfo.headers = getHeaders(response.getHeaders());
responseInfo.requestId = requestId;
responseInfo.statusCode = response.getStatusCode();
@ -61,11 +78,7 @@ public class FlipperHttpInterceptor<T> implements HttpExecuteInterceptor, HttpRe
Timber.e(e);
}
}
plugin.reportResponse(responseInfo);
}
public T getResponse() {
return body;
return responseInfo;
}
private List<Header> getHeaders(HttpHeaders headers) {

@ -1,10 +1,13 @@
package org.tasks.drive;
import static com.todoroo.andlib.utility.DateUtilities.now;
import android.accounts.AccountManager;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.javanet.NetHttpTransport;
@ -20,6 +23,7 @@ import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
import org.tasks.BuildConfig;
import org.tasks.DebugNetworkInterceptor;
import org.tasks.R;
import org.tasks.files.FileHelper;
import org.tasks.gtasks.GoogleAccountManager;
@ -34,6 +38,7 @@ public class DriveInvoker {
private final Context context;
private final Preferences preferences;
private final GoogleAccountManager googleAccountManager;
private final DebugNetworkInterceptor interceptor;
private final Drive service;
private final GoogleCredential credential = new GoogleCredential();
@ -41,10 +46,12 @@ public class DriveInvoker {
public DriveInvoker(
@ForApplication Context context,
Preferences preferences,
GoogleAccountManager googleAccountManager) {
GoogleAccountManager googleAccountManager,
DebugNetworkInterceptor interceptor) {
this.context = context;
this.preferences = preferences;
this.googleAccountManager = googleAccountManager;
this.interceptor = interceptor;
service =
new Drive.Builder(new NetHttpTransport(), new JacksonFactory(), credential)
.setApplicationName(String.format("Tasks/%s", BuildConfig.VERSION_NAME))
@ -109,7 +116,13 @@ public class DriveInvoker {
Timber.d("%s request: %s", getCaller(), request);
T response;
try {
response = request.execute();
if (preferences.isFlipperEnabled()) {
long start = now();
HttpResponse httpResponse = request.executeUnparsed();
response = interceptor.report(httpResponse, request.getResponseClass(), start, now());
} else {
response = request.execute();
}
} catch (HttpResponseException e) {
if (e.getStatusCode() == 401 && !retry) {
googleAccountManager.invalidateToken(credential.getAccessToken());

Loading…
Cancel
Save