apiUser function
Implementation
Future<int> apiUser() 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/users/${userData.id}'));
// 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"];
List<dynamic> skins = data["skins"];
// print(skins);
var board = skins[0], darkPieces = skins[6], lightPieces = skins[6];
for (var skin in skins) {
if (skin["type"] == "board" && skin["active"] == true) {
board = skin;
}
if (skin["type"] == "pieces" && skin["activeDark"] == true) {
darkPieces = skin;
}
if (skin["type"] == "pieces" && skin["activeLight"] == true) {
lightPieces = skin;
}
}
List<dynamic> achievements = data["achievements"];
// print(achievements);
List<List> ach = List.empty(growable: true);
for (var achievement in achievements) {
ach.add([
achievement["imgSrc"],
achievement["imgAlt"],
achievement["achieved"]
]);
}
updateProfile(
data["avatar"],
data["username"],
data["email"],
data["elo"],
data["ranking"],
data["stats"]["bulletWins"],
data["stats"]["bulletDraws"],
data["stats"]["bulletDefeats"],
data["stats"]["blitzWins"],
data["stats"]["blitzDraws"],
data["stats"]["blitzDefeats"],
data["stats"]["fastWins"],
data["stats"]["fastDraws"],
data["stats"]["fastDefeats"],
ach,
board["name"],
board["darkColor"],
board["lightColor"],
darkPieces["src"],
lightPieces["src"],
data["games"]);
// 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();
}
}