startBatchUpload method
Starts an upload of events. Responds appropriately if successful or not. If not, lets the respondant know if the task should be retried or not based on the response.
- Parameters:
- key: The write key the events are assocaited with.
- batch: The array of the events, considered a batch of events.
- completion: The closure executed when done. Passes if the task should be retried or not if failed.
Implementation
Future<bool> startBatchUpload(String writeKey, List<RawEvent> batch,
{String? host}) async {
final apihost = _analytics.target!.state.configuration.state.apiHost ??
host ??
defaultAPIHost;
Uri uploadURL = _url(apihost, "/batch");
try {
DateTime now = DateTime.now().toUtc();
String formattedTimestamp =
DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(now);
var urlRequest = _configuredRequest(uploadURL, "POST",
body: jsonEncode({
"batch": batch.map((e) => e.toJson()).toList(),
"sentAt": formattedTimestamp,
"writeKey": _analytics.target!.state.configuration.state.writeKey,
}));
var f = urlRequest.send().then(http.Response.fromStream);
var response = await f;
if (response.statusCode < 300) {
return true;
} else if (response.statusCode < 400) {
reportInternalError(NetworkUnexpectedHTTPCode(response.statusCode),
analytics: _analytics.target);
return false;
} else if (response.statusCode == 429) {
reportInternalError(NetworkServerLimited(response.statusCode),
analytics: _analytics.target);
return false;
} else {
reportInternalError(NetworkServerRejected(response.statusCode),
analytics: _analytics.target);
return false;
}
} catch (error) {
log("Error uploading request ${error.toString()}",
kind: LogFilterKind.error);
return false;
}
}