apiSave method
apiSave saves the record to https://flutster.com API
Implementation
Future<String> apiSave({
List<FlutsterTestEvent>? runEvents,
bool? runResult,
int retries = 20,
}) async {
if (apiKey == null ||
apiUser == null ||
apiUrl == null ||
apiKey!.isEmpty ||
apiUser!.isEmpty ||
apiUrl!.isEmpty) {
return ("API save is only possible if apiKey, apiUser and apiUrl are "
"provided");
}
String? ret;
Map<String, String> headers = {
"v": apiVersion,
};
Map<String, String> data = {
"api": "tr",
};
bool create = false;
if (id == null) {
if (runEvents != null) {
return ("Cannot save run events without a test record id");
}
create = true;
data["tr"] = "createContent";
} else {
if (runEvents != null) {
if (runResult == null) {
return ("Cannot save run without run result");
}
data["tr"] = "createRun";
data["runResult"] = runResult ? "1" : "0";
} else {
data["tr"] = "updateContent";
}
data["trid"] = id.toString();
}
data["u"] = apiUser!;
data["ak"] = apiKey!;
data["content"] = toJson(alternateEvents: runEvents);
String? res = await send(
apiUrl!,
data,
headers: headers,
timeoutMS: 10000,
);
bool wentOk =
(res?.startsWith("ok") ?? false) || (res?.startsWith("soso") ?? false);
if (res == null || !wentOk) {
ret = apiCallResultToMessage(res, "Error saving record to API");
} else {
if (create || runResult != null) {
int? resId = int.tryParse(res.replaceRange(0, 3, ""));
if (resId == null) {
ret = "Bad record id from API";
} else {
if (runResult == null) {
id = resId;
} else {
debugPrint("Run id: $resId");
return ("Record saved to API");
}
}
}
}
if (wentOk) {
return ("Record saved to API");
}
if (retries > 0) {
debugPrint("Warning: retrying to save to API: $retries");
return (await apiSave(
runEvents: runEvents,
runResult: runResult,
retries: retries - 1,
));
}
return (ret ?? "Failed to save record to API");
}