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 { 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 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>> getItems() async { final Dio client = await getClient(); final Response response = await client.get('/items'); final List items = (response.data as List) .map((final dynamic e) => Item.fromJson(e as Map)) .toList(); return ApiResponse>( statusCode: response.statusCode ?? 0, data: items, ); } // Create an item Future> 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); return ApiResponse( statusCode: response.statusCode ?? 0, data: createdItem, ); } // Update an item Future> 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); return ApiResponse( statusCode: response.statusCode ?? 0, data: updatedItem, ); } // Delete an item Future> 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); return ApiResponse( statusCode: response.statusCode ?? 0, data: deletedItem, ); } // Get a single item Future> 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); return ApiResponse( statusCode: response.statusCode ?? 0, data: item, ); } // Get all couriers Future>> getCouriers() async { final Dio client = await getClient(); final Response response = await client.get('/couriers'); final List couriers = (response.data as List) .map((final dynamic e) => Courier.fromJson(e as Map)) .toList(); return ApiResponse>( statusCode: response.statusCode ?? 0, data: couriers, ); } // Add a courier Future> 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); return ApiResponse( statusCode: response.statusCode ?? 0, data: createdCourier, ); } // Update a courier Future> 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); return ApiResponse( statusCode: response.statusCode ?? 0, data: updatedCourier, ); } // Get a single courier Future> 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); return ApiResponse( statusCode: response.statusCode ?? 0, data: courier, ); } // Get all orders Future>> getOrders() async { final Dio client = await getClient(); final Response response = await client.get('/orders'); final List orders = (response.data as List) .map((final dynamic e) => Order.fromJson(e as Map)) .toList(); return ApiResponse>( statusCode: response.statusCode ?? 0, data: orders, ); } // Get a single order Future> 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); return ApiResponse( statusCode: response.statusCode ?? 0, data: order, ); } // Create an order Future> 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); return ApiResponse( statusCode: response.statusCode ?? 0, data: createdOrder, ); } // Update an order Future> 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); return ApiResponse( statusCode: response.statusCode ?? 0, data: updatedOrder, ); } }