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

Remove deprecated constructor and use exposed builder pattern #483

Merged
merged 1 commit into from
Nov 9, 2020
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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
This file follows [Keepachangelog](https://keepachangelog.com/) format.
Please add your entries according to this format.

## Unreleased

### Removed

* Removed parametrized `ChuckerInterceptor` constructor in favour of builder pattern. Constructor that accepts only `Context` is still available.

## Version 3.4.0 *(2020-11-05)*

### Added
Expand Down Expand Up @@ -465,4 +471,4 @@ Initial release.
[#422]: https://github.com/ChuckerTeam/chucker/issues/422
[#465]: https://github.com/ChuckerTeam/chucker/issues/465
[#472]: https://github.com/ChuckerTeam/chucker/issues/472
[#480]: https://github.com/ChuckerTeam/chucker/issues/480
[#480]: https://github.com/ChuckerTeam/chucker/issues/480
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import kotlin.jvm.Throws
/**
* No-op implementation.
*/
public class ChuckerInterceptor @JvmOverloads constructor(
context: Context,
collector: Any? = null,
maxContentLength: Any? = null,
headersToRedact: Any? = null,
alwaysReadResponseBody: Any? = null,
public class ChuckerInterceptor private constructor(
builder: Builder,
) : Interceptor {

/**
* No-op implementation.
*/
public constructor(context: Context) : this(Builder(context))

public fun redactHeaders(vararg names: String): ChuckerInterceptor {
return this
}
Expand All @@ -41,6 +42,6 @@ public class ChuckerInterceptor @JvmOverloads constructor(

public fun alwaysReadResponseBody(enable: Boolean): Builder = this

public fun build(): ChuckerInterceptor = ChuckerInterceptor(context)
public fun build(): ChuckerInterceptor = ChuckerInterceptor(this)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chuckerteam.chucker.api

import android.content.Context
import androidx.annotation.VisibleForTesting
import com.chuckerteam.chucker.internal.data.entity.HttpTransaction
import com.chuckerteam.chucker.internal.support.CacheDirectoryProvider
import com.chuckerteam.chucker.internal.support.DepletingSource
Expand Down Expand Up @@ -28,73 +29,29 @@ import kotlin.jvm.Throws
/**
* An OkHttp Interceptor which persists and displays HTTP activity
* in your application for later inspection.
*
* @param context An Android [Context]
* @param collector A [ChuckerCollector] to customize data retention
* @param maxContentLength The maximum length for request and response content
* before their truncation. Warning: setting this value too high may cause unexpected
* results.
* @param cacheDirectoryProvider Provider of [File] where Chucker will save temporary responses
* before processing them.
* @param alwaysReadResponseBody If set to `true` Chucker will read full content of response
* bodies even in case of parsing errors or closing the response body without reading it.
* @param headersToRedact a [Set] of headers you want to redact. They will be replaced
* with a `**` in the Chucker UI.
*/
public class ChuckerInterceptor internal constructor(
private val context: Context,
private val collector: ChuckerCollector = ChuckerCollector(context),
private val maxContentLength: Long = 250000L,
private val cacheDirectoryProvider: CacheDirectoryProvider,
private val alwaysReadResponseBody: Boolean = false,
headersToRedact: Set<String> = emptySet(),
public class ChuckerInterceptor private constructor(
builder: Builder,
) : Interceptor {

/**
* An OkHttp Interceptor which persists and displays HTTP activity
* in your application for later inspection.
*
* This constructor is a shorthand for `ChuckerInterceptor.Builder(context).build()`.
*
* @param context An Android [Context]
* @param collector A [ChuckerCollector] to customize data retention
* @param maxContentLength The maximum length for request and response content
* before their truncation. Warning: setting this value too high may cause unexpected
* results.
* @param alwaysReadResponseBody If set to `true` Chucker will read full content of response
* bodies even in case of parsing errors or closing the response body without reading it.
* @param headersToRedact a [Set] of headers you want to redact. They will be replaced
* with a `**` in the Chucker UI.
* @see ChuckerInterceptor.Builder
*/
@JvmOverloads
@Deprecated(
message = "" +
"Customisation of ChuckerInterceptor should be replaced with a builder pattern " +
"unless you pass only Context.",
replaceWith = ReplaceWith(
"ChuckerInterceptor.Builder(context)\n" +
".collector(collector)\n" +
".maxContentLength(maxContentLength)\n" +
".redactHeaders(headersToRedact)\n" +
".alwaysReadResponseBody(alwaysReadResponseBody)\n" +
".build()"
)
)
public constructor(
context: Context,
collector: ChuckerCollector = ChuckerCollector(context),
maxContentLength: Long = 250000L,
headersToRedact: Set<String> = emptySet(),
alwaysReadResponseBody: Boolean = false,
) : this(
context = context,
collector = collector,
maxContentLength = maxContentLength,
cacheDirectoryProvider = { context.cacheDir },
alwaysReadResponseBody = alwaysReadResponseBody,
headersToRedact = headersToRedact,
)

private val io: IOUtils = IOUtils(context)
private val headersToRedact: MutableSet<String> = headersToRedact.toMutableSet()
public constructor(context: Context) : this(Builder(context))

private val context = builder.context
private val collector = builder.collector ?: ChuckerCollector(context)
private val maxContentLength = builder.maxContentLength
private val cacheDirectoryProvider = builder.cacheDirectoryProvider ?: CacheDirectoryProvider { context.filesDir }
Comment on lines +49 to +51
Copy link
Contributor Author

@MiSikora MiSikora Oct 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the inconvenience that I mentioned. Default fields are declared here instead of in the builder. It is only due to test purposes. Instantiating ChuckerCollector would require a lot of mocking of Context that I don't want to be bothered with.

On the other hand it is nice to not create stuff eagerly in the builder.

private val alwaysReadResponseBody = builder.alwaysReadResponseBody
private val io = IOUtils(builder.context)
private val headersToRedact = builder.headersToRedact.toMutableSet()

/** Adds [headerName] into [headersToRedact] */
public fun redactHeader(vararg headerName: String) {
Expand Down Expand Up @@ -308,12 +265,12 @@ public class ChuckerInterceptor internal constructor(
*
* @param context An Android [Context].
*/
public class Builder(private var context: Context) {
private var collector: ChuckerCollector? = null
private var maxContentLength = MAX_CONTENT_LENGTH
private var cacheDirectoryProvider: CacheDirectoryProvider? = null
private var alwaysReadResponseBody = false
private var headersToRedact = emptySet<String>()
public class Builder(internal var context: Context) {
internal var collector: ChuckerCollector? = null
internal var maxContentLength = MAX_CONTENT_LENGTH
internal var cacheDirectoryProvider: CacheDirectoryProvider? = null
internal var alwaysReadResponseBody = false
internal var headersToRedact = emptySet<String>()

/**
* Sets the [ChuckerCollector] to customize data retention.
Expand Down Expand Up @@ -358,17 +315,19 @@ public class ChuckerInterceptor internal constructor(
this.alwaysReadResponseBody = enable
}

/**
* Sets provider of a directory where Chucker will save temporary responses
* before processing them.
*/
@VisibleForTesting
internal fun cacheDirectorProvider(provider: CacheDirectoryProvider): Builder = apply {
this.cacheDirectoryProvider = provider
}

/**
* Creates a new [ChuckerInterceptor] instance with values defined in this builder.
*/
public fun build(): ChuckerInterceptor = ChuckerInterceptor(
context = context,
collector = collector ?: ChuckerCollector(context),
maxContentLength = maxContentLength,
cacheDirectoryProvider = cacheDirectoryProvider ?: CacheDirectoryProvider { context.filesDir },
alwaysReadResponseBody = alwaysReadResponseBody,
headersToRedact = headersToRedact,
)
public fun build(): ChuckerInterceptor = ChuckerInterceptor(this)
}

private companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ internal class ChuckerInterceptorDelegate(
}
}

private val chucker = ChuckerInterceptor(
context = mockContext,
collector = mockCollector,
maxContentLength = maxContentLength,
headersToRedact = headersToRedact,
cacheDirectoryProvider = cacheDirectoryProvider,
alwaysReadResponseBody = alwaysReadResponseBody,
)
private val chucker = ChuckerInterceptor.Builder(context = mockContext)
.collector(mockCollector)
.maxContentLength(maxContentLength)
.redactHeaders(headersToRedact)
.alwaysReadResponseBody(alwaysReadResponseBody)
.cacheDirectorProvider(cacheDirectoryProvider)
.build()

internal fun expectTransaction(): HttpTransaction {
if (transactions.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ class HttpBinClient(
retentionPeriod = RetentionManager.Period.ONE_HOUR
)

private val chuckerInterceptor = ChuckerInterceptor(
context = context,
collector = collector,
maxContentLength = 250000L,
headersToRedact = emptySet<String>()
)
private val chuckerInterceptor = ChuckerInterceptor.Builder(context)
.collector(collector)
.maxContentLength(250000L)
.redactHeaders(emptySet())
.build()

private val httpClient =
OkHttpClient.Builder()
Expand Down