apiMyTournaments function

Future<int> apiMyTournaments()

Implementation

Future<int> apiMyTournaments() async {
  UserData userData = UserData();

  var pemBytes = await rootBundle.load("assets/cert.pem");

  var context = SecurityContext()
    ..setTrustedCertificatesBytes(pemBytes.buffer.asUint8List(), password: '');

  var client = HttpClient(context: context)
    ..badCertificateCallback =
        (X509Certificate cert, String host, int port) => true;

  try {
    var request = await client.getUrl(Uri.parse(
        'https://api.gracehopper.xyz/v1/tournaments?sort=-startTime&filter=%7B%22participants%22%3A+%22${userData.id}%22%7D'));
    // Set headers
    request.headers.add('Content-Type', 'application/json');
    request.headers.add('Cookie', 'api-auth=${UserData().token}');

    var response = await request.close();
    var responseBody = await response.transform(utf8.decoder).join();
    var responseBodyDictionary = jsonDecode(responseBody);
    var data = responseBodyDictionary["data"];
    // Verify subscribed tournaments
    for (var t in data) {
      ManageTournamentData m = ManageTournamentData();
      m.update(
          t["id"],
          t["owner"]["id"],
          t["owner"]["username"],
          t["owner"]["avatar"],
          t["startTime"],
          t["rounds"],
          t["matchProps"]["time"],
          t["matchProps"]["increment"],
          t["finished"],
          t["hasStarted"]);
      ActualSelection.manageTournamentDatas.add(m);
    }

    // print(data);

    // print(responseBodyDictionary);
    return 0;
    //aqui ns que necesitas q devuelva
    // return responseBodyDictionary["status"]["error_code"];
  } catch (e) {
    // print(e.toString());
    return -1;
  } finally {
    client.close();
  }
}