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';
|
2019-04-22 17:16:26 +00:00
|
|
|
import 'package:meta/meta.dart';
|
|
|
|
|
|
|
|
/// API Request model
|
|
|
|
///
|
|
|
|
/// Contains all the information associated to an API request
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
|
|
|
class APIRequest {
|
|
|
|
final String uri;
|
|
|
|
final bool needLogin;
|
|
|
|
Map<String, String> args;
|
2019-04-25 18:14:19 +00:00
|
|
|
Map<String, File> files = Map();
|
2019-04-22 17:16:26 +00:00
|
|
|
|
2019-04-23 12:35:41 +00:00
|
|
|
APIRequest({@required this.uri, this.needLogin = false, this.args})
|
|
|
|
: assert(uri != null),
|
|
|
|
assert(needLogin != null) {
|
|
|
|
if (this.args == null) this.args = Map();
|
|
|
|
}
|
2019-04-22 17:16:26 +00:00
|
|
|
|
2020-04-16 17:16:44 +00:00
|
|
|
APIRequest addString(String name, String value) {
|
|
|
|
args[name] = value;
|
|
|
|
return this;
|
|
|
|
}
|
2019-04-22 17:16:26 +00:00
|
|
|
|
2020-04-16 17:16:44 +00:00
|
|
|
APIRequest addInt(String name, int value) {
|
|
|
|
args[name] = value.toString();
|
|
|
|
return this;
|
|
|
|
}
|
2019-04-22 17:16:26 +00:00
|
|
|
|
2020-04-16 17:16:44 +00:00
|
|
|
APIRequest addBool(String name, bool value) {
|
|
|
|
args[name] = value ? "true" : "false";
|
|
|
|
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
|
|
|
|
2020-04-15 12:31:45 +00:00
|
|
|
void addArgs(Map<String, String> newArgs) => args.addAll(newArgs);
|
|
|
|
|
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
|
|
|
|
|
|
|
/// Execute the request with files
|
|
|
|
Future<APIResponse> execWithFiles() async => APIHelper().execWithFiles(this);
|
2019-04-22 17:16:26 +00:00
|
|
|
}
|