import 'dart:convert'; import 'dart:io'; import 'package:satu/core/base/base_service.dart'; import 'package:http/http.dart' as http; import 'package:satu/core/models/auth/auth_response.dart'; import 'package:satu/core/models/flow/analytics/analytics_bean.dart'; import 'package:satu/core/models/flow/sell_request.dart'; import 'package:satu/core/models/flow/sell_response.dart'; import 'package:satu/core/models/flow/sell_return/sell_return_request.dart'; import 'package:satu/core/models/response.dart'; /// The service responsible for networking requests class ApiService extends BaseService { static const host = 'satu.aman.com.kz'; static const endpoint = '/api/v1'; http.Client client = http.Client(); //TOKEN String? token; Future _get(String point, {Map? requestBody, Map? header}) async { final Map headers = { HttpHeaders.contentTypeHeader: 'application/json', HttpHeaders.cacheControlHeader: 'no-cache' }; if (header != null && header.isNotEmpty) { headers.addAll(header); } final String url = '$endpoint$point'; final response = await http.get(Uri.https(host, url), headers: headers); return response.body; } Future _post(String point, {Map? requestBody, Map? header}) async { final Map headers = { HttpHeaders.contentTypeHeader: 'application/json', HttpHeaders.cacheControlHeader: 'no-cache' }; if (header != null && header.isNotEmpty) { headers.addAll(header); } final String url = '$endpoint$point'; if (requestBody != null) { log.i(jsonEncode(requestBody)); } final response = await http.post(Uri.https(host, url), body: jsonEncode(requestBody), headers: headers); return response.body; } Future login(String username, String password) async { final Map requestBody = { 'username': username, 'password': password }; AuthResponse result; try { final String response = await _post('/login', requestBody: requestBody); result = AuthResponse.fromMap(json.decode(response)); } catch (e, stack) { log.e('login', e, stack); result = AuthResponse() ..message = 'Ошибка вызова сервера' ..operation = false; } return result; } Future authorization(String token) async { final Map requestBody = {'token': token}; AuthResponse result; try { final String response = await _post('/authorization', requestBody: requestBody); result = AuthResponse.fromMap(json.decode(response)); } catch (e, stack) { log.e('authorization', e, stack); result = AuthResponse() ..message = 'Ошибка вызова сервера' ..operation = false; } return result; } Future auth(String token) async { final Map headers = { HttpHeaders.authorizationHeader: 'Bearer $token' }; AuthResponse result; try { final String response = await _post('/auth', header: headers); result = AuthResponse.fromMap(json.decode(response)); } catch (e, stack) { log.e('auth', e, stack); result = AuthResponse() ..message = 'Ошибка вызова сервера' ..operation = false; } return result; } Future logout() async { final Map headers = { HttpHeaders.authorizationHeader: 'Bearer $token' }; AuthResponse result; try { final String response = await _post('/logout', header: headers); result = AuthResponse.fromMap(json.decode(response)); } catch (e, stack) { log.e('auth', e, stack); result = AuthResponse() ..message = 'Ошибка вызова сервера' ..operation = false; } return result; } Future dictionaries(String target) async { Response result; try { final Map headers = { HttpHeaders.authorizationHeader: 'Bearer $token' }; final String response = await _post(target, header: headers); result = Response.fromMapList(json.decode(response), null); } catch (e, stack) { log.e('dictionaries', e, stack); result = Response() ..operation = false ..list = []; } return result; } Future sell(SellRequest request) async { SellResponse response; try { final Map headers = { HttpHeaders.authorizationHeader: 'Bearer $token' }; final String responseBody = await _post('/sell', header: headers, requestBody: request.toJson()); response = SellResponse.fromMap(json.decode(responseBody)); } catch (e, stack) { log.e('dictionaries', e, stack); response = SellResponse() ..operation = false ..message = e.toString(); } return response; } Future sellReturn(SellReturnRequest request) async { SellResponse response; try { final Map headers = { HttpHeaders.authorizationHeader: 'Bearer $token' }; final String responseBody = await _post('/sell_return', header: headers, requestBody: request.toJson()); response = SellResponse.fromMap(json.decode(responseBody)); } catch (e, stack) { log.e('sellReturn', e, stack); response = SellResponse() ..operation = false ..message = e.toString(); } return response; } Future getAnalytics() async { AnalyticsBean? response; try { final Map headers = { HttpHeaders.authorizationHeader: 'Bearer $token' }; final String responseBody = await _get('/get_analytics', header: headers); log.i(responseBody); response = AnalyticsBean.fromMap(json.decode(responseBody)); } catch (e, stack) { log.e('getAnalytics', e, stack); } return response; } }