Advertisement
Guest User

Untitled

a guest
Apr 9th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. mixin LoginLogic on Model {
  2. Map<String, dynamic> authData;
  3. final UtilityApi _api = UtilityApi();
  4.  
  5. Future<dynamic> _makeRequest(
  6. {String method,
  7. String schoolCode,
  8. String username,
  9. String password,
  10. DateTime date}) async {
  11. if (date == null) date = DateTime.now();
  12.  
  13. final headers = {
  14. 'x-key-app': _api.apiKey,
  15. 'x-version': _api.version,
  16. 'user-agent': _api.userAgent,
  17. 'x-cod-min': schoolCode ?? _api.schoolCode,
  18. };
  19.  
  20. if (method != 'login') {
  21. headers.addAll({
  22. 'x-auth-token': _api.token,
  23. 'x-prg-alunno': _api.prgAlunno,
  24. 'x-prg-scuola': _api.prgScuola,
  25. 'x-prg-scheda': _api.prgScheda
  26. });
  27. } else {
  28. headers.addAll({'x-user-id': username, 'x-pwd': password});
  29. }
  30.  
  31. http.Response response = await http.get(
  32. Uri(
  33. scheme: _api.apiUrl[0],
  34. host: _api.apiUrl[1],
  35. path: _api.apiUrl[2] + method,
  36. queryParameters: {
  37. "_dc": date.millisecondsSinceEpoch.toString(),
  38. "datGiorno":
  39. "${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}"
  40. },
  41. ),
  42. headers: headers);
  43.  
  44. if (response.statusCode != 200) {
  45. throw new Exception(
  46. "$method failed\n${response.request}\n${response.statusCode} : ${response.body}");
  47. }
  48. final decoded = jsonDecode(response.body);
  49. return decoded;
  50. }
  51.  
  52. Future _initClient({String schoolCode, String token}) async {
  53. assert(schoolCode != null && token != null);
  54. this._api.schoolCode = schoolCode;
  55. this._api.token = token;
  56. var response = await _makeRequest(method: "schede");
  57. print(response[0]);
  58. //school = new School.fromJson(response[0]);
  59. //student = new Student.fromJson(response[0]);
  60.  
  61. //_api.prgScuola = school.schoolId.toString();
  62. //_api.prgAlunno = student.studentId.toString();
  63. }
  64.  
  65. Future<String> login(
  66. String email, String schoolCode, String username, String password) async {
  67. authData = {
  68. 'email': email,
  69. 'schoolCode': schoolCode,
  70. 'username': username,
  71. 'password': password,
  72. };
  73. var jResponse = await _makeRequest(
  74. method: "login",
  75. schoolCode: authData['schoolCode'],
  76. username: authData['username'],
  77. password: authData['password']);
  78.  
  79. assert(jResponse["token"] != null, 'Token is null');
  80.  
  81. _api.schoolCode = authData['schoolCode'];
  82. _api.token = jResponse["token"];
  83.  
  84. _storeValues(authData, _api.token);
  85. await _initClient(schoolCode: authData['schoolCode'], token: _api.token);
  86. print(_api.token);
  87. notifyListeners();
  88. return _api.token;
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement