Skip to content

Commit bf97dc7

Browse files
authored
[refactor] update DebugOverlayNetworkInterceptor (#75)
- make DebugOverlayNetworkInterceptor more robust - captures headers and content for the detail screen which will be added
1 parent 313ed8a commit bf97dc7

File tree

4 files changed

+521
-63
lines changed

4 files changed

+521
-63
lines changed

debugoverlay-core/src/main/kotlin/com/ms/square/debugoverlay/internal/ui/NetworkTabContent.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ private fun NetworkStats.augmentNetworkStatsWith(requests: List<NetworkRequest>)
384384

385385
if (this == NetworkStats.UNSUPPORTED) {
386386
// since it might not have all the requests due to its size capping, this is just approximate.
387-
val totalDownloaded = requests.filter { it.responseSize >= 0 }.sumOf { it.responseSize }
388-
val totalUploaded = requests.filter { it.requestSize >= 0 }.sumOf { it.requestSize }
387+
val totalDownloaded = requests.mapNotNull { it.responseSize }.filter { it >= 0 }.sumOf { it }
388+
val totalUploaded = requests.mapNotNull { it.requestSize }.filter { it >= 0 }.sumOf { it }
389389
return NetworkStats(
390390
totalDownloaded = totalDownloaded,
391391
totalUploaded = totalUploaded,
@@ -409,8 +409,8 @@ private const val BYTES_PER_GB = 1024L * 1024L * 1024L
409409
/**
410410
* Format bytes to human-readable string.
411411
*/
412-
private fun formatBytes(bytes: Long): String = when {
413-
bytes < 0 -> ""
412+
private fun formatBytes(bytes: Long?): String = when {
413+
bytes == null || bytes < 0 -> ""
414414
bytes < BYTES_PER_KB -> "$bytes B"
415415
bytes < BYTES_PER_MB -> {
416416
val kb = bytes / BYTES_PER_KB.toDouble()

debugoverlay-core/src/main/kotlin/com/ms/square/debugoverlay/model/NetworkRequest.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@ import java.util.UUID
66
* Data class for network request.
77
*/
88
public data class NetworkRequest(
9+
// Base NetworkRequest fields
910
val id: String = UUID.randomUUID().toString(),
11+
val protocol: String?, // http/1.1, h2, quic..etc
1012
val method: HttpMethod, // GET, POST, etc.
1113
val fullUrl: String, // https://test.com/api/v1/feed
1214
val shortUrl: String, // /api/v1/feed
1315
val statusCode: Int?, // 200, 404, etc.
1416
val durationMs: Long, // 245
15-
val responseSize: Long, // bytes
16-
val requestSize: Long = 0, // bytes
17+
val responseSize: Long?, // bytes
18+
val requestSize: Long?, // bytes
1719
val timestamp: Long = System.currentTimeMillis(),
20+
// Extended fields for detail screen
21+
val requestHeaders: Map<String, String> = emptyMap(),
22+
val responseHeaders: Map<String, String> = emptyMap(),
23+
val requestBody: String? = null,
24+
val responseBody: String? = null,
25+
val error: NetworkError? = null,
1826
)
1927

2028
public enum class HttpMethod {
@@ -28,3 +36,8 @@ public enum class HttpMethod {
2836
TRACE,
2937
UNKNOWN,
3038
}
39+
40+
/**
41+
* Error information for failed requests.
42+
*/
43+
public data class NetworkError(val title: String, val message: String, val stackTrace: String? = null)

0 commit comments

Comments
 (0)