Skip to content

Commit 582a60b

Browse files
committed
api: Add get-messages route
1 parent cf0e744 commit 582a60b

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

lib/api/core.dart

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:convert';
22

3+
import 'package:flutter/foundation.dart';
34
import 'package:http/http.dart' as http;
45

56
abstract class Auth {
@@ -23,13 +24,30 @@ class ApiConnection {
2324
};
2425
}
2526

27+
Future<String> get(String route, Map<String, dynamic>? params) async {
28+
final baseUrl = Uri.parse(auth.realmUrl);
29+
final url = Uri(
30+
scheme: baseUrl.scheme,
31+
userInfo: baseUrl.userInfo,
32+
host: baseUrl.host,
33+
port: baseUrl.port,
34+
path: "/api/v1/$route",
35+
queryParameters: params?.map((k, v) => MapEntry(k, jsonEncode(v))));
36+
if (kDebugMode) print("GET $url");
37+
final response = await http.get(url, headers: _headers());
38+
if (response.statusCode != 200) {
39+
throw Exception("error on GET $route: status ${response.statusCode}");
40+
}
41+
return response.body;
42+
}
43+
2644
Future<String> post(String route, Map<String, dynamic>? params) async {
2745
final response = await http.post(
2846
Uri.parse("${auth.realmUrl}/api/v1/$route"),
2947
headers: _headers(),
3048
body: params?.map((k, v) => MapEntry(k, jsonEncode(v))));
3149
if (response.statusCode != 200) {
32-
throw Exception("error on $route: status ${response.statusCode}");
50+
throw Exception("error on POST $route: status ${response.statusCode}");
3351
}
3452
return response.body;
3553
}

lib/api/model/model.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ abstract class Message {
156156
if (type == 'private') return PmMessage.fromJson(json);
157157
throw Exception("Message.fromJson: unexpected message type $type");
158158
}
159+
160+
Map<String, dynamic> toJson();
159161
}
160162

161163
@JsonSerializable()
@@ -187,6 +189,9 @@ class StreamMessage extends Message {
187189

188190
factory StreamMessage.fromJson(Map<String, dynamic> json) =>
189191
_$StreamMessageFromJson(json);
192+
193+
@override
194+
Map<String, dynamic> toJson() => _$StreamMessageToJson(this);
190195
}
191196

192197
@JsonSerializable()
@@ -231,4 +236,7 @@ class PmMessage extends Message {
231236

232237
factory PmMessage.fromJson(Map<String, dynamic> json) =>
233238
_$PmMessageFromJson(json);
239+
240+
@override
241+
Map<String, dynamic> toJson() => _$PmMessageToJson(this);
234242
}

lib/api/route/messages.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// ignore_for_file: non_constant_identifier_names
2+
3+
import 'dart:convert';
4+
5+
import 'package:json_annotation/json_annotation.dart';
6+
7+
import '../core.dart';
8+
import '../model/model.dart';
9+
10+
part 'messages.g.dart';
11+
12+
/// https://zulip.com/api/get-messages
13+
Future<GetMessagesResult> getMessages(ApiConnection connection, {
14+
required int num_before,
15+
required int num_after,
16+
}) async {
17+
final data = await connection.get('messages', {
18+
// 'narrow': [], // TODO parametrize
19+
'anchor': 999999999, // TODO parametrize; needs raw for strings
20+
'num_before': num_before,
21+
'num_after': num_after,
22+
});
23+
return GetMessagesResult.fromJson(jsonDecode(data));
24+
}
25+
26+
@JsonSerializable()
27+
class GetMessagesResult {
28+
final int anchor;
29+
final bool found_newest;
30+
final bool found_oldest;
31+
final bool found_anchor;
32+
final bool history_limited;
33+
final List<Message> messages;
34+
35+
GetMessagesResult({
36+
required this.anchor,
37+
required this.found_newest,
38+
required this.found_oldest,
39+
required this.found_anchor,
40+
required this.history_limited,
41+
required this.messages,
42+
});
43+
44+
factory GetMessagesResult.fromJson(Map<String, dynamic> json) =>
45+
_$GetMessagesResultFromJson(json);
46+
}

lib/api/route/messages.g.dart

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)