diff --git a/CHANGELOG.md b/CHANGELOG.md index fbf777ac6..02da60b91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ This file follows [Keepachangelog](https://keepachangelog.com/) format. Please add your entries according to this format. ## Unreleased +* Added ability to log manual transactions. In order to do this call ChuckerCollector#saveTransaction(transaction: ManualHttpTransaction) method and pass ManualHttpTransaction entity to it. ### Added * Decoding of request and response bodies can now be customized. In order to do this a `BodyDecoder` interface needs to be implemented and installed in the `ChuckerInterceptor` via `ChuckerInterceptor.addBinaryDecoder(decoder)` method. Decoded bodies are then displayed in the Chucker UI. diff --git a/library-no-op/api/library-no-op.api b/library-no-op/api/library-no-op.api index 99867260f..dd60d9ce4 100644 --- a/library-no-op/api/library-no-op.api +++ b/library-no-op/api/library-no-op.api @@ -16,6 +16,7 @@ public final class com/chuckerteam/chucker/api/ChuckerCollector { public fun (Landroid/content/Context;ZLcom/chuckerteam/chucker/api/RetentionManager$Period;)V public synthetic fun (Landroid/content/Context;ZLcom/chuckerteam/chucker/api/RetentionManager$Period;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun getShowNotification ()Z + public final fun saveTransaction (Lcom/chuckerteam/chucker/api/entity/ManualHttpTransaction;)V public final fun setShowNotification (Z)V public final fun writeTransactions (Landroid/content/Context;Ljava/lang/Long;)Landroid/net/Uri; } @@ -55,3 +56,95 @@ public final class com/chuckerteam/chucker/api/RetentionManager$Period : java/la public static fun values ()[Lcom/chuckerteam/chucker/api/RetentionManager$Period; } +public final class com/chuckerteam/chucker/api/entity/ManualHttpTransaction { + public fun ()V + public fun (Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;ZLjava/lang/Integer;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Z[B)V + public synthetic fun (Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;ZLjava/lang/Integer;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Z[BILkotlin/jvm/internal/DefaultConstructorMarker;)V + public final fun component1 ()Ljava/lang/Long; + public final fun component10 ()Ljava/lang/String; + public final fun component11 ()Ljava/lang/String; + public final fun component12 ()Ljava/lang/Long; + public final fun component13 ()Ljava/lang/String; + public final fun component14 ()Ljava/lang/String; + public final fun component15 ()Ljava/lang/Long; + public final fun component16 ()Ljava/lang/String; + public final fun component17 ()Z + public final fun component18 ()Ljava/lang/Integer; + public final fun component19 ()Ljava/lang/String; + public final fun component2 ()Ljava/lang/Long; + public final fun component20 ()Ljava/lang/String; + public final fun component21 ()Ljava/lang/Long; + public final fun component22 ()Ljava/lang/String; + public final fun component23 ()Ljava/lang/String; + public final fun component24 ()Ljava/lang/Long; + public final fun component25 ()Ljava/lang/String; + public final fun component26 ()Z + public final fun component27 ()[B + public final fun component3 ()Ljava/lang/Long; + public final fun component4 ()Ljava/lang/String; + public final fun component5 ()Ljava/lang/String; + public final fun component6 ()Ljava/lang/String; + public final fun component7 ()Ljava/lang/String; + public final fun component8 ()Ljava/lang/String; + public final fun component9 ()Ljava/lang/String; + public final fun copy (Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;ZLjava/lang/Integer;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Z[B)Lcom/chuckerteam/chucker/api/entity/ManualHttpTransaction; + public static synthetic fun copy$default (Lcom/chuckerteam/chucker/api/entity/ManualHttpTransaction;Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;ZLjava/lang/Integer;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Z[BILjava/lang/Object;)Lcom/chuckerteam/chucker/api/entity/ManualHttpTransaction; + public fun equals (Ljava/lang/Object;)Z + public final fun getError ()Ljava/lang/String; + public final fun getHost ()Ljava/lang/String; + public final fun getMethod ()Ljava/lang/String; + public final fun getPath ()Ljava/lang/String; + public final fun getProtocol ()Ljava/lang/String; + public final fun getRequestBody ()Ljava/lang/String; + public final fun getRequestContentType ()Ljava/lang/String; + public final fun getRequestDate ()Ljava/lang/Long; + public final fun getRequestHeaders ()Ljava/lang/String; + public final fun getRequestHeadersSize ()Ljava/lang/Long; + public final fun getRequestPayloadSize ()Ljava/lang/Long; + public final fun getResponseBody ()Ljava/lang/String; + public final fun getResponseCipherSuite ()Ljava/lang/String; + public final fun getResponseCode ()Ljava/lang/Integer; + public final fun getResponseContentType ()Ljava/lang/String; + public final fun getResponseDate ()Ljava/lang/Long; + public final fun getResponseHeaders ()Ljava/lang/String; + public final fun getResponseHeadersSize ()Ljava/lang/Long; + public final fun getResponseImageData ()[B + public final fun getResponseMessage ()Ljava/lang/String; + public final fun getResponsePayloadSize ()Ljava/lang/Long; + public final fun getResponseTlsVersion ()Ljava/lang/String; + public final fun getScheme ()Ljava/lang/String; + public final fun getTookMs ()Ljava/lang/Long; + public final fun getUrl ()Ljava/lang/String; + public fun hashCode ()I + public final fun isRequestBodyEncoded ()Z + public final fun isResponseBodyEncoded ()Z + public final fun setError (Ljava/lang/String;)V + public final fun setHost (Ljava/lang/String;)V + public final fun setMethod (Ljava/lang/String;)V + public final fun setPath (Ljava/lang/String;)V + public final fun setProtocol (Ljava/lang/String;)V + public final fun setRequestBody (Ljava/lang/String;)V + public final fun setRequestBodyEncoded (Z)V + public final fun setRequestContentType (Ljava/lang/String;)V + public final fun setRequestDate (Ljava/lang/Long;)V + public final fun setRequestHeaders (Ljava/lang/String;)V + public final fun setRequestHeadersSize (Ljava/lang/Long;)V + public final fun setRequestPayloadSize (Ljava/lang/Long;)V + public final fun setResponseBody (Ljava/lang/String;)V + public final fun setResponseBodyEncoded (Z)V + public final fun setResponseCipherSuite (Ljava/lang/String;)V + public final fun setResponseCode (Ljava/lang/Integer;)V + public final fun setResponseContentType (Ljava/lang/String;)V + public final fun setResponseDate (Ljava/lang/Long;)V + public final fun setResponseHeaders (Ljava/lang/String;)V + public final fun setResponseHeadersSize (Ljava/lang/Long;)V + public final fun setResponseImageData ([B)V + public final fun setResponseMessage (Ljava/lang/String;)V + public final fun setResponsePayloadSize (Ljava/lang/Long;)V + public final fun setResponseTlsVersion (Ljava/lang/String;)V + public final fun setScheme (Ljava/lang/String;)V + public final fun setTookMs (Ljava/lang/Long;)V + public final fun setUrl (Ljava/lang/String;)V + public fun toString ()Ljava/lang/String; +} + diff --git a/library-no-op/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerCollector.kt b/library-no-op/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerCollector.kt index dbea75a52..2198e8797 100644 --- a/library-no-op/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerCollector.kt +++ b/library-no-op/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerCollector.kt @@ -1,6 +1,7 @@ package com.chuckerteam.chucker.api import android.content.Context +import com.chuckerteam.chucker.api.entity.ManualHttpTransaction import android.net.Uri /** @@ -12,6 +13,14 @@ public class ChuckerCollector @JvmOverloads constructor( public var showNotification: Boolean = true, retentionPeriod: RetentionManager.Period = RetentionManager.Period.ONE_WEEK ) { + + /** + * No-op implementation. + */ + public fun saveTransaction(transaction: ManualHttpTransaction) { + // Empty method for the library-no-op artifact + } + @Suppress("FunctionOnlyReturningConstant") public fun writeTransactions( context: Context, diff --git a/library-no-op/src/main/kotlin/com/chuckerteam/chucker/api/entity/ManualHttpTransaction.kt b/library-no-op/src/main/kotlin/com/chuckerteam/chucker/api/entity/ManualHttpTransaction.kt new file mode 100644 index 000000000..6cb52966f --- /dev/null +++ b/library-no-op/src/main/kotlin/com/chuckerteam/chucker/api/entity/ManualHttpTransaction.kt @@ -0,0 +1,34 @@ +package com.chuckerteam.chucker.api.entity + +/** + * No-op implementation. + */ +public data class ManualHttpTransaction( + public var requestDate: Long? = null, + public var responseDate: Long? = null, + public var tookMs: Long? = null, + public var protocol: String? = null, + public var method: String? = null, + public var url: String? = null, + public var host: String? = null, + public var path: String? = null, + public var scheme: String? = null, + public var responseTlsVersion: String? = null, + public var responseCipherSuite: String? = null, + public var requestPayloadSize: Long? = null, + public var requestContentType: String? = null, + public var requestHeaders: String? = null, + public var requestHeadersSize: Long? = null, + public var requestBody: String? = null, + public var isRequestBodyEncoded: Boolean = false, + public var responseCode: Int? = null, + public var responseMessage: String? = null, + public var error: String? = null, + public var responsePayloadSize: Long? = null, + public var responseContentType: String? = null, + public var responseHeaders: String? = null, + public var responseHeadersSize: Long? = null, + public var responseBody: String? = null, + public var isResponseBodyEncoded: Boolean = false, + public var responseImageData: ByteArray? = null +) diff --git a/library/api/library.api b/library/api/library.api index 3ee45a1ff..d625ce928 100644 --- a/library/api/library.api +++ b/library/api/library.api @@ -16,6 +16,7 @@ public final class com/chuckerteam/chucker/api/ChuckerCollector { public fun (Landroid/content/Context;ZLcom/chuckerteam/chucker/api/RetentionManager$Period;)V public synthetic fun (Landroid/content/Context;ZLcom/chuckerteam/chucker/api/RetentionManager$Period;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun getShowNotification ()Z + public final fun saveTransaction (Lcom/chuckerteam/chucker/api/entity/ManualHttpTransaction;)V public final fun setShowNotification (Z)V public final fun writeTransactions (Landroid/content/Context;Ljava/lang/Long;)Landroid/net/Uri; } @@ -54,3 +55,95 @@ public final class com/chuckerteam/chucker/api/RetentionManager$Period : java/la public static fun values ()[Lcom/chuckerteam/chucker/api/RetentionManager$Period; } +public final class com/chuckerteam/chucker/api/entity/ManualHttpTransaction { + public fun ()V + public fun (Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;ZLjava/lang/Integer;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Z[B)V + public synthetic fun (Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;ZLjava/lang/Integer;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Z[BILkotlin/jvm/internal/DefaultConstructorMarker;)V + public final fun component1 ()Ljava/lang/Long; + public final fun component10 ()Ljava/lang/String; + public final fun component11 ()Ljava/lang/String; + public final fun component12 ()Ljava/lang/Long; + public final fun component13 ()Ljava/lang/String; + public final fun component14 ()Ljava/lang/String; + public final fun component15 ()Ljava/lang/Long; + public final fun component16 ()Ljava/lang/String; + public final fun component17 ()Z + public final fun component18 ()Ljava/lang/Integer; + public final fun component19 ()Ljava/lang/String; + public final fun component2 ()Ljava/lang/Long; + public final fun component20 ()Ljava/lang/String; + public final fun component21 ()Ljava/lang/Long; + public final fun component22 ()Ljava/lang/String; + public final fun component23 ()Ljava/lang/String; + public final fun component24 ()Ljava/lang/Long; + public final fun component25 ()Ljava/lang/String; + public final fun component26 ()Z + public final fun component27 ()[B + public final fun component3 ()Ljava/lang/Long; + public final fun component4 ()Ljava/lang/String; + public final fun component5 ()Ljava/lang/String; + public final fun component6 ()Ljava/lang/String; + public final fun component7 ()Ljava/lang/String; + public final fun component8 ()Ljava/lang/String; + public final fun component9 ()Ljava/lang/String; + public final fun copy (Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;ZLjava/lang/Integer;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Z[B)Lcom/chuckerteam/chucker/api/entity/ManualHttpTransaction; + public static synthetic fun copy$default (Lcom/chuckerteam/chucker/api/entity/ManualHttpTransaction;Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;ZLjava/lang/Integer;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Z[BILjava/lang/Object;)Lcom/chuckerteam/chucker/api/entity/ManualHttpTransaction; + public fun equals (Ljava/lang/Object;)Z + public final fun getError ()Ljava/lang/String; + public final fun getHost ()Ljava/lang/String; + public final fun getMethod ()Ljava/lang/String; + public final fun getPath ()Ljava/lang/String; + public final fun getProtocol ()Ljava/lang/String; + public final fun getRequestBody ()Ljava/lang/String; + public final fun getRequestContentType ()Ljava/lang/String; + public final fun getRequestDate ()Ljava/lang/Long; + public final fun getRequestHeaders ()Ljava/lang/String; + public final fun getRequestHeadersSize ()Ljava/lang/Long; + public final fun getRequestPayloadSize ()Ljava/lang/Long; + public final fun getResponseBody ()Ljava/lang/String; + public final fun getResponseCipherSuite ()Ljava/lang/String; + public final fun getResponseCode ()Ljava/lang/Integer; + public final fun getResponseContentType ()Ljava/lang/String; + public final fun getResponseDate ()Ljava/lang/Long; + public final fun getResponseHeaders ()Ljava/lang/String; + public final fun getResponseHeadersSize ()Ljava/lang/Long; + public final fun getResponseImageData ()[B + public final fun getResponseMessage ()Ljava/lang/String; + public final fun getResponsePayloadSize ()Ljava/lang/Long; + public final fun getResponseTlsVersion ()Ljava/lang/String; + public final fun getScheme ()Ljava/lang/String; + public final fun getTookMs ()Ljava/lang/Long; + public final fun getUrl ()Ljava/lang/String; + public fun hashCode ()I + public final fun isRequestBodyEncoded ()Z + public final fun isResponseBodyEncoded ()Z + public final fun setError (Ljava/lang/String;)V + public final fun setHost (Ljava/lang/String;)V + public final fun setMethod (Ljava/lang/String;)V + public final fun setPath (Ljava/lang/String;)V + public final fun setProtocol (Ljava/lang/String;)V + public final fun setRequestBody (Ljava/lang/String;)V + public final fun setRequestBodyEncoded (Z)V + public final fun setRequestContentType (Ljava/lang/String;)V + public final fun setRequestDate (Ljava/lang/Long;)V + public final fun setRequestHeaders (Ljava/lang/String;)V + public final fun setRequestHeadersSize (Ljava/lang/Long;)V + public final fun setRequestPayloadSize (Ljava/lang/Long;)V + public final fun setResponseBody (Ljava/lang/String;)V + public final fun setResponseBodyEncoded (Z)V + public final fun setResponseCipherSuite (Ljava/lang/String;)V + public final fun setResponseCode (Ljava/lang/Integer;)V + public final fun setResponseContentType (Ljava/lang/String;)V + public final fun setResponseDate (Ljava/lang/Long;)V + public final fun setResponseHeaders (Ljava/lang/String;)V + public final fun setResponseHeadersSize (Ljava/lang/Long;)V + public final fun setResponseImageData ([B)V + public final fun setResponseMessage (Ljava/lang/String;)V + public final fun setResponsePayloadSize (Ljava/lang/Long;)V + public final fun setResponseTlsVersion (Ljava/lang/String;)V + public final fun setScheme (Ljava/lang/String;)V + public final fun setTookMs (Ljava/lang/Long;)V + public final fun setUrl (Ljava/lang/String;)V + public fun toString ()Ljava/lang/String; +} + diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerCollector.kt b/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerCollector.kt index daf332114..42f09d33c 100644 --- a/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerCollector.kt +++ b/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerCollector.kt @@ -1,6 +1,7 @@ package com.chuckerteam.chucker.api import android.content.Context +import com.chuckerteam.chucker.api.entity.ManualHttpTransaction import android.net.Uri import com.chuckerteam.chucker.internal.data.entity.HttpTransaction import com.chuckerteam.chucker.internal.data.repository.RepositoryProvider @@ -67,6 +68,15 @@ public class ChuckerCollector @JvmOverloads constructor( } } + /** + * Call this method whenever you want to save a transaction that is not collected by Chucker interceptor + * + * @param transaction the manual transaction you want to save it manually + */ + public fun saveTransaction(transaction: ManualHttpTransaction) { + onRequestSent(transaction.convertToHttpTransaction()) + } + /** * Export the Chucker transactions to a file. Please note that this function is blocking * and performs Disk I/O. Make sure you run it on a separate thread or coroutine. diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/api/entity/ManualHttpTransaction.kt b/library/src/main/kotlin/com/chuckerteam/chucker/api/entity/ManualHttpTransaction.kt new file mode 100644 index 000000000..d82573b6a --- /dev/null +++ b/library/src/main/kotlin/com/chuckerteam/chucker/api/entity/ManualHttpTransaction.kt @@ -0,0 +1,74 @@ +package com.chuckerteam.chucker.api.entity + +import com.chuckerteam.chucker.internal.data.entity.HttpTransaction + +/** + * Represent Manual Http transaction that developer want to populate as an custom http transaction + */ +public data class ManualHttpTransaction( + public var requestDate: Long? = null, + public var responseDate: Long? = null, + public var tookMs: Long? = null, + public var protocol: String? = null, + public var method: String? = null, + public var url: String? = null, + public var host: String? = null, + public var path: String? = null, + public var scheme: String? = null, + public var responseTlsVersion: String? = null, + public var responseCipherSuite: String? = null, + public var requestPayloadSize: Long? = null, + public var requestContentType: String? = null, + public var requestHeaders: String? = null, + public var requestHeadersSize: Long? = null, + public var requestBody: String? = null, + public var isRequestBodyEncoded: Boolean = false, + public var responseCode: Int? = null, + public var responseMessage: String? = null, + public var error: String? = null, + public var responsePayloadSize: Long? = null, + public var responseContentType: String? = null, + public var responseHeaders: String? = null, + public var responseHeadersSize: Long? = null, + public var responseBody: String? = null, + public var isResponseBodyEncoded: Boolean = false, + public var responseImageData: ByteArray? = null +) { + + /** + * this will convert this class to HttpTransaction to be able to save it on database as a real + * transaction. + */ + internal fun convertToHttpTransaction(): HttpTransaction { + return HttpTransaction( + 0, + requestDate, + responseDate, + tookMs, + protocol, + method, + url, + host, + path, + scheme, + responseTlsVersion, + responseCipherSuite, + requestPayloadSize, + requestContentType, + requestHeaders, + requestHeadersSize, + requestBody, + isRequestBodyEncoded, + responseCode, + responseMessage, + error, + responsePayloadSize, + responseContentType, + responseHeaders, + responseHeadersSize, + responseBody, + isResponseBodyEncoded, + responseImageData + ) + } +} diff --git a/sample/src/main/kotlin/com/chuckerteam/chucker/sample/MainActivity.kt b/sample/src/main/kotlin/com/chuckerteam/chucker/sample/MainActivity.kt index 6f281f2db..dafbb21c1 100644 --- a/sample/src/main/kotlin/com/chuckerteam/chucker/sample/MainActivity.kt +++ b/sample/src/main/kotlin/com/chuckerteam/chucker/sample/MainActivity.kt @@ -9,6 +9,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.lifecycleScope import com.chuckerteam.chucker.api.Chucker import com.chuckerteam.chucker.api.ChuckerCollector +import com.chuckerteam.chucker.api.RetentionManager import com.chuckerteam.chucker.sample.databinding.ActivityMainSampleBinding import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -20,12 +21,25 @@ class MainActivity : AppCompatActivity() { private lateinit var mainBinding: ActivityMainSampleBinding + private val chuckerCollector: ChuckerCollector by lazy { + ChuckerCollector( + context = applicationContext, + showNotification = true, + retentionPeriod = RetentionManager.Period.ONE_HOUR + ) + } + private val client by lazy { - createOkHttpClient(applicationContext, interceptorTypeSelector) + createOkHttpClient(applicationContext, interceptorTypeSelector, chuckerCollector) } private val httpTasks by lazy { - listOf(HttpBinHttpTask(client), DummyImageHttpTask(client), PostmanEchoHttpTask(client)) + listOf( + HttpBinHttpTask(client), + DummyImageHttpTask(client), + PostmanEchoHttpTask(client), + ManualTransactionTask(chuckerCollector) + ) } override fun onCreate(savedInstanceState: Bundle?) { diff --git a/sample/src/main/kotlin/com/chuckerteam/chucker/sample/ManualTransactionTask.kt b/sample/src/main/kotlin/com/chuckerteam/chucker/sample/ManualTransactionTask.kt new file mode 100644 index 000000000..2dffda1fe --- /dev/null +++ b/sample/src/main/kotlin/com/chuckerteam/chucker/sample/ManualTransactionTask.kt @@ -0,0 +1,34 @@ +package com.chuckerteam.chucker.sample + +import com.chuckerteam.chucker.api.ChuckerCollector +import com.chuckerteam.chucker.api.entity.ManualHttpTransaction + +class ManualTransactionTask( + private val chuckerCollector: ChuckerCollector +) : HttpTask { + + override fun run() { + val responseBody = "Some Custom response body!" + chuckerCollector.saveTransaction( + transaction = ManualHttpTransaction( + requestDate = System.currentTimeMillis(), + responseDate = System.currentTimeMillis(), + protocol = "CustomProtocol", + method = "Custom Method", + responseContentType = "text/html; charset=UTF-8", + requestContentType = "text/html; charset=UTF-8", + scheme = "http", + isResponseBodyEncoded = false, + tookMs = 1000, + responseCode = 200, + responseMessage = "OK", + responseBody = responseBody, + isRequestBodyEncoded = false, + responsePayloadSize = responseBody.toByteArray().size.toLong(), + host = "Custom HOST", + url = "http://customUrl.com/", + path = "/custom", + ) + ) + } +} diff --git a/sample/src/main/kotlin/com/chuckerteam/chucker/sample/OkHttpUtils.kt b/sample/src/main/kotlin/com/chuckerteam/chucker/sample/OkHttpUtils.kt index 33e3e3c50..d6b983e3e 100644 --- a/sample/src/main/kotlin/com/chuckerteam/chucker/sample/OkHttpUtils.kt +++ b/sample/src/main/kotlin/com/chuckerteam/chucker/sample/OkHttpUtils.kt @@ -16,12 +16,13 @@ const val SEGMENT_SIZE = 8_192L fun createOkHttpClient( context: Context, interceptorTypeProvider: InterceptorType.Provider, -): OkHttpClient { - val collector = ChuckerCollector( + collector: ChuckerCollector = ChuckerCollector( context = context, showNotification = true, retentionPeriod = RetentionManager.Period.ONE_HOUR ) +): OkHttpClient { + @Suppress("MagicNumber") val chuckerInterceptor = ChuckerInterceptor.Builder(context)