fixed api
This commit is contained in:
@ -2,49 +2,37 @@
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:developer';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:get_storage/get_storage.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:logger/logger.dart';
|
||||
import '../globals.dart' as globals;
|
||||
|
||||
class PengirimApi {
|
||||
static var client = http.Client();
|
||||
|
||||
class PengirimApi extends GetConnect {
|
||||
static var storage = GetStorage();
|
||||
|
||||
static var username = storage.read("username");
|
||||
static var password = storage.read("password");
|
||||
static var id = storage.read("id");
|
||||
|
||||
static clientClose(http.Client client) {
|
||||
client.close();
|
||||
}
|
||||
final log = Logger();
|
||||
|
||||
// get all kurir
|
||||
static Future<Map<String, dynamic>> getAllKurir() async {
|
||||
client = http.Client();
|
||||
Future<Map<String, dynamic>> getAllKurir() async {
|
||||
late Map<String, dynamic> result;
|
||||
|
||||
bool _cek_jaringan = await cek_jaringan(client);
|
||||
|
||||
// log("cek jaringan : " + _cek_jaringan.toString());
|
||||
|
||||
if (!_cek_jaringan) {
|
||||
result = {
|
||||
'status': 500,
|
||||
'message':
|
||||
"Tidak dapat terhubung ke server, Sila periksa koneksi internet anda"
|
||||
};
|
||||
} else {
|
||||
// wait for 3 sec
|
||||
// await Future.delayed(Duration(seconds: 3));
|
||||
// result = {'status': 200, 'message': "sini dia"};
|
||||
bool _checkServer = await cek_jaringan();
|
||||
|
||||
if (_checkServer) {
|
||||
await EasyLoading.show(
|
||||
status: 'Loading...',
|
||||
maskType: EasyLoadingMaskType.black,
|
||||
);
|
||||
try {
|
||||
var response = await client.get(
|
||||
var response = await http.get(
|
||||
Uri.parse(
|
||||
"${globals.http_to_server}api/pengirim/kurir?username=$username&password=$password&id=$id"),
|
||||
headers: {
|
||||
@ -69,40 +57,63 @@ class PengirimApi {
|
||||
'data': data
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
} on SocketException catch (e) {
|
||||
// abort the client
|
||||
await EasyLoading.dismiss();
|
||||
// closeClient();
|
||||
|
||||
log.i(e.toString() + " ini error socket");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message':
|
||||
"Tidak dapat terhubung ke server, Sila periksa koneksi internet anda"
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} on TimeoutException catch (e) {
|
||||
// client.close();
|
||||
log.i(e.toString() + " ini timeout");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} on Exception catch (e) {
|
||||
// client.close();
|
||||
log.i(e.toString() + " ini error");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} catch (e) {
|
||||
log.i(e.toString() + " ini di catch");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} finally {
|
||||
await EasyLoading.dismiss();
|
||||
}
|
||||
} else {
|
||||
result = {
|
||||
'status': 500,
|
||||
'message':
|
||||
"Tidak dapat terhubung ke server, Sila periksa koneksi internet anda"
|
||||
};
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// get kurir by nama
|
||||
static Future<Map<String, dynamic>> getKurirByNama(String nama) async {
|
||||
client = http.Client();
|
||||
Future<Map<String, dynamic>> getKurirByNama(String nama) async {
|
||||
late Map<String, dynamic> result;
|
||||
|
||||
bool _cek_jaringan = await cek_jaringan(client);
|
||||
|
||||
// log("cek jaringan : " + _cek_jaringan.toString());
|
||||
|
||||
if (!_cek_jaringan) {
|
||||
result = {
|
||||
'status': 500,
|
||||
'message':
|
||||
"Tidak dapat terhubung ke server, Sila periksa koneksi internet anda"
|
||||
};
|
||||
} else {
|
||||
// wait for 3 sec
|
||||
// await Future.delayed(Duration(seconds: 3));
|
||||
// result = {'status': 200, 'message': "sini dia"};
|
||||
bool _checkServer = await cek_jaringan();
|
||||
|
||||
if (_checkServer) {
|
||||
try {
|
||||
var response = await client.get(
|
||||
await EasyLoading.show(
|
||||
status: 'Loading...',
|
||||
maskType: EasyLoadingMaskType.black,
|
||||
);
|
||||
var response = await http.get(
|
||||
Uri.parse(
|
||||
"${globals.http_to_server}api/pengirim/kurir/nama?nama=$nama&username=$username&password=$password&id=$id"),
|
||||
headers: {
|
||||
@ -127,45 +138,68 @@ class PengirimApi {
|
||||
'data': data
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
} on SocketException catch (e) {
|
||||
// abort the client
|
||||
await EasyLoading.dismiss();
|
||||
// closeClient();
|
||||
|
||||
log.i(e.toString() + " ini error socket");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message':
|
||||
"Tidak dapat terhubung ke server, Sila periksa koneksi internet anda"
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} on TimeoutException catch (e) {
|
||||
// client.close();
|
||||
log.i(e.toString() + " ini timeout");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} on Exception catch (e) {
|
||||
// client.close();
|
||||
log.i(e.toString() + " ini error");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} catch (e) {
|
||||
log.i(e.toString() + " ini di catch");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} finally {
|
||||
await EasyLoading.dismiss();
|
||||
}
|
||||
} else {
|
||||
result = {
|
||||
'status': 500,
|
||||
'message':
|
||||
"Tidak dapat terhubung ke server, Sila periksa koneksi internet anda"
|
||||
};
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// get kurir by nama
|
||||
static Future<Map<String, dynamic>> getKurirByFilter(
|
||||
Future<Map<String, dynamic>> getKurirByFilter(
|
||||
String? nama, int? biayaMaksimal, int? biayaPerKm) async {
|
||||
client = http.Client();
|
||||
late Map<String, dynamic> result;
|
||||
|
||||
bool _cek_jaringan = await cek_jaringan(client);
|
||||
|
||||
// log("cek jaringan : " + _cek_jaringan.toString());
|
||||
|
||||
if (!_cek_jaringan) {
|
||||
result = {
|
||||
'status': 500,
|
||||
'message':
|
||||
"Tidak dapat terhubung ke server, Sila periksa koneksi internet anda"
|
||||
};
|
||||
} else {
|
||||
// wait for 3 sec
|
||||
// await Future.delayed(Duration(seconds: 3));
|
||||
// result = {'status': 200, 'message': "sini dia"};
|
||||
bool _checkServer = await cek_jaringan();
|
||||
|
||||
if (_checkServer) {
|
||||
await EasyLoading.show(
|
||||
status: 'Loading...',
|
||||
maskType: EasyLoadingMaskType.black,
|
||||
);
|
||||
try {
|
||||
String _nama = nama ?? "";
|
||||
String _biayaMaksimal = biayaMaksimal?.toString() ?? "";
|
||||
String _biayaPerKm = biayaPerKm?.toString() ?? "";
|
||||
|
||||
var response = await client.get(
|
||||
var response = await http.get(
|
||||
Uri.parse(
|
||||
"${globals.http_to_server}api/pengirim/kurir/filter?nama=$_nama&biaya_maksimal=$_biayaMaksimal&biaya_per_km=$_biayaPerKm&username=$username&password=$password&id=$id"),
|
||||
headers: {
|
||||
@ -190,41 +224,67 @@ class PengirimApi {
|
||||
'data': data
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
} on SocketException catch (e) {
|
||||
// abort the client
|
||||
await EasyLoading.dismiss();
|
||||
// closeClient();
|
||||
|
||||
log.i(e.toString() + " ini error socket");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message':
|
||||
"Tidak dapat terhubung ke server, Sila periksa koneksi internet anda"
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} on TimeoutException catch (e) {
|
||||
// client.close();
|
||||
log.i(e.toString() + " ini timeout");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} on Exception catch (e) {
|
||||
// client.close();
|
||||
log.i(e.toString() + " ini error");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} catch (e) {
|
||||
log.i(e.toString() + " ini di catch");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} finally {
|
||||
await EasyLoading.dismiss();
|
||||
}
|
||||
} else {
|
||||
result = {
|
||||
'status': 500,
|
||||
'message':
|
||||
"Tidak dapat terhubung ke server, Sila periksa koneksi internet anda"
|
||||
};
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// post create pengiriman barang
|
||||
static Future<Map<String, dynamic>> createPengirimanBarang(
|
||||
Future<Map<String, dynamic>> createPengirimanBarang(
|
||||
Map<String, dynamic> _datanya) async {
|
||||
client = http.Client();
|
||||
late Map<String, dynamic> result;
|
||||
|
||||
bool _cek_jaringan = await cek_jaringan(client);
|
||||
bool _checkServer = await cek_jaringan();
|
||||
|
||||
// log("cek jaringan : " + _cek_jaringan.toString());
|
||||
|
||||
if (!_cek_jaringan) {
|
||||
result = {
|
||||
'status': 500,
|
||||
'message':
|
||||
"Tidak dapat terhubung ke server, Sila periksa koneksi internet anda"
|
||||
};
|
||||
} else {
|
||||
if (_checkServer) {
|
||||
try {
|
||||
await EasyLoading.show(
|
||||
status: 'Loading...',
|
||||
maskType: EasyLoadingMaskType.black,
|
||||
);
|
||||
String foto_path = _datanya['foto_path'];
|
||||
// log(foto_path.toString() + " ini datanya di pengiriman barang");
|
||||
// remove foto_path from _datanya
|
||||
_datanya.remove('foto_path');
|
||||
log(_datanya.toString());
|
||||
var postUri = Uri.parse(
|
||||
'${globals.http_to_server}api/pengirim/pengiriman_barang?username=$username&password=$password&id=$id');
|
||||
var request = http.MultipartRequest("POST", postUri);
|
||||
@ -232,49 +292,68 @@ class PengirimApi {
|
||||
request.files.add(
|
||||
await http.MultipartFile.fromPath('foto_pengiriman', foto_path));
|
||||
var streamResponse =
|
||||
await request.send().timeout(const Duration(seconds: 30));
|
||||
await request.send().timeout(const Duration(seconds: 60));
|
||||
// var streamResponse = await request.send();
|
||||
var response = await http.Response.fromStream(streamResponse);
|
||||
|
||||
var datanya = jsonDecode(response.body);
|
||||
|
||||
log(datanya.toString() + " ini datanya di pengiriman barang");
|
||||
|
||||
result = {
|
||||
'status': 200,
|
||||
'message': datanya['message'],
|
||||
};
|
||||
} on SocketException catch (e) {
|
||||
// abort the client
|
||||
// closeClient();
|
||||
|
||||
log.i(e.toString() + " ini error socket");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} on TimeoutException catch (e) {
|
||||
// client.close();
|
||||
log.i(e.toString() + " ini timeout");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} on Exception catch (e) {
|
||||
// client.close();
|
||||
log.i(e.toString() + " ini error");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} catch (e) {
|
||||
log(e.toString() + " ini error");
|
||||
result = {'status': 500, 'message': e.toString()};
|
||||
log.i(e.toString() + " ini di catch");
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} finally {
|
||||
await EasyLoading.dismiss();
|
||||
}
|
||||
} else {
|
||||
result = {
|
||||
'status': 500,
|
||||
'message':
|
||||
"Tidak dapat terhubung ke server, Sila periksa koneksi internet anda"
|
||||
};
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// get log Kiriman
|
||||
static Future<Map<String, dynamic>> getLogKiriman() async {
|
||||
client = http.Client();
|
||||
Future<Map<String, dynamic>> getLogKiriman() async {
|
||||
late Map<String, dynamic> result;
|
||||
|
||||
bool _cek_jaringan = await cek_jaringan(client);
|
||||
|
||||
// log("cek jaringan : " + _cek_jaringan.toString());
|
||||
|
||||
if (!_cek_jaringan) {
|
||||
result = {
|
||||
'status': 500,
|
||||
'message':
|
||||
"Tidak dapat terhubung ke server, Sila periksa koneksi internet anda"
|
||||
};
|
||||
} else {
|
||||
// wait for 3 sec
|
||||
// await Future.delayed(Duration(seconds: 3));
|
||||
// result = {'status': 200, 'message': "sini dia"};
|
||||
bool _checkServer = await cek_jaringan();
|
||||
|
||||
if (_checkServer) {
|
||||
try {
|
||||
var response = await client.get(
|
||||
var response = await http.get(
|
||||
Uri.parse(
|
||||
"${globals.http_to_server}api/pengirim/log_kiriman?username=$username&password=$password&id=$id"),
|
||||
headers: {
|
||||
@ -298,66 +377,88 @@ class PengirimApi {
|
||||
'data': data
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
log(e.toString() + " ini error");
|
||||
} on SocketException {
|
||||
// abort the client
|
||||
// closeClient();
|
||||
|
||||
result = {
|
||||
'status': 500,
|
||||
'message':
|
||||
"Tidak dapat terhubung ke server, Sila periksa koneksi internet anda"
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} on TimeoutException {
|
||||
// client.close();
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} on Exception {
|
||||
// client.close();
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} catch (e) {
|
||||
result = {
|
||||
'status': 500,
|
||||
'message': "Tidak dapat terhubung ke server,koneksi timeout"
|
||||
};
|
||||
} finally {
|
||||
await EasyLoading.dismiss();
|
||||
}
|
||||
} else {
|
||||
result = {
|
||||
'status': 500,
|
||||
'message':
|
||||
"Tidak dapat terhubung ke server, Sila periksa koneksi internet anda"
|
||||
};
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static Future<double> jarak_route(
|
||||
Future<double> jarak_route(
|
||||
double lat1, double lng1, double lat2, double lng2) async {
|
||||
client = http.Client();
|
||||
double jarak = 0;
|
||||
|
||||
bool _cek_jaringan = await cek_jaringan(client);
|
||||
try {
|
||||
var response = await http.get(
|
||||
Uri.parse(
|
||||
"https://maps.googleapis.com/maps/api/directions/json?origin=$lat1,$lng1&destination=$lat2,$lng2&key=${globals.api_key}"),
|
||||
headers: {
|
||||
"Accept": "application/json",
|
||||
// "authorization":
|
||||
// "Basic ${base64Encode(utf8.encode("Kicap_karan:bb10c6d9f01ec0cb16726b59e36c2f73"))}",
|
||||
"crossDomain": "true"
|
||||
}).timeout(const Duration(seconds: 10));
|
||||
|
||||
// log("cek jaringan : " + _cek_jaringan.toString());
|
||||
// log()
|
||||
final data = jsonDecode(response.body);
|
||||
|
||||
if (!_cek_jaringan) {
|
||||
jarak = 0;
|
||||
} else {
|
||||
try {
|
||||
var response = await client.get(
|
||||
Uri.parse(
|
||||
"https://maps.googleapis.com/maps/api/directions/json?origin=$lat1,$lng1&destination=$lat2,$lng2&key=${globals.api_key}"),
|
||||
headers: {
|
||||
"Accept": "application/json",
|
||||
// "authorization":
|
||||
// "Basic ${base64Encode(utf8.encode("Kicap_karan:bb10c6d9f01ec0cb16726b59e36c2f73"))}",
|
||||
"crossDomain": "true"
|
||||
}).timeout(const Duration(seconds: 10));
|
||||
|
||||
// log()
|
||||
final data = jsonDecode(response.body);
|
||||
|
||||
if (data["routes"].length > 0) {
|
||||
jarak = data["routes"][0]["legs"][0]["distance"]["value"] / 1000;
|
||||
} else {
|
||||
jarak = 0;
|
||||
}
|
||||
} catch (e) {
|
||||
if (data["routes"].length > 0) {
|
||||
jarak = data["routes"][0]["legs"][0]["distance"]["value"] / 1000;
|
||||
} else {
|
||||
jarak = 0;
|
||||
}
|
||||
} catch (e) {
|
||||
jarak = 0;
|
||||
}
|
||||
|
||||
return jarak;
|
||||
}
|
||||
|
||||
// checking connection to server
|
||||
static Future<bool> cek_jaringan(http.Client client) async {
|
||||
Future<bool> cek_jaringan() async {
|
||||
late bool result;
|
||||
|
||||
// client get for globals.http_to_server
|
||||
await EasyLoading.show(
|
||||
status: 'Sedang\nCek\nJaringan\n...',
|
||||
maskType: EasyLoadingMaskType.black,
|
||||
);
|
||||
|
||||
try {
|
||||
var response =
|
||||
await client.get(Uri.parse("${globals.http_to_server}api"), headers: {
|
||||
await http.get(Uri.parse("${globals.http_to_server}api"), headers: {
|
||||
"Accept": "application/json",
|
||||
// "authorization":
|
||||
// "Basic ${base64Encode(utf8.encode("Kicap_karan:bb10c6d9f01ec0cb16726b59e36c2f73"))}",
|
||||
@ -370,22 +471,23 @@ class PengirimApi {
|
||||
result = false;
|
||||
}
|
||||
} on SocketException {
|
||||
await EasyLoading.dismiss();
|
||||
result = false;
|
||||
await clientClose(client);
|
||||
log(" ini error socket");
|
||||
// await clientClose(client);
|
||||
log.i(" ini error socket");
|
||||
} on TimeoutException {
|
||||
await EasyLoading.dismiss();
|
||||
result = false;
|
||||
// close client
|
||||
await clientClose(client);
|
||||
log(" ini timeout");
|
||||
// await clientClose(client);
|
||||
log.i(" ini timeout");
|
||||
} on Exception {
|
||||
result = false;
|
||||
log(" ini timeout");
|
||||
log.i(" ini timeout");
|
||||
} catch (e) {
|
||||
result = false;
|
||||
log(" ini timeout");
|
||||
log.i(" ini timeout");
|
||||
} finally {
|
||||
await EasyLoading.dismiss();
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user