52 lines
1.7 KiB
Dart
52 lines
1.7 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:aman_kassa_flutter/core/base/base_service.dart';
|
|
import 'package:aman_kassa_flutter/core/models/session.dart';
|
|
import '../models/authResponse.dart';
|
|
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
/// The service responsible for networking requests
|
|
class ApiService extends BaseService {
|
|
static const endpoint = 'https://kassa-test.aman.com.kz/ru/api/v2';
|
|
|
|
var client = new http.Client();
|
|
|
|
Future<AuthBody> authenticate(String email, String password) async {
|
|
Map<String, String> requestBody = <String, String>{
|
|
'email': email,
|
|
'password': password
|
|
};
|
|
var response = await requestFormData('/authenticate', requestBody);
|
|
final respStr = await response.stream.bytesToString();
|
|
print(respStr);
|
|
AuthResponse aman = AuthResponse.fromJson(json.decode(respStr));
|
|
return aman.body;
|
|
}
|
|
|
|
Future<Session> isActive(String token) async {
|
|
Map<String, String> requestBody = <String, String>{'api_token': token};
|
|
var response = await requestFormData('/test_auth', requestBody);
|
|
final respStr = await response.stream.bytesToString();
|
|
return Session.fromData(token, json.decode(respStr)['body']);
|
|
}
|
|
|
|
Future<http.StreamedResponse> requestFormData(
|
|
String point, Map<String, String> requestBody) async {
|
|
Map<String, String> headers = <String, String>{
|
|
HttpHeaders.contentTypeHeader: "multipart/form-data",
|
|
HttpHeaders.cacheControlHeader: "no-cache"
|
|
};
|
|
|
|
var uri = Uri.parse('$endpoint$point');
|
|
var request = http.MultipartRequest('POST', uri)
|
|
..headers.addAll(
|
|
headers) //if u have headers, basic auth, token bearer... Else remove line
|
|
..fields.addAll(requestBody);
|
|
return await request.send();
|
|
}
|
|
|
|
}
|