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

Network plugin - Support formatting network request #5652

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,36 @@
package com.facebook.flipper.plugins.network;

import android.util.Base64;

import com.facebook.flipper.core.ErrorReportingRunnable;
import com.facebook.flipper.core.FlipperArray;
import com.facebook.flipper.core.FlipperObject;
import com.facebook.flipper.plugins.common.BufferingFlipperPlugin;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Nullable;

public class NetworkFlipperPlugin extends BufferingFlipperPlugin implements NetworkReporter {
public static final String ID = "Network";
private static final int MAX_BODY_SIZE_IN_BYTES = 1024 * 1024;

private List<NetworkResponseFormatter> mFormatters;
private final List<NetworkRequestFormatter> mRequestFormatters;

public NetworkFlipperPlugin() {
this(null);
}

public NetworkFlipperPlugin(List<NetworkResponseFormatter> formatters) {
this.mFormatters = formatters;
this.mRequestFormatters = null;
}

public NetworkFlipperPlugin(List<NetworkResponseFormatter> formatters, List<NetworkRequestFormatter> requestFormatters) {
this.mFormatters = formatters;
this.mRequestFormatters = requestFormatters;
}

@Override
Expand All @@ -40,7 +51,7 @@ public void setFormatters(List<NetworkResponseFormatter> formatters) {

@Override
public void reportRequest(final RequestInfo requestInfo) {
(new ErrorReportingRunnable(getConnection()) {
final Runnable job = new ErrorReportingRunnable(getConnection()) {
@Override
protected void runOrThrow() throws Exception {
final FlipperObject request =
Expand All @@ -55,8 +66,26 @@ protected void runOrThrow() throws Exception {

send("newRequest", request);
}
})
.run();
};

if (mRequestFormatters != null) {
for (NetworkRequestFormatter formatter : mRequestFormatters) {
if (formatter.shouldFormat(requestInfo)) {
formatter.format(
requestInfo,
new NetworkRequestFormatter.OnCompletionListener() {
@Override
public void onCompletion(final String json) {
requestInfo.body = json.getBytes();
job.run();
}
});
return;
}
}
}

job.run();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.facebook.flipper.plugins.network;

public interface NetworkRequestFormatter {
interface OnCompletionListener {
void onCompletion(String json);
}

boolean shouldFormat(NetworkReporter.RequestInfo request);

void format(NetworkReporter.RequestInfo request, OnCompletionListener onCompletionListener);
}
Loading