apiRequest method
Implementation
Future<String> apiRequest() async {
String basicAuth;
if(authObjectSDK.containsKey('access_token') && authObjectSDK['access_token'] != null && authObjectSDK['access_token'] != ""){
basicAuth = "Bearer " + authObjectSDK['access_token'];
}
else {
String username = authObjectSDK["client_id"].toString();
String password = authObjectSDK["secret_key"].toString();
String encodeBasicAuth = base64Encode(utf8.encode('$username:$password'));
basicAuth = "Basic $encodeBasicAuth";
}
Dio dio = new Dio();
dio.options.headers['Accept'] = 'application/json';
dio.options.headers['Content-Type'] = 'application/json';
dio.options.headers["Authorization"] = basicAuth;
try{
final response = await dio.post("https://api.shuftipro.com/",
data: requestedPayload,
options: Options(
followRedirects: false,
validateStatus: (status) { return status! < 500; },
),
);
if (response.statusCode == 301) {
String redirect_url = response.headers["location"].toString();
String final_url = redirect_url.substring( 1, redirect_url.length - 1 );
final getResponse = await Dio().get(final_url);
return getResponse.toString();
}
else if (response.statusCode == 200) {
return response.toString();
}
else{
return response.toString();
}
}on DioError catch (e) {
print("Exception: ");
print(e.type);
if (e.type == DioErrorType.connectTimeout || e.type == DioErrorType.sendTimeout || e.type == DioErrorType.receiveTimeout || e.response.toString().toLowerCase().contains("504 gateway time-out")) {
return "request.timeout";
}
else{
return e.message;
}
};
}