upload method
Batch upload the file
containing a list of DataPoints to the CARP
backend.
The file
can be created using a FileDataManager
in carp_mobile_sensing
.
Note that the file should be raw JSON, and hence not zipped.
Returns when successful. Throws a CarpServiceException if not.
Implementation
Future<void> upload(File file) async {
final String url = "$dataEndpointUri/batch";
var request = http.MultipartRequest("POST", Uri.parse(url));
request.headers['Authorization'] = headers['Authorization']!;
request.headers['Content-Type'] = 'multipart/form-data';
request.headers['cache-control'] = 'no-cache';
request.files.add(ClonableMultipartFile.fromFileSync(file.path));
// sending the request using the retry approach
httpr.send(request).then((response) async {
final int httpStatusCode = response.statusCode;
// CARP web service returns 200 or 201 when a file is uploaded to the server
if ((httpStatusCode == HttpStatus.ok) ||
(httpStatusCode == HttpStatus.created)) return;
// everything else is an exception
response.stream.toStringStream().first.then((body) {
final Map<String, dynamic> responseJson =
json.decode(body) as Map<String, dynamic>;
throw CarpServiceException(
httpStatus: HTTPStatus(httpStatusCode, response.reasonPhrase),
message: responseJson["message"].toString(),
path: responseJson["path"].toString(),
);
});
});
}