Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General improvements #101

Merged
merged 6 commits into from
Mar 31, 2024
Merged
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
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
ext {
kotlinVersion = '1.6.21'
androidGradleVersion = '7.0.4'
androidGradleVersion = '7.4.2'
coroutineVersion = '1.5.2'

// Google libraries
Expand All @@ -25,7 +25,6 @@ buildscript {

// Debug and quality control
binaryCompatibilityValidator = '0.8.0'
detektVersion = '1.14.0'
dokkaVersion = '1.4.10.2'
ktLintVersion = '0.39.0'
ktLintGradleVersion = '10.0.0'
Expand Down Expand Up @@ -55,7 +54,6 @@ buildscript {
classpath "de.mannodermaus.gradle.plugins:android-junit5:$junitGradlePluignVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokkaVersion"
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:$detektVersion"
classpath "org.jlleitschuh.gradle:ktlint-gradle:$ktLintGradleVersion"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:$jfrogExtractor"
classpath "org.jetbrains.kotlinx:binary-compatibility-validator:$binaryCompatibilityValidator"
Expand Down
23 changes: 0 additions & 23 deletions gradle/kotlin-static-analysis.gradle

This file was deleted.

2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
4 changes: 1 addition & 3 deletions library-no-op/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
}

apply from: rootProject.file('gradle/kotlin-static-analysis.gradle')

ext.versionName = { ->
def currentTag = 'git tag --points-at HEAD'.execute().in.text.toString().trim()
def currentBranch = 'git rev-parse --abbrev-ref HEAD'.execute().in.text.toString().trim()
Expand All @@ -59,7 +57,7 @@ def libraryVersion = versionName()

task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.srcDirs
from android.sourceSets.main.kotlin.srcDirs
}

artifactoryPublish.dependsOn('build')
Expand Down
4 changes: 1 addition & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ dependencies {
testImplementation "org.robolectric:robolectric:$robolectricVersion"
}

apply from: rootProject.file('gradle/kotlin-static-analysis.gradle')

ext.versionName = { ->
def currentTag = 'git tag --points-at HEAD'.execute().in.text.toString().trim()
def currentBranch = 'git rev-parse --abbrev-ref HEAD'.execute().in.text.toString().trim()
Expand All @@ -112,7 +110,7 @@ def libraryVersion = versionName()

task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.srcDirs
from android.sourceSets.main.kotlin.srcDirs
}

artifactoryPublish.dependsOn('build')
Expand Down
23 changes: 23 additions & 0 deletions library/src/main/kotlin/com/chuckerteam/chucker/api/Chucker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import android.os.Build
import android.util.Log
import androidx.core.content.getSystemService
import com.chuckerteam.chucker.R
import com.chuckerteam.chucker.internal.data.entity.HttpTransaction
import com.chuckerteam.chucker.internal.data.repository.RepositoryProvider
import com.chuckerteam.chucker.internal.support.HarUtils
import com.chuckerteam.chucker.internal.support.Logger
import com.chuckerteam.chucker.internal.support.NotificationHelper
import com.chuckerteam.chucker.internal.support.TransactionDetailsHarSharable
import com.chuckerteam.chucker.internal.ui.MainActivity
import okio.buffer

/**
* Chucker methods and utilities to interact with the library.
Expand Down Expand Up @@ -75,6 +80,24 @@ public object Chucker {
NotificationHelper(context).dismissNotifications()
}

public suspend fun clearTransactions() {
RepositoryProvider.transaction().deleteAllTransactions()
NotificationHelper.clearBuffer()
}

public suspend fun generateHar(context: Context, transactionsLimit: Int = 1000): ByteArray {
val transactions: List<HttpTransaction> =
RepositoryProvider.transaction().getTransactions(transactionsLimit)
val sharable = TransactionDetailsHarSharable(
content = HarUtils.harStringFromTransactions(
transactions,
context.getString(R.string.chucker_name),
context.getString(R.string.chucker_version)
)
)
return sharable.toSharableContent(context).buffer().use { it.readByteArray() }
}

internal var logger: Logger = object : Logger {
val TAG = "Chucker"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class ChuckerInterceptor private constructor(

private val collector = builder.collector ?: ChuckerCollector(builder.context)

private val saveImageResponses = builder.saveImageResponses

private val requestProcessor = RequestProcessor(
builder.context,
collector,
Expand All @@ -51,6 +53,7 @@ public class ChuckerInterceptor private constructor(
headersToRedact,
builder.alwaysReadResponseBody,
decoders,
saveImageResponses,
)

init {
Expand Down Expand Up @@ -95,6 +98,7 @@ public class ChuckerInterceptor private constructor(
internal var headersToRedact = emptySet<String>()
internal var decoders = emptyList<BodyDecoder>()
internal var createShortcut = true
internal var saveImageResponses = false

/**
* Sets the [ChuckerCollector] to customize data retention.
Expand Down Expand Up @@ -155,6 +159,10 @@ public class ChuckerInterceptor private constructor(
this.createShortcut = enable
}

public fun saveImageResponses(enable: Boolean): Builder = apply {
this.saveImageResponses = enable
}

/**
* Sets provider of a directory where Chucker will save temporary responses
* before processing them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ internal class HttpTransaction(
return FormattedUrl.fromHttpUrl(httpUrl, encode).url
}

fun isResponseImage(): Boolean {
val contentType = responseContentType
return contentType != null && contentType.startsWith("image/", ignoreCase = true)
}

fun getFormattedPath(encode: Boolean): String {
val httpUrl = url?.toHttpUrl() ?: return ""
return FormattedUrl.fromHttpUrl(httpUrl, encode).pathWithQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ internal class HttpTransactionDatabaseRepository(private val database: ChuckerDa
}

override suspend fun getAllTransactions(): List<HttpTransaction> = transactionDao.getAll()

override suspend fun getTransactions(limit: Int): List<HttpTransaction> = transactionDao.getN(limit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ internal interface HttpTransactionRepository {
fun getTransaction(transactionId: Long): LiveData<HttpTransaction?>

suspend fun getAllTransactions(): List<HttpTransaction>

suspend fun getTransactions(limit: Int): List<HttpTransaction>
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ internal interface HttpTransactionDao {

@Query("SELECT * FROM transactions")
suspend fun getAll(): List<HttpTransaction>

@Query("SELECT * FROM transactions LIMIT :limit")
suspend fun getN(limit: Int): List<HttpTransaction>
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal class ResponseProcessor(
private val headersToRedact: Set<String>,
private val alwaysReadResponseBody: Boolean,
private val bodyDecoders: List<BodyDecoder>,
private val saveImageResponses: Boolean,
) {
fun process(response: Response, transaction: HttpTransaction): Response {
processResponseMetadata(response, transaction)
Expand Down Expand Up @@ -90,7 +91,7 @@ internal class ResponseProcessor(
val contentType = responseBody.contentType()

val isImageContentType = contentType?.toString()?.contains(CONTENT_TYPE_IMAGE, ignoreCase = true) == true
if (isImageContentType) {
if (isImageContentType && saveImageResponses) {
if (payload.size < MAX_BLOB_SIZE) {
transaction.responseImageData = payload.readByteArray()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chuckerteam.chucker.internal.ui.transaction

import android.os.Bundle
import android.text.util.Linkify
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
Expand Down Expand Up @@ -56,6 +57,9 @@ internal class TransactionOverviewFragment : Fragment() {
private fun populateUI(transaction: HttpTransaction?, encodeUrl: Boolean) {
with(overviewBinding) {
url.text = transaction?.getFormattedUrl(encodeUrl)
if (transaction?.isResponseImage() == true) {
Linkify.addLinks(url, Linkify.WEB_URLS)
}
method.text = transaction?.method
protocol.text = transaction?.protocol
status.text = transaction?.status.toString()
Expand Down
2 changes: 0 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,3 @@ dependencies {

debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakcanaryVersion"
}

apply from: rootProject.file('gradle/kotlin-static-analysis.gradle')
Loading