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

Add an Android Client Builder following Coil, Koin patterns #7846

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2022 Block, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package okhttp3.android

import android.annotation.SuppressLint
import android.content.Context
import okhttp3.Cache
import okhttp3.ConnectionPool
import okhttp3.OkHttpClient
import okhttp3.brotli.BrotliInterceptor

@SuppressLint("NewApi")
fun OkHttpClient.Companion.newAndroidClient(
context: Context,
debug: Boolean,
clientConfig: OkHttpClient.Builder.() -> Unit = {},
): OkHttpClient {
val clientBuilder = OkHttpClient.Builder()
.addInterceptor(TracingInterceptor())
// .addInterceptor(AlwaysHttpsInterceptor.AndroidNetworkSecurityPolicy)
.apply {
if (debug) {
// eventListenerFactory(LoggingEventListener.AndroidLogging())
}
}

with(clientBuilder) {
clientConfig()
}

val cache = Cache(context.cacheDir.resolve("okhttp"), 10_000_000)

clientBuilder
.addInterceptor(BrotliInterceptor)
.connectionPool(ConnectionPool(connectionListener = TracingConnectionListener()))
.cache(cache)

return clientBuilder.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package okhttp3.android

import android.content.Context
import android.content.pm.ApplicationInfo
import okhttp3.OkHttpClient


object OkHttpClientContext {
private var _okHttpClient: OkHttpClient? = null

val Context.okHttpClient: OkHttpClient
get() {
return _okHttpClient ?: newOkHttpClient(this)
}

@Synchronized
private fun newOkHttpClient(context: Context): OkHttpClient {
_okHttpClient?.let { return it }

val newOkHttpClient = (context.applicationContext as? OkHttpClientFactory)?.newOkHttpClient()
?: OkHttpClient.newAndroidClient(context = context, debug = context.isDebug)
_okHttpClient = newOkHttpClient
return newOkHttpClient
}

private val Context.isDebug: Boolean
get() = (applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package okhttp3.android

import okhttp3.OkHttpClient

interface OkHttpClientFactory {
fun newOkHttpClient(): OkHttpClient
}