dev
dart 64 lines 1.86 KB
Raw
1 import 'dart:convert';
2 import 'package:cake_wallet/core/address_resolver/mastodon/mastodon_user.dart';
3 import 'package:cw_core/utils/proxy_wrapper.dart';
4 import 'package:cw_core/utils/print_verbose.dart';
5
6 class MastodonAPI {
7 static const httpsScheme = 'https';
8 static const userPath = '/api/v1/accounts/lookup';
9 static const statusesPath = '/api/v1/accounts/:id/statuses';
10
11 static Future<MastodonUser?> lookupUserByUserName(
12 {required String userName, required String apiHost}) async {
13 try {
14 final queryParams = {'acct': userName};
15
16 final uri = Uri(
17 scheme: httpsScheme,
18 host: apiHost,
19 path: userPath,
20 queryParameters: queryParams,
21 );
22
23 final response = await ProxyWrapper().get(clearnetUri: uri);
24
25 if (response.statusCode != 200) return null;
26
27 final Map<String, dynamic> responseJSON = json.decode(response.body) as Map<String, dynamic>;
28
29 return MastodonUser.fromJson(responseJSON);
30 } catch (e) {
31 printV('Error in lookupUserByUserName: $e');
32 return null;
33 }
34 }
35
36 static Future<List<PinnedPost>> getPinnedPosts({
37 required String userId,
38 required String apiHost,
39 }) async {
40 try {
41 final queryParams = {'pinned': 'true'};
42
43 final uri = Uri(
44 scheme: httpsScheme,
45 host: apiHost,
46 path: statusesPath.replaceAll(':id', userId),
47 queryParameters: queryParams,
48 );
49
50 final response = await ProxyWrapper().get(clearnetUri: uri);
51
52 if (response.statusCode != 200) {
53 throw Exception('Unexpected HTTP status: ${response.statusCode}');
54 }
55
56 final List<dynamic> responseJSON = json.decode(response.body) as List<dynamic>;
57
58 return responseJSON.map((json) => PinnedPost.fromJson(json as Map<String, dynamic>)).toList();
59 } catch (e) {
60 printV('Error in getPinnedPosts: $e');
61 throw e;
62 }
63 }
64 }