Created
April 4, 2019 07:34
-
-
Save pedrovgs/11a17805f51ccf2ac5a1415d0a6872d1 to your computer and use it in GitHub Desktop.
OkHttp version 1 client interceptor for Flipper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.facebook.flipper.plugins.network.NetworkFlipperPlugin; | |
import com.facebook.flipper.plugins.network.NetworkReporter; | |
import com.squareup.okhttp.*; | |
import okio.Buffer; | |
import okio.BufferedSource; | |
import javax.annotation.Nullable; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.UUID; | |
public class FlipperInterceptor implements Interceptor { | |
public @Nullable | |
NetworkFlipperPlugin plugin; | |
public FlipperInterceptor() { | |
this.plugin = null; | |
} | |
public FlipperInterceptor(NetworkFlipperPlugin plugin) { | |
this.plugin = plugin; | |
} | |
@Override | |
public Response intercept(Interceptor.Chain chain) throws IOException { | |
Request request = chain.request(); | |
String identifier = UUID.randomUUID().toString(); | |
plugin.reportRequest(convertRequest(request, identifier)); | |
Response response = chain.proceed(request); | |
ResponseBody body = response.body(); | |
NetworkReporter.ResponseInfo responseInfo = convertResponse(response, body, identifier); | |
plugin.reportResponse(responseInfo); | |
return response; | |
} | |
private static byte[] bodyToByteArray(final Request request) throws IOException { | |
final Buffer buffer = new Buffer(); | |
request.body().writeTo(buffer); | |
return buffer.readByteArray(); | |
} | |
private NetworkReporter.RequestInfo convertRequest(Request request, String identifier) throws IOException { | |
List<NetworkReporter.Header> headers = convertHeader(request.headers()); | |
NetworkReporter.RequestInfo info = new NetworkReporter.RequestInfo(); | |
info.requestId = identifier; | |
info.timeStamp = System.currentTimeMillis(); | |
info.headers = headers; | |
info.method = request.method(); | |
info.uri = request.url().toString(); | |
if (request.body() != null) { | |
info.body = bodyToByteArray(request); | |
} | |
return info; | |
} | |
private NetworkReporter.ResponseInfo convertResponse(Response response, ResponseBody body, String identifier) | |
throws IOException { | |
List<NetworkReporter.Header> headers = convertHeader(response.headers()); | |
NetworkReporter.ResponseInfo info = new NetworkReporter.ResponseInfo(); | |
info.requestId = identifier; | |
info.timeStamp = System.currentTimeMillis(); | |
info.statusCode = response.code(); | |
info.headers = headers; | |
BufferedSource source = body.source(); | |
source.request(Long.MAX_VALUE); | |
Buffer buffer = source.buffer().clone(); | |
info.body = buffer.readByteArray(); | |
return info; | |
} | |
private List<NetworkReporter.Header> convertHeader(Headers headers) { | |
List<NetworkReporter.Header> list = new ArrayList<>(); | |
Set<String> keys = headers.names(); | |
for (String key : keys) { | |
list.add(new NetworkReporter.Header(key, headers.get(key))); | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment