Skip to content

Commit

Permalink
api [nfc]: Push jsonDecode inside ApiConnection.send
Browse files Browse the repository at this point in the history
  • Loading branch information
gnprice committed May 19, 2023
1 parent b868df1 commit 99c8d10
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 28 deletions.
12 changes: 7 additions & 5 deletions lib/api/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ class ApiConnection {

bool _isOpen = true;

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

void close() {
Expand All @@ -65,15 +67,15 @@ class ApiConnection {
_isOpen = false;
}

Future<String> get(String route, Map<String, dynamic>? params) async {
Future<Map<String, dynamic>> get(String route, Map<String, dynamic>? params) async {
final url = realmUrl.replace(
path: "/api/v1/$route", queryParameters: encodeParameters(params));
assert(debugLog("GET $url"));
final request = http.Request('GET', url);
return send(request);
}

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

Future<String> postFileFromStream(String route, Stream<List<int>> content, int length, { String? filename }) async {
Future<Map<String, dynamic>> postFileFromStream(String route, Stream<List<int>> content, int length, { String? filename }) async {
http.MultipartRequest request = http.MultipartRequest('POST', Uri.parse("$realmUrl/api/v1/$route"))
..files.add(http.MultipartFile('file', content, length, filename: filename));
return send(request);
Expand Down
7 changes: 2 additions & 5 deletions lib/api/route/account.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

import '../core.dart';
Expand All @@ -12,7 +10,7 @@ Future<FetchApiKeyResult> fetchApiKey({
required String username,
required String password,
}) async {
final String data;
final Map<String, dynamic> data;
// TODO make this function testable by taking ApiConnection from caller
final connection = ApiConnection.live(realmUrl: realmUrl);
try {
Expand All @@ -24,8 +22,7 @@ Future<FetchApiKeyResult> fetchApiKey({
connection.close();
}

final json = jsonDecode(data);
return FetchApiKeyResult.fromJson(json);
return FetchApiKeyResult.fromJson(data);
}

@JsonSerializable(fieldRename: FieldRename.snake)
Expand Down
7 changes: 2 additions & 5 deletions lib/api/route/events.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

import '../core.dart';
Expand All @@ -21,8 +19,7 @@ Future<InitialSnapshot> registerQueue(ApiConnection connection) async {
'user_settings_object': true,
},
});
final json = jsonDecode(data);
return InitialSnapshot.fromJson(json);
return InitialSnapshot.fromJson(data);
}

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

@JsonSerializable(fieldRename: FieldRename.snake)
Expand Down
8 changes: 3 additions & 5 deletions lib/api/route/messages.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

import '../core.dart';
Expand All @@ -18,7 +16,7 @@ Future<GetMessagesResult> getMessages(ApiConnection connection, {
'num_before': numBefore,
'num_after': numAfter,
});
return GetMessagesResult.fromJson(jsonDecode(data));
return GetMessagesResult.fromJson(data);
}

@JsonSerializable(fieldRename: FieldRename.snake)
Expand Down Expand Up @@ -79,7 +77,7 @@ Future<SendMessageResult> sendMessage(
'topic': RawParameter(topic),
'content': RawParameter(content),
});
return SendMessageResult.fromJson(jsonDecode(data));
return SendMessageResult.fromJson(data);
}

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

@JsonSerializable(fieldRename: FieldRename.snake)
Expand Down
7 changes: 2 additions & 5 deletions lib/api/route/realm.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

import '../core.dart';
Expand All @@ -20,7 +18,7 @@ part 'realm.g.dart';
Future<GetServerSettingsResult> getServerSettings({
required Uri realmUrl,
}) async {
final String data;
final Map<String, dynamic> data;
// TODO make this function testable by taking ApiConnection from caller
final connection = ApiConnection.live(realmUrl: realmUrl);
try {
Expand All @@ -29,8 +27,7 @@ Future<GetServerSettingsResult> getServerSettings({
connection.close();
}

final json = jsonDecode(data);
return GetServerSettingsResult.fromJson(json);
return GetServerSettingsResult.fromJson(data);
}

@JsonSerializable(fieldRename: FieldRename.snake)
Expand Down
4 changes: 1 addition & 3 deletions lib/api/route/users.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

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

@JsonSerializable(fieldRename: FieldRename.snake)
Expand Down

0 comments on commit 99c8d10

Please sign in to comment.