218 lines
6.8 KiB
Dart
218 lines
6.8 KiB
Dart
|
import 'dart:async';
|
||
|
import 'dart:convert';
|
||
|
import 'dart:io';
|
||
|
|
||
|
import 'package:dio/adapter.dart';
|
||
|
import 'package:dio/dio.dart';
|
||
|
|
||
|
import 'package:selfprivacy/models/courier.dart';
|
||
|
import 'package:selfprivacy/models/order.dart';
|
||
|
import 'package:selfprivacy/models/item.dart';
|
||
|
|
||
|
class ApiResponse<D> {
|
||
|
ApiResponse({
|
||
|
required this.statusCode,
|
||
|
required this.data,
|
||
|
this.errorMessage,
|
||
|
});
|
||
|
final int statusCode;
|
||
|
final String? errorMessage;
|
||
|
final D data;
|
||
|
|
||
|
bool get isSuccess => statusCode >= 200 && statusCode < 300;
|
||
|
}
|
||
|
|
||
|
class ServerApi {
|
||
|
Future<Dio> getClient() async {
|
||
|
final Dio dio = Dio(await options);
|
||
|
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
|
||
|
(final HttpClient client) {
|
||
|
client.badCertificateCallback =
|
||
|
(final X509Certificate cert, final String host, final int port) =>
|
||
|
true;
|
||
|
return client;
|
||
|
};
|
||
|
|
||
|
dio.interceptors.add(
|
||
|
InterceptorsWrapper(
|
||
|
onError: (final DioError e, final ErrorInterceptorHandler handler) {
|
||
|
print(e.requestOptions.path);
|
||
|
print(e.requestOptions.data);
|
||
|
|
||
|
print(e.message);
|
||
|
print(e.response);
|
||
|
|
||
|
return handler.next(e);
|
||
|
},
|
||
|
),
|
||
|
);
|
||
|
return dio;
|
||
|
}
|
||
|
|
||
|
BaseOptions get options =>
|
||
|
BaseOptions(
|
||
|
baseUrl: 'http://10.100.0.6:8888/api/v1',
|
||
|
connectTimeout: 10000,
|
||
|
receiveTimeout: 10000,
|
||
|
);
|
||
|
|
||
|
ValidateStatus? validateStatus;
|
||
|
|
||
|
void close(final Dio client) {
|
||
|
client.close();
|
||
|
validateStatus = null;
|
||
|
}
|
||
|
|
||
|
Future<ApiResponse<List<Item>>> getItems() async {
|
||
|
final Dio client = await getClient();
|
||
|
final Response response = await client.get('/items');
|
||
|
final List<Item> items = (response.data as List)
|
||
|
.map((final dynamic e) => Item.fromJson(e as Map<String, dynamic>))
|
||
|
.toList();
|
||
|
return ApiResponse<List<Item>>(
|
||
|
statusCode: response.statusCode ?? 0,
|
||
|
data: items,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// Create an item
|
||
|
Future<ApiResponse<Item>> createItem(final Item item) async {
|
||
|
final Dio client = await getClient();
|
||
|
final Response response = await client.post('/items', data: item.toJson(),);
|
||
|
final Item createdItem = Item.fromJson(response.data as Map<String, dynamic>);
|
||
|
return ApiResponse<Item>(
|
||
|
statusCode: response.statusCode ?? 0,
|
||
|
data: createdItem,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// Update an item
|
||
|
Future<ApiResponse<Item>> updateItem(final Item item) async {
|
||
|
final Dio client = await getClient();
|
||
|
final Response response = await client.put('/items/${item.id}', data: {
|
||
|
'title': item.title,
|
||
|
'description': item.description,
|
||
|
'price': item.price,
|
||
|
},);
|
||
|
final Item updatedItem = Item.fromJson(response.data as Map<String, dynamic>);
|
||
|
return ApiResponse<Item>(
|
||
|
statusCode: response.statusCode ?? 0,
|
||
|
data: updatedItem,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// Delete an item
|
||
|
Future<ApiResponse<Item>> deleteItem(final Item item) async {
|
||
|
final Dio client = await getClient();
|
||
|
final Response response = await client.delete('/items/${item.id}');
|
||
|
final Item deletedItem = Item.fromJson(response.data as Map<String, dynamic>);
|
||
|
return ApiResponse<Item>(
|
||
|
statusCode: response.statusCode ?? 0,
|
||
|
data: deletedItem,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// Get a single item
|
||
|
Future<ApiResponse<Item>> getItem(final int id) async {
|
||
|
final Dio client = await getClient();
|
||
|
final Response response = await client.get('/items/$id');
|
||
|
final Item item = Item.fromJson(response.data as Map<String, dynamic>);
|
||
|
return ApiResponse<Item>(
|
||
|
statusCode: response.statusCode ?? 0,
|
||
|
data: item,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// Get all couriers
|
||
|
Future<ApiResponse<List<Courier>>> getCouriers() async {
|
||
|
final Dio client = await getClient();
|
||
|
final Response response = await client.get('/couriers');
|
||
|
final List<Courier> couriers = (response.data as List)
|
||
|
.map((final dynamic e) => Courier.fromJson(e as Map<String, dynamic>))
|
||
|
.toList();
|
||
|
return ApiResponse<List<Courier>>(
|
||
|
statusCode: response.statusCode ?? 0,
|
||
|
data: couriers,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// Add a courier
|
||
|
Future<ApiResponse<Courier>> addCourier(final Courier courier) async {
|
||
|
final Dio client = await getClient();
|
||
|
final Response response = await client.post('/couriers', data: courier.toJson(),);
|
||
|
final Courier createdCourier = Courier.fromJson(response.data as Map<String, dynamic>);
|
||
|
return ApiResponse<Courier>(
|
||
|
statusCode: response.statusCode ?? 0,
|
||
|
data: createdCourier,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// Update a courier
|
||
|
Future<ApiResponse<Courier>> updateCourier(final Courier courier) async {
|
||
|
final Dio client = await getClient();
|
||
|
final Response response = await client.put('/couriers/${courier.id}', data: courier.toJson(),);
|
||
|
final Courier updatedCourier = Courier.fromJson(response.data as Map<String, dynamic>);
|
||
|
return ApiResponse<Courier>(
|
||
|
statusCode: response.statusCode ?? 0,
|
||
|
data: updatedCourier,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// Get a single courier
|
||
|
Future<ApiResponse<Courier>> getCourier(final int id) async {
|
||
|
final Dio client = await getClient();
|
||
|
final Response response = await client.get('/couriers/$id');
|
||
|
final Courier courier = Courier.fromJson(response.data as Map<String, dynamic>);
|
||
|
return ApiResponse<Courier>(
|
||
|
statusCode: response.statusCode ?? 0,
|
||
|
data: courier,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// Get all orders
|
||
|
Future<ApiResponse<List<Order>>> getOrders() async {
|
||
|
final Dio client = await getClient();
|
||
|
final Response response = await client.get('/orders');
|
||
|
final List<Order> orders = (response.data as List)
|
||
|
.map((final dynamic e) => Order.fromJson(e as Map<String, dynamic>))
|
||
|
.toList();
|
||
|
return ApiResponse<List<Order>>(
|
||
|
statusCode: response.statusCode ?? 0,
|
||
|
data: orders,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// Get a single order
|
||
|
Future<ApiResponse<Order>> getOrder(final int id) async {
|
||
|
final Dio client = await getClient();
|
||
|
final Response response = await client.get('/orders/$id');
|
||
|
final Order order = Order.fromJson(response.data as Map<String, dynamic>);
|
||
|
return ApiResponse<Order>(
|
||
|
statusCode: response.statusCode ?? 0,
|
||
|
data: order,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// Create an order
|
||
|
Future<ApiResponse<Order>> createOrder(final Order order) async {
|
||
|
final Dio client = await getClient();
|
||
|
final Response response = await client.post('/orders', data: order.toJsonAsNew());
|
||
|
final Order createdOrder = Order.fromJson(response.data as Map<String, dynamic>);
|
||
|
return ApiResponse<Order>(
|
||
|
statusCode: response.statusCode ?? 0,
|
||
|
data: createdOrder,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// Update an order
|
||
|
Future<ApiResponse<Order>> updateOrder(final Order order) async {
|
||
|
final Dio client = await getClient();
|
||
|
final Response response = await client.put('/orders/${order.id}', data: order.toJson());
|
||
|
final Order updatedOrder = Order.fromJson(response.data as Map<String, dynamic>);
|
||
|
return ApiResponse<Order>(
|
||
|
statusCode: response.statusCode ?? 0,
|
||
|
data: updatedOrder,
|
||
|
);
|
||
|
}
|
||
|
}
|