submitReport static method

Future<BugReport> submitReport(
  1. BugReportRequest submitReportRequest
)

Implementation

static Future<BugReport> submitReport(BugReportRequest submitReportRequest) async {
  final uri = Uri.parse('$_apiUrl/bug_reports');
  final request = http.MultipartRequest('POST', uri)
    ..fields['api_token'] = submitReportRequest.apiToken
    ..fields['app_install[id]'] = submitReportRequest.appInstall.id.toString()
    ..fields['bug_report[description]'] = submitReportRequest.report.description ?? 'no description'
    ..fields['bug_report[steps_to_reproduce]'] =
        submitReportRequest.report.stepsToReproduce ?? 'no steps to reproduce'
    ..fields['bug_report[user_identifier]'] = submitReportRequest.report.userIdentifier ?? 'no user identifier'
    ..fields.addAll(await Api.deviceStatus());

  if (submitReportRequest.report.attachments?.isNotEmpty ?? false) {
    await Future.wait(submitReportRequest.report.attachments!.map((attachment) async {
      if (attachment.path != null) {
        request.files.add(await http.MultipartFile.fromPath('bug_report[attachments][]', attachment.path!,
            filename: attachment.name));
      }
    }));
  }

  final Completer<BugReport> completer = Completer<BugReport>();

  request.send().then((response) {
    print('Response: ${response.statusCode}');
    final contents = StringBuffer();
    response.stream.transform(utf8.decoder).listen((data) {
      contents.write(data);
    }, onDone: () {
      print(contents.toString());
      completer.complete(BugReport.fromJson(json.decode(contents.toString())));
    });
  });

  return completer.future;
}