Skip to content

Commit

Permalink
add caching configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Anikate-De committed Oct 3, 2024
1 parent 828257a commit f21d51a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
4 changes: 3 additions & 1 deletion pkgs/ok_http/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import 'book.dart';
void main() {
final Client httpClient;
if (Platform.isAndroid) {
httpClient = OkHttpClient();
httpClient = OkHttpClient(
configuration:
const OkHttpClientConfiguration(cacheSize: 10 * 1024 * 1024));
} else {
httpClient = IOClient(HttpClient()..userAgent = 'Book Agent');
}
Expand Down
30 changes: 26 additions & 4 deletions pkgs/ok_http/lib/src/ok_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ class OkHttpClientConfiguration {
/// See [OkHttpClient.Builder.writeTimeout](https://square.github.io/okhttp/5.x/okhttp/okhttp3/-ok-http-client/-builder/write-timeout.html).
final Duration writeTimeout;

/// TODO: Some documentation.
final int? cacheSize;

const OkHttpClientConfiguration({
this.callTimeout = Duration.zero,
this.connectTimeout = const Duration(milliseconds: 10000),
this.readTimeout = const Duration(milliseconds: 10000),
this.writeTimeout = const Duration(milliseconds: 10000),
this.cacheSize,
});
}

Expand Down Expand Up @@ -92,7 +96,14 @@ class OkHttpClient extends BaseClient {
OkHttpClient({
this.configuration = const OkHttpClientConfiguration(),
}) {
_client = bindings.OkHttpClient.new1();
if (configuration.cacheSize != null) {
_client = bindings.OkHttpClient_Builder.new()
.cache(bindings.Cache.new1(
bindings.File("".toJString()), configuration.cacheSize!))
.build();
} else {
_client = bindings.OkHttpClient.new1();
}
}

@override
Expand Down Expand Up @@ -198,7 +209,7 @@ class OkHttpClient extends BaseClient {
// `followRedirects` is set to `false` to handle redirects manually.
// (Since OkHttp sets a hard limit of 20 redirects.)
// https://github.com/square/okhttp/blob/54238b4c713080c3fd32fb1a070fb5d6814c9a09/okhttp/src/main/kotlin/okhttp3/internal/http/RetryAndFollowUpInterceptor.kt#L350
final reqConfiguredClient = bindings.RedirectInterceptor.Companion
final reqConfiguredClientBuilder = bindings.RedirectInterceptor.Companion
.addRedirectInterceptor(
_client.newBuilder().followRedirects(false),
maxRedirects,
Expand All @@ -222,15 +233,26 @@ class OkHttpClient extends BaseClient {
.readTimeout(configuration.readTimeout.inMilliseconds,
bindings.TimeUnit.MILLISECONDS)
.writeTimeout(configuration.writeTimeout.inMilliseconds,
bindings.TimeUnit.MILLISECONDS)
.build();
bindings.TimeUnit.MILLISECONDS);

final reqConfiguredClient = reqConfiguredClientBuilder.build();

// `enqueue()` schedules the request to be executed in the future.
// https://square.github.io/okhttp/5.x/okhttp/okhttp3/-call/enqueue.html
reqConfiguredClient
.newCall(reqBuilder.build())
.enqueue(bindings.Callback.implement(bindings.$CallbackImpl(
onResponse: (bindings.Call call, bindings.Response response) {
var cacheResponse = response.cacheResponse();
if (!cacheResponse.isNull) {
print('Cache response: ${cacheResponse.toString()}');
}

var networkResponse = response.networkResponse();
if (!networkResponse.isNull) {
print('Network response: ${networkResponse.toString()}');
}

var reader = bindings.AsyncInputStreamReader();
var respBodyStreamController = StreamController<List<int>>();

Expand Down

0 comments on commit f21d51a

Please sign in to comment.