Skip to content

Commit

Permalink
Network plugin - Support formatting network request (#5652)
Browse files Browse the repository at this point in the history
Summary:
The network plugin allows for formatting network responses, making it possible to convert protobuf messages into a human-readable format. However, there is currently no method to format network request bodies. This PR introduces a NetworkRequestFormatter to address this issue.

## Changelog

Network plugin: Support formatting request body

Pull Request resolved: #5652

Test Plan:
Here we intercepted the network call and modified the request and response bodies

<img width="1280" alt="Screenshot 2024-07-11 at 23 13 58" src="https://github.com/user-attachments/assets/5bd20884-0ffc-48cf-867e-c74c4b6735df">

Reviewed By: antonk52

Differential Revision: D59750064

Pulled By: passy

fbshipit-source-id: 787227184d5609e9fdb3f07962b7cff0e972726f
  • Loading branch information
IshmaelT authored and facebook-github-bot committed Jul 15, 2024
1 parent 9ad0a15 commit ea91782
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@
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 +49,8 @@ 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 +65,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,18 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

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);
}

0 comments on commit ea91782

Please sign in to comment.