Start to build synchronization logic
This commit is contained in:
43
moneymgr_mobile/lib/services/api/files_api.dart
Normal file
43
moneymgr_mobile/lib/services/api/files_api.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:http_parser/http_parser.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:moneymgr_mobile/services/api/api_client.dart';
|
||||
|
||||
part 'files_api.freezed.dart';
|
||||
part 'files_api.g.dart';
|
||||
|
||||
@freezed
|
||||
abstract class UploadResult with _$UploadResult {
|
||||
const factory UploadResult({required int id}) = _UploadResult;
|
||||
|
||||
factory UploadResult.fromJson(Map<String, dynamic> json) =>
|
||||
_$UploadResultFromJson(json);
|
||||
}
|
||||
|
||||
extension FilesApi on ApiClient {
|
||||
/// Upload a file
|
||||
Future<UploadResult> uploadFile({
|
||||
required String filename,
|
||||
required String mimeType,
|
||||
required Uint8List bytes,
|
||||
}) async {
|
||||
final res = await execute(
|
||||
"/file",
|
||||
method: "POST",
|
||||
data: FormData.fromMap({
|
||||
"file": MultipartFile.fromBytes(
|
||||
bytes,
|
||||
filename: filename,
|
||||
contentType: MediaType.parse(mimeType),
|
||||
),
|
||||
}),
|
||||
);
|
||||
|
||||
Logger.root.fine("Successfully uploaded file with response=${res.data}");
|
||||
|
||||
return UploadResult.fromJson(res.data);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user