2019-04-25 18:14:19 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2019-04-23 12:35:41 +00:00
|
|
|
import 'package:comunic/helpers/api_helper.dart';
|
|
|
|
import 'package:comunic/models/api_response.dart';
|
2021-03-13 08:00:00 +00:00
|
|
|
import 'package:dio/dio.dart';
|
2020-04-24 11:35:05 +00:00
|
|
|
import 'package:http_parser/http_parser.dart';
|
2019-04-22 17:16:26 +00:00
|
|
|
|
|
|
|
/// API Request model
|
|
|
|
///
|
|
|
|
/// Contains all the information associated to an API request
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
2020-04-17 06:44:57 +00:00
|
|
|
class BytesFile {
|
|
|
|
final String filename;
|
2022-03-10 18:39:57 +00:00
|
|
|
final List<int>? bytes;
|
|
|
|
final MediaType? type;
|
2020-04-17 06:44:57 +00:00
|
|
|
|
2020-04-24 11:35:05 +00:00
|
|
|
const BytesFile(
|
|
|
|
this.filename,
|
|
|
|
this.bytes, {
|
|
|
|
this.type,
|
|
|
|
});
|
2020-04-17 06:44:57 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 17:16:26 +00:00
|
|
|
class APIRequest {
|
2022-03-10 18:39:57 +00:00
|
|
|
final String uri;
|
|
|
|
final bool needLogin;
|
|
|
|
ProgressCallback? progressCallback;
|
|
|
|
CancelToken? cancelToken;
|
|
|
|
Map<String, String?>? args;
|
2019-04-25 18:14:19 +00:00
|
|
|
Map<String, File> files = Map();
|
2022-03-10 18:39:57 +00:00
|
|
|
Map<String, BytesFile?> bytesFiles = Map();
|
2019-04-22 17:16:26 +00:00
|
|
|
|
2022-03-11 15:27:01 +00:00
|
|
|
APIRequest({required this.uri, this.needLogin = false, this.args}) {
|
2019-04-23 12:35:41 +00:00
|
|
|
if (this.args == null) this.args = Map();
|
|
|
|
}
|
2019-04-22 17:16:26 +00:00
|
|
|
|
2022-03-11 15:27:01 +00:00
|
|
|
APIRequest.withLogin(this.uri, {this.args}) : needLogin = true {
|
2020-05-02 11:59:40 +00:00
|
|
|
if (args == null) this.args = Map();
|
|
|
|
}
|
|
|
|
|
2022-03-11 15:27:01 +00:00
|
|
|
APIRequest.withoutLogin(this.uri, {this.args}) : needLogin = false {
|
2020-05-03 12:22:06 +00:00
|
|
|
if (args == null) this.args = Map();
|
|
|
|
}
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
APIRequest addString(String name, String? value) {
|
|
|
|
args![name] = value;
|
2020-04-16 17:16:44 +00:00
|
|
|
return this;
|
|
|
|
}
|
2019-04-22 17:16:26 +00:00
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
APIRequest addInt(String name, int? value) {
|
|
|
|
args![name] = value.toString();
|
2020-04-16 17:16:44 +00:00
|
|
|
return this;
|
|
|
|
}
|
2019-04-22 17:16:26 +00:00
|
|
|
|
2020-04-16 17:16:44 +00:00
|
|
|
APIRequest addBool(String name, bool value) {
|
2022-03-10 18:39:57 +00:00
|
|
|
args![name] = value ? "true" : "false";
|
2020-04-16 17:16:44 +00:00
|
|
|
return this;
|
|
|
|
}
|
2019-04-23 12:35:41 +00:00
|
|
|
|
2020-04-16 17:16:44 +00:00
|
|
|
APIRequest addFile(String name, File file) {
|
|
|
|
files[name] = file;
|
|
|
|
return this;
|
|
|
|
}
|
2019-04-25 18:14:19 +00:00
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
APIRequest addBytesFile(String name, BytesFile? file) {
|
2020-04-17 06:44:57 +00:00
|
|
|
this.bytesFiles[name] = file;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
void addArgs(Map<String, String> newArgs) => args!.addAll(newArgs);
|
2020-04-15 12:31:45 +00:00
|
|
|
|
2019-04-23 12:35:41 +00:00
|
|
|
/// Execute the request
|
|
|
|
Future<APIResponse> exec() async => APIHelper().exec(this);
|
2019-04-25 18:14:19 +00:00
|
|
|
|
2020-04-30 11:32:22 +00:00
|
|
|
/// Execute the request, throws an exception in case of failure
|
|
|
|
Future<APIResponse> execWithThrow() async => (await exec()).assertOk();
|
|
|
|
|
2021-02-18 17:58:47 +00:00
|
|
|
/// Execute the request, throws an exception in case of failure
|
|
|
|
Future<Map<String, dynamic>> execWithThrowGetObject() async =>
|
|
|
|
(await execWithThrow()).getObject();
|
|
|
|
|
2019-04-25 18:14:19 +00:00
|
|
|
/// Execute the request with files
|
|
|
|
Future<APIResponse> execWithFiles() async => APIHelper().execWithFiles(this);
|
2020-05-02 06:51:34 +00:00
|
|
|
|
|
|
|
/// Execute the request with files to send & throws in case of failure
|
|
|
|
Future<APIResponse> execWithFilesAndThrow() async =>
|
|
|
|
(await execWithFiles()).assertOk();
|
2019-04-22 17:16:26 +00:00
|
|
|
}
|