apiJoinOrLeaveTournament function

Future<int> apiJoinOrLeaveTournament(
  1. String type,
  2. String id
)

Implementation

Future<int> apiJoinOrLeaveTournament(String type, String id) async {
  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;
  //print(type + " - " + id);
  try {
    var request = await client.getUrl(
        Uri.parse('https://api.gracehopper.xyz/v1/tournaments/$type/$id'));
    // Set headers
    request.headers.add('Content-Type', 'application/json');
    request.headers.add('Cookie', 'api-auth=${UserData().token}');

    // Create JSON body
    // var body = jsonEncode({});

    // Set body
    // request.write(body);
    var response = await request.close();
    var responseBody = await response.transform(utf8.decoder).join();
    var responseBodyDictionary = jsonDecode(responseBody);

    if (type == "join") {
      await apiOtherTournaments();
    } else if (type == "leave") {
      await apiMyTournaments();
    }
    // print(responseBodyDictionary);
    //print(responseBodyDictionary["status"]["error_code"]);
    // print(apiAuthCookie);
    return responseBodyDictionary["status"]["error_code"];
  } catch (e) {
    //print(e.toString());
    return -1;
  } finally {
    client.close();
  }
}