Skip to content

Commit

Permalink
fix: use platform-specific HTTP client (fixes go-vikunja#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
denysvitali committed Mar 26, 2024
1 parent facadb5 commit 3025e10
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
27 changes: 23 additions & 4 deletions lib/api/client.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'dart:convert';
import 'dart:core';
import 'dart:io';
import 'package:cronet_http/cronet_http.dart' as cronet_http;
import 'package:cupertino_http/cupertino_http.dart' as cupertino_http;
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:http/io_client.dart' as io_client;
import 'package:vikunja_app/api/response.dart';
import 'package:vikunja_app/components/string_extension.dart';
import 'package:vikunja_app/global.dart';
Expand Down Expand Up @@ -33,6 +36,22 @@ class Client {
configure(token: token, base: base, authenticated: authenticated);
}

http.Client get httpClient {
if (Platform.isAndroid) {
final engine = cronet_http.CronetEngine.build(
cacheMode: cronet_http.CacheMode.memory, cacheMaxSize: 1000000);
return cronet_http.CronetClient.fromCronetEngine(engine);
}
if (Platform.isIOS || Platform.isMacOS) {
final config =
cupertino_http.URLSessionConfiguration.ephemeralSessionConfiguration()
..cache =
cupertino_http.URLCache.withCapacity(memoryCapacity: 1000000);
return cupertino_http.CupertinoClient.fromSessionConfiguration(config);
}
return io_client.IOClient();
}

void reloadIgnoreCerts(bool? val) {
ignoreCertificates = val ?? false;
HttpOverrides.global = new IgnoreCertHttpOverrides(ignoreCertificates);
Expand Down Expand Up @@ -84,14 +103,14 @@ class Client {
queryParameters: queryParameters,
fragment: uri.fragment);

return http
return httpClient
.get(uri, headers: _headers)
.then(_handleResponse)
.onError((error, stackTrace) => _handleError(error, stackTrace));
}

Future<Response?> delete(String url) {
return http
return httpClient
.delete(
'${this.base}$url'.toUri()!,
headers: _headers,
Expand All @@ -101,7 +120,7 @@ class Client {
}

Future<Response?> post(String url, {dynamic body}) {
return http
return httpClient
.post(
'${this.base}$url'.toUri()!,
headers: _headers,
Expand All @@ -112,7 +131,7 @@ class Client {
}

Future<Response?> put(String url, {dynamic body}) {
return http
return httpClient
.put(
'${this.base}$url'.toUri()!,
headers: _headers,
Expand Down
32 changes: 32 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.7.2"
cronet_http:
dependency: "direct main"
description:
name: cronet_http
sha256: "9b9f00ae48971bc8a8cbdd4528bd35511adce00fb79d1ebf9f9907667056640f"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
crypto:
dependency: transitive
description:
Expand All @@ -185,6 +193,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.0"
cupertino_http:
dependency: "direct main"
description:
name: cupertino_http
sha256: "20c167fd843c9ff6fc25cc4a0e8efa4180dfe119fb6d18c3c55104113e9cfc6f"
url: "https://pub.dev"
source: hosted
version: "1.4.0"
cupertino_icons:
dependency: "direct main"
description:
Expand Down Expand Up @@ -592,6 +608,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.4"
jni:
dependency: transitive
description:
name: jni
sha256: "499558e919997adfc45809a66caf0b95b91393e23289dd2826b152f8f04e6611"
url: "https://pub.dev"
source: hosted
version: "0.7.3"
js:
dependency: transitive
description:
Expand Down Expand Up @@ -1341,6 +1365,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.5.1"
web_socket:
dependency: transitive
description:
name: web_socket
sha256: "3f81fde6fbc799d03c0fb3f2c3ac84368ee267012a4beb876685c029946da4e0"
url: "https://pub.dev"
source: hosted
version: "0.1.0"
web_socket_channel:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ dependencies:
timezone: ^0.9.2
json_annotation: ^4.8.1
collection: ^1.18.0
cupertino_http: ^1.4.0
cronet_http: ^1.2.0
dev_dependencies:
flutter_test:
sdk: flutter
Expand Down

0 comments on commit 3025e10

Please sign in to comment.