-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta_client.dart
More file actions
43 lines (39 loc) · 1.35 KB
/
meta_client.dart
File metadata and controls
43 lines (39 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import '../core/http/misskey_http_client.dart';
import '../core/http/request_options.dart';
import '../models/meta.dart';
/// `/api/meta` を取得するクライアント
class MetaClient {
final MisskeyHttpClient http;
Meta? _cached;
MetaClient(this.http);
/// Misskeyサーバの `/api/meta` エンドポイントからメタ情報を取得する
///
/// [refresh] を `true` にするとキャッシュを無視して常に最新の情報を取得する
/// デフォルトでは、一度取得したメタ情報をキャッシュし、2回目以降はキャッシュを返す
Future<Meta> getMeta({bool refresh = false}) async {
if (!refresh && _cached != null) return _cached!;
final res = await http.send<Map<String, dynamic>>(
'/meta',
method: 'POST',
body: const {},
options: const RequestOptions(authRequired: false, idempotent: true),
);
_cached = Meta.fromJson(res);
return _cached!;
}
/// 簡易なサーバーの能力検出(キー存在で判定)
bool supports(String keyPath) {
final meta = _cached;
if (meta == null) return false;
final parts = keyPath.split('.');
dynamic cursor = meta.raw;
for (final p in parts) {
if (cursor is Map && cursor.containsKey(p)) {
cursor = cursor[p];
} else {
return false;
}
}
return true;
}
}