imageEditForm<T> static method
Implementation
static Future imageEditForm<T>({
required String to,
required T Function(Map<String, dynamic>) onSuccess,
required File image,
required File? mask,
required Map<String, String> body,
}) async {
AILogger.log("starting request to $to");
final http.MultipartRequest request = http.MultipartRequest(
"POST",
Uri.parse(to),
);
request.headers.addAll(AIConfigBuilder.instance.headers());
final http.MultipartFile file =
await http.MultipartFile.fromPath("image", image.path);
final http.MultipartFile? maskFile = mask != null
? await http.MultipartFile.fromPath("mask", mask.path)
: null;
request.files.add(file);
if (maskFile != null) request.files.add(maskFile);
request.fields.addAll(body);
final http.StreamedResponse response = await request.send();
AILogger.log(
"request to $to finished with status code ${response.statusCode}");
AILogger.log("starting decoding response body");
final String encodedBody = await response.stream.bytesToString();
final Map<String, dynamic> decodedBody =
jsonDecode(encodedBody) as Map<String, dynamic>;
AILogger.log("response body decoded successfully");
if (decodedBody['error'] != null) {
AILogger.log("an error occurred, throwing exception");
final Map<String, dynamic> error = decodedBody['error'];
throw AIRequestException(
error["message"],
response.statusCode,
);
} else {
AILogger.log("request finished successfully");
return onSuccess(decodedBody);
}
}