You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
4.2 KiB
Dart
150 lines
4.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/http.dart';
|
|
import 'package:test_sa/models/user.dart';
|
|
|
|
class ApiManager {
|
|
ApiManager._();
|
|
|
|
Map<String, String> get _headers => {
|
|
'Content-Type': 'application/json',
|
|
if (user != null) 'Authorization': 'Bearer ${user.token}',
|
|
};
|
|
|
|
static ApiManager instance = ApiManager._();
|
|
|
|
User user;
|
|
|
|
Future<http.Response> get(
|
|
String url, {
|
|
Map<String, String> headers,
|
|
enableToastMessage = true,
|
|
}) async {
|
|
headers ??= {};
|
|
|
|
headers.addAll(_headers);
|
|
|
|
Uri _url = Uri.parse(url);
|
|
// print(_url);
|
|
// print(headers);
|
|
http.Response response = await http.get(_url, headers: headers);
|
|
if (jsonDecode(response.body) is Map<String, dynamic>) {
|
|
final message = jsonDecode(response.body)["message"];
|
|
if (message != null && message.toString().isNotEmpty && enableToastMessage) {
|
|
Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
|
|
}
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
Future<http.Response> post(
|
|
String url, {
|
|
Map<String, String> headers,
|
|
@required Map<String, dynamic> body,
|
|
}) async {
|
|
headers ??= {};
|
|
|
|
headers.addAll(_headers);
|
|
|
|
Uri _url = Uri.parse(url);
|
|
if (!kReleaseMode) {
|
|
print("URL:$_url");
|
|
print("Headers:$headers");
|
|
print("Body:$body");
|
|
}
|
|
|
|
var request = http.Request('POST', _url);
|
|
request.body = json.encode(body);
|
|
request.headers.addAll(headers);
|
|
|
|
http.StreamedResponse _streamedResponse = await request.send();
|
|
http.Response response = await http.Response.fromStream(_streamedResponse);
|
|
// print(response.statusCode);
|
|
// log(response.body);
|
|
|
|
if (jsonDecode(response.body) is Map<String, dynamic>) {
|
|
final message = jsonDecode(response.body)["message"];
|
|
if (message != null && message.toString().isNotEmpty) {
|
|
Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
|
|
}
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
Future<http.Response> put(
|
|
String url, {
|
|
Map<String, String> headers,
|
|
@required Map<String, dynamic> body,
|
|
}) async {
|
|
headers ??= {};
|
|
|
|
headers.addAll(_headers);
|
|
|
|
Uri _url = Uri.parse(url);
|
|
print(_url);
|
|
// print(headers);
|
|
// log(json.encode(body));
|
|
var request = http.Request('PUT', _url);
|
|
request.body = json.encode(body);
|
|
request.headers.addAll(headers);
|
|
|
|
http.StreamedResponse streamedResponse = await request.send();
|
|
http.Response response = await http.Response.fromStream(streamedResponse);
|
|
// print(response.statusCode);
|
|
// log(response.body);
|
|
if (jsonDecode(response.body) is Map<String, dynamic>) {
|
|
final message = jsonDecode(response.body)["message"];
|
|
if (message != null && message.toString().isNotEmpty) {
|
|
Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
|
|
}
|
|
}
|
|
return response;
|
|
}
|
|
|
|
Future<http.Response> multiPart(
|
|
String url, {
|
|
Map<String, String> headers,
|
|
@required Map<String, String> body,
|
|
@required List<Future<MultipartFile>> files,
|
|
}) async {
|
|
Map<String, String> _headers = const {
|
|
'Content-Type': 'multipart/form-data',
|
|
};
|
|
|
|
headers ??= {};
|
|
|
|
headers.addAll(_headers);
|
|
|
|
Uri _url = Uri.parse(url);
|
|
// print(_url);
|
|
// print(_headers);
|
|
// print(json.encode(body));
|
|
var request = http.MultipartRequest('POST', _url);
|
|
request.fields.addAll(body);
|
|
request.headers.addAll(_headers);
|
|
|
|
for (var element in files) {
|
|
request.files.add(await element);
|
|
}
|
|
|
|
//request.files.addAll(_files);
|
|
// print(request.files);
|
|
http.StreamedResponse streamedResponse = await request.send();
|
|
http.Response response = await http.Response.fromStream(streamedResponse);
|
|
// print(response.statusCode);
|
|
// log(response.body);
|
|
if (jsonDecode(response.body) is Map<String, dynamic>) {
|
|
final message = jsonDecode(response.body)["message"];
|
|
if (message != null && message.toString().isNotEmpty) {
|
|
Fluttertoast.showToast(msg: message ?? "", toastLength: Toast.LENGTH_LONG);
|
|
}
|
|
}
|
|
return response;
|
|
}
|
|
}
|