apiGames function
Implementation
Future<String> apiGames(String url) async {
// var page = 1;
// var limit = 50;
// url = 'https://api.gracehopper.xyz/v1/games?page=$page&limit=$limit';
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(url));
// 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"];
// print(data);
// print(responseBodyDictionary);
List<dynamic> gameList = data;
for (var element in gameList) {
// print(element);
GameData gameData = GameData(
element["id"],
element["lightPlayer"],
element["darkPlayer"],
element["board"],
element["moves"],
element["times"]["initial"],
element["times"]["increment"],
element["times"]["lightTimer"],
element["times"]["darkTimer"],
element["gameType"],
element["createdAt"],
element["updatedAt"]);
// print(element);
if (element["state"] == "PAUSED") {
// print(element);
addSavedGame(gameData);
}
if (element["state"] == "ENDED") {
gameData.addResult(element["winner"], element["endState"]);
// print(element);
addPlayedGame(gameData);
}
}
if (responseBodyDictionary["meta"]["nextPage"] == null) {
return "null";
} else {
return responseBodyDictionary["meta"]["nextPage"];
}
//aqui ns que necesitas q devuelva
// return responseBodyDictionary["status"]["error_code"];
} catch (e) {
// print(e.toString());
return "null";
} finally {
client.close();
}
}