Skip to content

Commit baee7de

Browse files
committed
Add interceptors support to DefaultHttpClient and WpRequestExecutor
Allow OkHttp interceptors to be passed to DefaultHttpClient while preserving hostname verification functionality. Add a convenience constructor to WpRequestExecutor that accepts interceptors directly. This enables clients to inject interceptors (e.g., for network debugging) without needing to manage OkHttpClient configuration. Changes: - Add `interceptors` parameter to `DefaultHttpClient` - Refactor to use `buildClient()` method for client construction - Add convenience constructor to `WpRequestExecutor`
1 parent 8670f3d commit baee7de

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/WpHttpClient.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
package rs.wordpress.api.kotlin
22

3+
import okhttp3.Interceptor
34
import okhttp3.OkHttpClient
45
import javax.net.ssl.HostnameVerifier
56
import javax.net.ssl.SSLSession
67

78
sealed class WpHttpClient {
89
abstract fun getClient(): OkHttpClient
910

10-
class DefaultHttpClient : WpHttpClient() {
11-
private var client: OkHttpClient = OkHttpClient()
11+
class DefaultHttpClient(
12+
private val interceptors: List<Interceptor> = emptyList()
13+
) : WpHttpClient() {
14+
private var client: OkHttpClient = buildClient()
1215

1316
private var allowedHostnames: Map<String, List<String>> = emptyMap()
1417

1518
fun addAllowedAlternativeNamesForHostname(hostname: String, allowedNames: List<String>) {
1619
// Preserve the previous records for this key
1720
val previousList = allowedHostnames[hostname].orEmpty()
1821
allowedHostnames = allowedHostnames.plus(Pair(hostname, allowedNames.plus(previousList)))
19-
updateClient()
22+
client = buildClient()
2023
}
2124

22-
private fun updateClient() {
23-
client = client.newBuilder()
24-
.hostnameVerifier(WpRequestExecutorHostnameVerifier(allowedHostnames))
25-
.build()
25+
private fun buildClient(): OkHttpClient {
26+
return OkHttpClient.Builder().apply {
27+
this@DefaultHttpClient.interceptors.forEach { addInterceptor(it) }
28+
hostnameVerifier(WpRequestExecutorHostnameVerifier(allowedHostnames))
29+
}.build()
2630
}
2731

2832
override fun getClient() = client

native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/WpRequestExecutor.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import kotlinx.coroutines.delay
66
import kotlinx.coroutines.withContext
77
import okhttp3.Call
88
import okhttp3.HttpUrl
9+
import okhttp3.Interceptor
910
import okhttp3.MediaType.Companion.toMediaType
1011
import okhttp3.MultipartBody
1112
import okhttp3.OkHttp
@@ -39,6 +40,23 @@ class WpRequestExecutor(
3940
private val fileResolver: FileResolver = DefaultFileResolver(),
4041
private val uploadListener: UploadListener? = null
4142
) : RequestExecutor {
43+
44+
/**
45+
* Convenience constructor that accepts a list of OkHttp interceptors.
46+
* Uses [WpHttpClient.DefaultHttpClient] internally with the provided interceptors.
47+
*/
48+
constructor(
49+
interceptors: List<Interceptor>,
50+
dispatcher: CoroutineDispatcher = Dispatchers.IO,
51+
fileResolver: FileResolver = DefaultFileResolver(),
52+
uploadListener: UploadListener? = null
53+
) : this(
54+
httpClient = WpHttpClient.DefaultHttpClient(interceptors),
55+
dispatcher = dispatcher,
56+
fileResolver = fileResolver,
57+
uploadListener = uploadListener
58+
)
59+
4260
override suspend fun execute(request: WpNetworkRequest): WpNetworkResponse =
4361
withContext(dispatcher) {
4462
val requestBuilder = Request.Builder().url(request.url())

0 commit comments

Comments
 (0)