httpRequest method
dynamic
httpRequest(
- dynamic type,
- dynamic url,
- dynamic data
Implementation
httpRequest(type,url,data) async {
// ignore: prefer_typing_uninitialized_variables
var response,httpResponse;
try {
type = type.toString().toLowerCase();
var headers = {
'Content-type': 'application/json',
'Accept': 'application/json',
};
data['api_key'] = sdkApiKey;
data['sub_client_api_key'] = apiKey;
HttpClient client = HttpClient()..badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
var ioClient = IOClient(client);
if(type == 'get'){
httpResponse = await ioClient.get(Uri.parse(url),headers: headers);
}else if(type == 'delete'){
httpResponse = await ioClient.delete(Uri.parse(url), body: json.encode(data),headers: headers);
}else if(type == 'patch'){
httpResponse = await ioClient.patch(Uri.parse(url), body: json.encode(data),headers: headers);
}else{
httpResponse = await ioClient.post(Uri.parse(url), body: json.encode(data),headers: headers);
}
_printToLog("$url");
_printToLog("$data");
if(httpResponse.statusCode == 200){
//
response = json.decode(httpResponse.body);
}else{
response = {'status':'error','message':'${httpResponse.statusCode} error $url ${httpResponse.body}'};
}
if(response['message'].toString().contains('SocketException: OS Error')){
response['message'] = 'Network Error';
}
if(response['message'].toString().contains('No address associated with hostname')){
response['message'] = 'Connection Error';
}
} catch (error) {
_printToLog("$error");
var response = {'status':'error','message':'Error: $error $url'};
if(response['message'].toString().contains('SocketException')){
response['message'] = 'Network Error';
}
if(response['message'].toString().contains('No address associated with hostname')){
response['message'] = 'Connection Error';
}
}
_printToLog("$response");
return response;
}