Koin: Create a single OkHttpClient with dynamic parameter such as SSL Socket factory

Created on 3 Feb 2020  路  1Comment  路  Source: InsertKoinIO/koin

I have created a module httpClientModule i.e.

val httpClientModule = module {
    single(named(OK_HTTP_CLIENT)) { (sslSocketFactory: SSLSocketFactory?) ->
        RetrofitFactory.buildHttpClient(sslSocketFactory = sslSocketFactory)
    }
}

In the app there are some requests that needs to have a SSLSocketFactory and others that doesn't need for the SSLSocketFactory i.e.
request that needs SSLSocketFactory

fun buildHttpClientWithCertificate(): OkHttpClient {
        return get(named(OK_HTTP_CLIENT)) {
            parametersOf(playerLegacy.getOpVaultSSLSocketFactory())
        }
    } 

request that doesn't need SSLSocketFactory

fun buildOkHttpClient(): OkHttpClient =
        get(named(OK_HTTP_CLIENT)) { parametersOf(null) }

I am wondering if there is a way to test with Unit tests or functional tests to be sure that when we do a request with the SSLSocketFactory the okHttp instance use the given one ?
Same thing When there is no need for the SSLSocketFactory, the same okHttp instance created doesn't use the SSLSocketFactory instead it will use the null value given in the parametersOf ?

Thanks for your help 馃憤

question

Most helpful comment

To me this approach is not the way to go. If you have requests that require a (certain) SSLSocketfactory and others don't, than you seem to have two logically different setups. Indeed, OkHttp's client is immutable and you can't just live switch.

Instead, I suggest you set up 2 (named) instances of OkHttp, one with and one without the ssl factory, and use them appropriatedly in your app. That also solves your problem of having to test if the paramter given is the correct one - there will be no parameter, but only the (correct) OkHttp client instance.

>All comments

To me this approach is not the way to go. If you have requests that require a (certain) SSLSocketfactory and others don't, than you seem to have two logically different setups. Indeed, OkHttp's client is immutable and you can't just live switch.

Instead, I suggest you set up 2 (named) instances of OkHttp, one with and one without the ssl factory, and use them appropriatedly in your app. That also solves your problem of having to test if the paramter given is the correct one - there will be no parameter, but only the (correct) OkHttp client instance.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

miladsalimiiii picture miladsalimiiii  路  3Comments

dakuenjery picture dakuenjery  路  4Comments

caleb-allen picture caleb-allen  路  4Comments

sankarsana picture sankarsana  路  4Comments

iRYO400 picture iRYO400  路  3Comments