import 'dart:convert'; import 'package:unit2/utils/request.dart'; import 'package:unit2/utils/urls.dart'; import '../model/profile/attachment.dart'; import 'package:http/http.dart' as http; class AttachmentServices { static final AttachmentServices _instance = AttachmentServices(); static AttachmentServices get instance => _instance; Future> getCategories() async { List attachmentCategories = []; String path = Url.instance.attachmentCategories(); Map headers = { 'Content-Type': 'application/json; charset=UTF-8', }; try { http.Response response = await Request.instance .getRequest(param: {}, path: path, headers: headers); if (response.statusCode == 200) { Map data = jsonDecode(response.body); for (var cat in data['data']) { AttachmentCategory newCat = AttachmentCategory.fromJson(cat); attachmentCategories.add(newCat); } } } catch (e) { throw e.toString(); } return attachmentCategories; } Future> attachment( {required String categoryId, required String module, required List paths, required String token, required String profileId}) async { String authtoken = "Token $token"; Map headers = {'Authorization': authtoken}; String path = Url.instance.attachments(); Map? response = {}; Map body = { "attachment_category_id": categoryId.toString(), "attachment_module": module.toString() }; try { var request = http.MultipartRequest( 'POST', Uri.parse('http://${Url.instance.host()}$path$profileId/')); request.fields.addAll(body); request.headers.addAll(headers); paths.forEach((element) async { request.files .add(await http.MultipartFile.fromPath('attachments', element)); }); http.StreamedResponse res = await request.send(); final steamResponse = await res.stream.bytesToString(); Map data = jsonDecode(steamResponse); if (res.statusCode == 201) { response = data; } else { String message = data['response']['details']; response.addAll({'message': message}); response.addAll( {'success': false}, ); } } catch (e) { throw e.toString(); } return response; } }