73 lines
2.2 KiB
Dart
73 lines
2.2 KiB
Dart
|
|
import '../../core/constants/api_endpoints.dart';
|
||
|
|
import '../../core/network/api_response.dart';
|
||
|
|
import '../../core/network/dio_client.dart';
|
||
|
|
import '../models/kline_candle.dart';
|
||
|
|
|
||
|
|
/// K线 REST API 服务
|
||
|
|
class KlineService {
|
||
|
|
final DioClient _client;
|
||
|
|
|
||
|
|
KlineService(this._client);
|
||
|
|
|
||
|
|
/// 获取历史K线数据
|
||
|
|
Future<ApiResponse<List<KlineCandle>>> fetchHistory({
|
||
|
|
required String coinCode,
|
||
|
|
required String interval,
|
||
|
|
int limit = 200,
|
||
|
|
int? before,
|
||
|
|
}) async {
|
||
|
|
final params = <String, dynamic>{
|
||
|
|
'coinCode': coinCode,
|
||
|
|
'interval': interval,
|
||
|
|
'limit': limit,
|
||
|
|
};
|
||
|
|
if (before != null) params['before'] = before;
|
||
|
|
|
||
|
|
final response = await _client.get<Map<String, dynamic>>(
|
||
|
|
ApiEndpoints.klineHistory,
|
||
|
|
queryParameters: params,
|
||
|
|
);
|
||
|
|
|
||
|
|
if (response.success && response.data != null) {
|
||
|
|
final list = response.data!['list'] as List? ?? [];
|
||
|
|
final candles = list
|
||
|
|
.map((e) => KlineCandle.fromHistoryJson(e as Map<String, dynamic>))
|
||
|
|
.toList();
|
||
|
|
return ApiResponse.success(candles, response.message);
|
||
|
|
}
|
||
|
|
return ApiResponse.fail(response.message ?? '获取K线数据失败');
|
||
|
|
}
|
||
|
|
|
||
|
|
/// 获取当前进行中的K线
|
||
|
|
Future<ApiResponse<KlineCandle>> fetchCurrentCandle({
|
||
|
|
required String coinCode,
|
||
|
|
required String interval,
|
||
|
|
}) async {
|
||
|
|
final response = await _client.get<Map<String, dynamic>>(
|
||
|
|
ApiEndpoints.klineCurrent,
|
||
|
|
queryParameters: {'coinCode': coinCode, 'interval': interval},
|
||
|
|
);
|
||
|
|
|
||
|
|
if (response.success && response.data != null) {
|
||
|
|
return ApiResponse.success(
|
||
|
|
KlineCandle.fromJson(response.data!),
|
||
|
|
response.message,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
return ApiResponse.fail(response.message ?? '获取当前K线失败');
|
||
|
|
}
|
||
|
|
|
||
|
|
/// 获取支持的周期列表
|
||
|
|
Future<ApiResponse<List<String>>> fetchIntervals() async {
|
||
|
|
final response = await _client.get<Map<String, dynamic>>(
|
||
|
|
ApiEndpoints.klineIntervals,
|
||
|
|
);
|
||
|
|
|
||
|
|
if (response.success && response.data != null) {
|
||
|
|
final list = response.data!['list'] as List? ?? [];
|
||
|
|
return ApiResponse.success(list.cast<String>(), response.message);
|
||
|
|
}
|
||
|
|
return ApiResponse.fail(response.message ?? '获取周期列表失败');
|
||
|
|
}
|
||
|
|
}
|