You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.7 KiB
Dart
72 lines
2.7 KiB
Dart
|
3 years ago
|
import 'dart:convert';
|
||
|
|
|
||
|
|
import 'package:http/http.dart';
|
||
|
|
import 'package:mohem_flutter_app/api/api_client.dart';
|
||
|
|
import 'package:mohem_flutter_app/app_state/app_state.dart';
|
||
|
|
import 'package:mohem_flutter_app/classes/consts.dart';
|
||
|
|
import 'package:mohem_flutter_app/models/marathon/marathon_generic_model.dart';
|
||
|
|
import 'package:mohem_flutter_app/models/marathon/marathon_model.dart';
|
||
|
|
|
||
|
|
class MarathonApiClient {
|
||
|
|
Future<String> getMarathonToken() async {
|
||
|
|
String employeeUserName = AppState().getUserName ?? "";
|
||
|
|
String employeeSession = AppState().postParamsObject?.pSessionId.toString() ?? "";
|
||
|
|
|
||
|
|
Map<String, String> jsonObject = {"userName": employeeUserName, "password": employeeSession};
|
||
|
|
Response response = await ApiClient().postJsonForResponse(ApiConsts.marathonParticipantLoginUrl, jsonObject);
|
||
|
|
|
||
|
|
var json = jsonDecode(response.body);
|
||
|
|
|
||
|
|
MarathonGenericModel marathonModel = MarathonGenericModel.fromJson(json);
|
||
|
|
|
||
|
|
if (marathonModel.statusCode == 200) {
|
||
|
|
if (marathonModel.data != null && marathonModel.isSuccessful == true) {
|
||
|
|
print("bearerToken: ${marathonModel.data["token"]}");
|
||
|
|
AppState().setMarathonToken = marathonModel.data["token"] ?? "";
|
||
|
|
return marathonModel.data["token"] ?? "";
|
||
|
|
} else {
|
||
|
|
//TODO : DO ERROR HANDLING HERE
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
//TODO : DO ERROR HANDLING HERE
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<String> getProjectId() async {
|
||
|
|
Response response = await ApiClient().postJsonForResponse(ApiConsts.marathonProjectGetUrl, <String, dynamic>{}, token: AppState().getMarathonToken ?? await getMarathonToken());
|
||
|
|
|
||
|
|
var json = jsonDecode(response.body);
|
||
|
|
MarathonGenericModel marathonModel = MarathonGenericModel.fromJson(json);
|
||
|
|
|
||
|
|
if (marathonModel.statusCode == 200) {
|
||
|
|
if (marathonModel.data != null && marathonModel.isSuccessful == true) {
|
||
|
|
print("projectID: ${marathonModel.data[0]["id"]}");
|
||
|
|
AppState().setMarathonProjectId = marathonModel.data[0]["id"] ?? "";
|
||
|
|
return marathonModel.data[0]["id"] ?? "";
|
||
|
|
} else {
|
||
|
|
//TODO : DO ERROR HANDLING HERE
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
//TODO : DO ERROR HANDLING HERE
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<MarathonDetailModel> getMarathonDetails() async {
|
||
|
|
String payrollString = AppState().postParamsObject?.payrollCodeStr.toString() ?? "CS";
|
||
|
|
|
||
|
|
Response response = await ApiClient().getJsonForResponse(ApiConsts.marathonUpcomingUrl + payrollString, token: AppState().getMarathonToken ?? await getMarathonToken());
|
||
|
|
|
||
|
|
var json = jsonDecode(response.body);
|
||
|
|
|
||
|
|
MarathonGenericModel marathonGenericModel = MarathonGenericModel.fromJson(json);
|
||
|
|
|
||
|
|
MarathonDetailModel marathonDetailModel = MarathonDetailModel.fromJson(marathonGenericModel.data);
|
||
|
|
|
||
|
|
return marathonDetailModel;
|
||
|
|
}
|
||
|
|
}
|