From ea917828ae653075c43c5b54b99f8f3ed59fd8fe Mon Sep 17 00:00:00 2001 From: Ishmael Tshikhovhokhovho Date: Mon, 15 Jul 2024 09:24:15 -0700 Subject: [PATCH] Network plugin - Support formatting network request (#5652) 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: https://github.com/facebook/flipper/pull/5652 Test Plan: Here we intercepted the network call and modified the request and response bodies Screenshot 2024-07-11 at 23 13 58 Reviewed By: antonk52 Differential Revision: D59750064 Pulled By: passy fbshipit-source-id: 787227184d5609e9fdb3f07962b7cff0e972726f --- .../plugins/network/NetworkFlipperPlugin.java | 34 +++++++++++++++++-- .../network/NetworkRequestFormatter.java | 18 ++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/NetworkRequestFormatter.java diff --git a/android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/NetworkFlipperPlugin.java b/android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/NetworkFlipperPlugin.java index 4c9a1bf2d18..f3499d46d3b 100644 --- a/android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/NetworkFlipperPlugin.java +++ b/android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/NetworkFlipperPlugin.java @@ -19,7 +19,9 @@ 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 mFormatters; + private final List mRequestFormatters; public NetworkFlipperPlugin() { this(null); @@ -27,6 +29,13 @@ public NetworkFlipperPlugin() { public NetworkFlipperPlugin(List formatters) { this.mFormatters = formatters; + this.mRequestFormatters = null; + } + + public NetworkFlipperPlugin( + List formatters, List requestFormatters) { + this.mFormatters = formatters; + this.mRequestFormatters = requestFormatters; } @Override @@ -40,7 +49,8 @@ public void setFormatters(List 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 = @@ -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 diff --git a/android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/NetworkRequestFormatter.java b/android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/NetworkRequestFormatter.java new file mode 100644 index 00000000000..9ae35ab19a3 --- /dev/null +++ b/android/plugins/network/src/main/java/com/facebook/flipper/plugins/network/NetworkRequestFormatter.java @@ -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); +}