Skip to content

Commit dbe166f

Browse files
committed
api: Have LiveApiConnection instance use a persistent http.Client
As Alex points out in discussion: https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/flutter.3A.20http.20.60Client.60/near/1545511 > Looks like doing so will allow connection reuse, which is a great > performance boost, particularly with HTTP/2. So that seems good. This will also help with file uploads (zulip#56, zulip#57, and zulip#61) because http.Client has a `send` method -- https://pub.dev/documentation/http/latest/http/Client/send.html -- that we can use for requests we want more control over (in particular, a file-upload request), and a counterpart toplevel convenience function like `http.send` isn't offered. See doc: https://pub.dev/documentation/http/latest/http/Client-class.html
1 parent 381257d commit dbe166f

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

lib/api/core.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ Map<String, String> authHeader(Auth auth) {
5151
class LiveApiConnection extends ApiConnection {
5252
LiveApiConnection({required super.auth});
5353

54+
final http.Client _client = http.Client();
55+
5456
bool _isOpen = true;
5557

5658
@override
5759
void close() {
5860
assert(_isOpen);
61+
_client.close();
5962
_isOpen = false;
6063
}
6164

@@ -73,7 +76,7 @@ class LiveApiConnection extends ApiConnection {
7376
path: "/api/v1/$route",
7477
queryParameters: encodeParameters(params));
7578
if (kDebugMode) print("GET $url");
76-
final response = await http.get(url, headers: _headers());
79+
final response = await _client.get(url, headers: _headers());
7780
if (response.statusCode != 200) {
7881
throw Exception("error on GET $route: status ${response.statusCode}");
7982
}
@@ -83,7 +86,7 @@ class LiveApiConnection extends ApiConnection {
8386
@override
8487
Future<String> post(String route, Map<String, dynamic>? params) async {
8588
assert(_isOpen);
86-
final response = await http.post(
89+
final response = await _client.post(
8790
Uri.parse("${auth.realmUrl}/api/v1/$route"),
8891
headers: _headers(),
8992
body: encodeParameters(params));

0 commit comments

Comments
 (0)