upload method
Uploads data to the server.
This method uploads data (typically files) to the server. Progress can be
tracked using the optional onProgress callback.
Parameters:
request: The request configuration for the upload, including the data to uploadonProgress: Optional callback to track upload progress
Returns a Future that completes with an AdapterResponse when the upload is complete.
Throws AdapterException if the upload fails.
Example:
final request = AdapterRequest(
baseUrl: 'https://api.example.com',
path: '/upload',
method: HttpMethod.post,
bodyParams: {'file': File('/path/to/file.jpg')},
);
await adapter.upload(
request,
onProgress: (count, total) {
print('Uploaded: ${(count / total * 100).toStringAsFixed(1)}%');
},
);
Implementation
@override
Future<AdapterResponse> upload(
AdapterRequest request, {
ProgressCallback? onProgress,
}) async {
// 记录请求历史
_requestHistory.add(request);
// 创建 Completer 用于取消
final completer = Completer<AdapterResponse>();
// 如果有 cancelToken,注册取消回调
if (request.cancelToken != null) {
_registerCancelToken(request.cancelToken!, completer);
// 如果已经取消,立即抛出异常
if (request.cancelToken!.isCancelled) {
throw AdapterException(
type: AdapterExceptionType.cancel,
message: request.cancelToken!.cancelReason ?? 'Request cancelled',
);
}
}
try {
// 获取模拟配置
final config = _mockConfigs[request.path];
// 应用延迟(支持取消)
if (config?.delay != null) {
try {
await Future.any([
Future.delayed(config!.delay!),
completer.future,
]);
// 检查是否被取消
if (completer.isCompleted) {
throw await completer.future;
}
} catch (e) {
_unregisterCancelToken(request.cancelToken, completer);
rethrow;
}
}
// 模拟进度回调
if (onProgress != null) {
final total = 1024;
for (var i = 0; i <= 10; i++) {
await Future.delayed(const Duration(milliseconds: 10));
onProgress((i * total / 10).toInt(), total);
}
}
// 如果配置了错误,抛出错误
if (config?.error != null) {
_unregisterCancelToken(request.cancelToken, completer);
throw config!.error!;
}
// 返回模拟响应
_unregisterCancelToken(request.cancelToken, completer);
return config?.response ?? _createDefaultResponse(request);
} catch (e) {
_unregisterCancelToken(request.cancelToken, completer);
rethrow;
}
}