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/response.dart'; /// The service responsible for networking requests class ApiService extends BaseService { static const host = 'satu.aman.com.kz'; static const endpoint = '/api/v1'; var client = new http.Client(); //TOKEN String _token; String get token => this._token; set token(String value) => this._token = value; Future _get(String point, {Map requestBody, Map header}) async { Map headers = { HttpHeaders.contentTypeHeader: "application/json", HttpHeaders.cacheControlHeader: "no-cache" }; if (header != null && header.isNotEmpty) { headers.addAll(header); } 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 { Map headers = { HttpHeaders.contentTypeHeader: "application/json", HttpHeaders.cacheControlHeader: "no-cache" }; if (header != null && header.isNotEmpty) { headers.addAll(header); } 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 { Map requestBody = {'username': username, 'password': password}; AuthResponse result; try { String response = await _post('/login', requestBody: requestBody); result = AuthResponse.fromMap(json.decode(response)); } catch (e, stack) { log.e("login", e, stack); result = new AuthResponse() ..message = 'Ошибка вызова сервера' ..operation = false; } return result; } Future authorization(String token) async { Map requestBody = {'token': token}; AuthResponse result; try { String response = await _post('/authorization', requestBody: requestBody); result = AuthResponse.fromMap(json.decode(response)); } catch (e, stack) { log.e("authorization", e, stack); result = new AuthResponse() ..message = 'Ошибка вызова сервера' ..operation = false; } return result; } Future auth(String token) async { Map headers = {HttpHeaders.authorizationHeader: 'Bearer $token'}; AuthResponse result; try { String response = await _post('/auth', header: headers); result = AuthResponse.fromMap(json.decode(response)); } catch (e, stack) { log.e("auth", e, stack); result = new AuthResponse() ..message = 'Ошибка вызова сервера' ..operation = false; } return result; } Future logout() async { Map headers = {HttpHeaders.authorizationHeader: 'Bearer $token'}; AuthResponse result; try { String response = await _post('/logout', header: headers); result = AuthResponse.fromMap(json.decode(response)); } catch (e, stack) { log.e("auth", e, stack); result = new AuthResponse() ..message = 'Ошибка вызова сервера' ..operation = false; } return result; } Future dictionaries(String target) async { Response result; try { Map headers = {HttpHeaders.authorizationHeader: 'Bearer $token'}; String response = await _post(target, header: headers); result = Response.fromMapList(json.decode(response), null); } catch (e, stack) { log.e("dictionaries", e, stack); result = new Response()..operation=false..list=[]; } return result; } }