Skip to content

Commit efdcf3e

Browse files
committed
api [nfc]: Push jsonDecode inside ApiConnection.send
1 parent c109b17 commit efdcf3e

File tree

6 files changed

+17
-28
lines changed

6 files changed

+17
-28
lines changed

lib/api/core.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,16 @@ class ApiConnection {
4949

5050
bool _isOpen = true;
5151

52-
Future<String> send(http.BaseRequest request) async {
52+
Future<Map<String, dynamic>> send(http.BaseRequest request) async {
5353
assert(_isOpen);
5454
addAuth(request);
5555
final response = await _client.send(request);
5656
if (response.statusCode != 200) {
5757
throw Exception("error on ${request.method} ${request.url.path}: status ${response.statusCode}");
5858
}
59-
return utf8.decode(await response.stream.toBytes());
59+
final bytes = await response.stream.toBytes();
60+
return jsonDecode(utf8.decode(bytes));
61+
// TODO(#37): inspect response to throw structured errors
6062
}
6163

6264
void close() {
@@ -65,15 +67,15 @@ class ApiConnection {
6567
_isOpen = false;
6668
}
6769

68-
Future<String> get(String route, Map<String, dynamic>? params) async {
70+
Future<Map<String, dynamic>> get(String route, Map<String, dynamic>? params) async {
6971
final url = realmUrl.replace(
7072
path: "/api/v1/$route", queryParameters: encodeParameters(params));
7173
assert(debugLog("GET $url"));
7274
final request = http.Request('GET', url);
7375
return send(request);
7476
}
7577

76-
Future<String> post(String route, Map<String, dynamic>? params) async {
78+
Future<Map<String, dynamic>> post(String route, Map<String, dynamic>? params) async {
7779
final url = realmUrl.replace(path: "/api/v1/$route");
7880
final request = http.Request('POST', url);
7981
if (params != null) {
@@ -82,7 +84,7 @@ class ApiConnection {
8284
return send(request);
8385
}
8486

85-
Future<String> postFileFromStream(String route, Stream<List<int>> content, int length, { String? filename }) async {
87+
Future<Map<String, dynamic>> postFileFromStream(String route, Stream<List<int>> content, int length, { String? filename }) async {
8688
http.MultipartRequest request = http.MultipartRequest('POST', Uri.parse("$realmUrl/api/v1/$route"))
8789
..files.add(http.MultipartFile('file', content, length, filename: filename));
8890
return send(request);

lib/api/route/account.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:convert';
2-
31
import 'package:json_annotation/json_annotation.dart';
42

53
import '../core.dart';
@@ -12,7 +10,7 @@ Future<FetchApiKeyResult> fetchApiKey({
1210
required String username,
1311
required String password,
1412
}) async {
15-
final String data;
13+
final Map<String, dynamic> data;
1614
// TODO make this function testable by taking ApiConnection from caller
1715
final connection = ApiConnection.live(realmUrl: realmUrl);
1816
try {
@@ -24,8 +22,7 @@ Future<FetchApiKeyResult> fetchApiKey({
2422
connection.close();
2523
}
2624

27-
final json = jsonDecode(data);
28-
return FetchApiKeyResult.fromJson(json);
25+
return FetchApiKeyResult.fromJson(data);
2926
}
3027

3128
@JsonSerializable(fieldRename: FieldRename.snake)

lib/api/route/events.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:convert';
2-
31
import 'package:json_annotation/json_annotation.dart';
42

53
import '../core.dart';
@@ -21,8 +19,7 @@ Future<InitialSnapshot> registerQueue(ApiConnection connection) async {
2119
'user_settings_object': true,
2220
},
2321
});
24-
final json = jsonDecode(data);
25-
return InitialSnapshot.fromJson(json);
22+
return InitialSnapshot.fromJson(data);
2623
}
2724

2825
/// https://zulip.com/api/get-events
@@ -34,7 +31,7 @@ Future<GetEventsResult> getEvents(ApiConnection connection, {
3431
if (lastEventId != null) 'last_event_id': lastEventId,
3532
if (dontBlock != null) 'dont_block': dontBlock,
3633
});
37-
return GetEventsResult.fromJson(jsonDecode(data));
34+
return GetEventsResult.fromJson(data);
3835
}
3936

4037
@JsonSerializable(fieldRename: FieldRename.snake)

lib/api/route/messages.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:convert';
2-
31
import 'package:json_annotation/json_annotation.dart';
42

53
import '../core.dart';
@@ -18,7 +16,7 @@ Future<GetMessagesResult> getMessages(ApiConnection connection, {
1816
'num_before': numBefore,
1917
'num_after': numAfter,
2018
});
21-
return GetMessagesResult.fromJson(jsonDecode(data));
19+
return GetMessagesResult.fromJson(data);
2220
}
2321

2422
@JsonSerializable(fieldRename: FieldRename.snake)
@@ -79,7 +77,7 @@ Future<SendMessageResult> sendMessage(
7977
'topic': RawParameter(topic),
8078
'content': RawParameter(content),
8179
});
82-
return SendMessageResult.fromJson(jsonDecode(data));
80+
return SendMessageResult.fromJson(data);
8381
}
8482

8583
@JsonSerializable(fieldRename: FieldRename.snake)
@@ -106,7 +104,7 @@ Future<UploadFileResult> uploadFile(
106104
required String filename,
107105
}) async {
108106
final data = await connection.postFileFromStream('user_uploads', content, length, filename: filename);
109-
return UploadFileResult.fromJson(jsonDecode(data));
107+
return UploadFileResult.fromJson(data);
110108
}
111109

112110
@JsonSerializable(fieldRename: FieldRename.snake)

lib/api/route/realm.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:convert';
2-
31
import 'package:json_annotation/json_annotation.dart';
42

53
import '../core.dart';
@@ -20,7 +18,7 @@ part 'realm.g.dart';
2018
Future<GetServerSettingsResult> getServerSettings({
2119
required Uri realmUrl,
2220
}) async {
23-
final String data;
21+
final Map<String, dynamic> data;
2422
// TODO make this function testable by taking ApiConnection from caller
2523
final connection = ApiConnection.live(realmUrl: realmUrl);
2624
try {
@@ -29,8 +27,7 @@ Future<GetServerSettingsResult> getServerSettings({
2927
connection.close();
3028
}
3129

32-
final json = jsonDecode(data);
33-
return GetServerSettingsResult.fromJson(json);
30+
return GetServerSettingsResult.fromJson(data);
3431
}
3532

3633
@JsonSerializable(fieldRename: FieldRename.snake)

lib/api/route/users.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:convert';
2-
31
import 'package:json_annotation/json_annotation.dart';
42

53
import '../core.dart';
@@ -12,7 +10,7 @@ part 'users.g.dart';
1210
/// as a workaround on old servers.
1311
Future<GetOwnUserResult> getOwnUser(ApiConnection connection) async {
1412
final data = await connection.get('users/me', {});
15-
return GetOwnUserResult.fromJson(jsonDecode(data));
13+
return GetOwnUserResult.fromJson(data);
1614
}
1715

1816
@JsonSerializable(fieldRename: FieldRename.snake)

0 commit comments

Comments
 (0)