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

Support configuring ApolloClient with lazily initialized Call.Factory. #5784

Merged
merged 1 commit into from
Apr 8, 2024
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
2 changes: 2 additions & 0 deletions libraries/apollo-runtime/api/android/apollo-runtime.api
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public abstract interface class com/apollographql/apollo3/network/NetworkTranspo
}

public final class com/apollographql/apollo3/network/OkHttpExtensionsKt {
public static final fun okHttpCallFactory (Lcom/apollographql/apollo3/ApolloClient$Builder;Lkotlin/jvm/functions/Function0;)Lcom/apollographql/apollo3/ApolloClient$Builder;
public static final fun okHttpCallFactory (Lcom/apollographql/apollo3/ApolloClient$Builder;Lokhttp3/Call$Factory;)Lcom/apollographql/apollo3/ApolloClient$Builder;
public static final fun okHttpCallFactory (Lcom/apollographql/apollo3/network/http/HttpNetworkTransport$Builder;Lokhttp3/Call$Factory;)Lcom/apollographql/apollo3/network/http/HttpNetworkTransport$Builder;
public static final fun okHttpClient (Lcom/apollographql/apollo3/ApolloClient$Builder;Lokhttp3/OkHttpClient;)Lcom/apollographql/apollo3/ApolloClient$Builder;
Expand Down Expand Up @@ -223,6 +224,7 @@ public final class com/apollographql/apollo3/network/http/BatchingHttpIntercepto
public final class com/apollographql/apollo3/network/http/DefaultHttpEngine {
public static final fun DefaultHttpEngine (J)Lcom/apollographql/apollo3/network/http/HttpEngine;
public static final fun DefaultHttpEngine (JJ)Lcom/apollographql/apollo3/network/http/HttpEngine;
public static final fun DefaultHttpEngine (Lkotlin/jvm/functions/Function0;)Lcom/apollographql/apollo3/network/http/HttpEngine;
public static final fun DefaultHttpEngine (Lokhttp3/Call$Factory;)Lcom/apollographql/apollo3/network/http/HttpEngine;
public static final fun DefaultHttpEngine (Lokhttp3/OkHttpClient;)Lcom/apollographql/apollo3/network/http/HttpEngine;
public static synthetic fun DefaultHttpEngine$default (JILjava/lang/Object;)Lcom/apollographql/apollo3/network/http/HttpEngine;
Expand Down
2 changes: 2 additions & 0 deletions libraries/apollo-runtime/api/jvm/apollo-runtime.api
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public abstract interface class com/apollographql/apollo3/network/NetworkTranspo
}

public final class com/apollographql/apollo3/network/OkHttpExtensionsKt {
public static final fun okHttpCallFactory (Lcom/apollographql/apollo3/ApolloClient$Builder;Lkotlin/jvm/functions/Function0;)Lcom/apollographql/apollo3/ApolloClient$Builder;
public static final fun okHttpCallFactory (Lcom/apollographql/apollo3/ApolloClient$Builder;Lokhttp3/Call$Factory;)Lcom/apollographql/apollo3/ApolloClient$Builder;
public static final fun okHttpCallFactory (Lcom/apollographql/apollo3/network/http/HttpNetworkTransport$Builder;Lokhttp3/Call$Factory;)Lcom/apollographql/apollo3/network/http/HttpNetworkTransport$Builder;
public static final fun okHttpClient (Lcom/apollographql/apollo3/ApolloClient$Builder;Lokhttp3/OkHttpClient;)Lcom/apollographql/apollo3/ApolloClient$Builder;
Expand Down Expand Up @@ -220,6 +221,7 @@ public final class com/apollographql/apollo3/network/http/BatchingHttpIntercepto
public final class com/apollographql/apollo3/network/http/DefaultHttpEngine {
public static final fun DefaultHttpEngine (J)Lcom/apollographql/apollo3/network/http/HttpEngine;
public static final fun DefaultHttpEngine (JJ)Lcom/apollographql/apollo3/network/http/HttpEngine;
public static final fun DefaultHttpEngine (Lkotlin/jvm/functions/Function0;)Lcom/apollographql/apollo3/network/http/HttpEngine;
public static final fun DefaultHttpEngine (Lokhttp3/Call$Factory;)Lcom/apollographql/apollo3/network/http/HttpEngine;
public static final fun DefaultHttpEngine (Lokhttp3/OkHttpClient;)Lcom/apollographql/apollo3/network/http/HttpEngine;
public static synthetic fun DefaultHttpEngine$default (JILjava/lang/Object;)Lcom/apollographql/apollo3/network/http/HttpEngine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ fun ApolloClient.Builder.okHttpCallFactory(callFactory: Call.Factory) = apply {
httpEngine(DefaultHttpEngine(callFactory))
}

/**
* Configures the [ApolloClient] to use the lazily initialized [callFactory] for network requests.
*/
fun ApolloClient.Builder.okHttpCallFactory(callFactory: () -> Call.Factory) = apply {
httpEngine(DefaultHttpEngine(callFactory))
}

/**
* Configures the [HttpNetworkTransport] to use the [DefaultHttpEngine] for network requests.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ actual fun DefaultHttpEngine(timeoutMillis: Long): HttpEngine = JvmHttpEngine(ti

fun DefaultHttpEngine(httpCallFactory: Call.Factory): HttpEngine = JvmHttpEngine(httpCallFactory)

fun DefaultHttpEngine(httpCallFactory: () -> Call.Factory): HttpEngine = JvmHttpEngine(httpCallFactory)

fun DefaultHttpEngine(okHttpClient: OkHttpClient): HttpEngine = JvmHttpEngine(okHttpClient)

fun DefaultHttpEngine(connectTimeoutMillis: Long, readTimeoutMillis: Long): HttpEngine = JvmHttpEngine(connectTimeoutMillis, readTimeoutMillis)

private class JvmHttpEngine(
private val httpCallFactory: Call.Factory,
private val httpCallFactory: () -> Call.Factory,
) : HttpEngine {
private val callFactory by lazy { httpCallFactory() }

// an overload that takes an OkHttpClient for easier discovery
constructor(okHttpClient: OkHttpClient) : this(okHttpClient as Call.Factory)
constructor(httpCallFactory: Call.Factory) : this({ httpCallFactory })

constructor(timeoutMillis: Long) : this(timeoutMillis, timeoutMillis)

Expand All @@ -47,7 +49,7 @@ private class JvmHttpEngine(
)

override suspend fun execute(request: HttpRequest): HttpResponse {
return httpCallFactory.execute(request.toOkHttpRequest()).toApolloHttpResponse()
return callFactory.execute(request.toOkHttpRequest()).toApolloHttpResponse()
}

override fun close() {
Expand Down
Loading