postAPI method

Future<BaseResponse> postAPI(
  1. String url,
  2. String method,
  3. dynamic data, {
  4. bool hasHeader = true,
  5. bool fullPath = false,
  6. int timeout = 20,
  7. Map<String, String>? body,
  8. List<String>? files,
  9. List<FileByte>? realFiles,
  10. String paramFile = 'attachment[file][]',
})

Implementation

Future<BaseResponse> postAPI(String url, String method, data,
    {bool hasHeader = true, bool fullPath = false, int timeout = 20,
    Map<String, String>? body,
    List<String>? files,
    List<FileByte>? realFiles,
    String paramFile = 'attachment[file][]'}) async {
  final baseResponse = BaseResponse();
  try {
    final request = http.MultipartRequest(method, Uri.parse(fullPath ? url : (Constants().baseUrl + url)));

    if (hasHeader) request.headers.addAll(await _getHeader());

    if (body != null) request.fields.addAll(body);

    if (files != null && files.isNotEmpty) {
      for (String path in files)
        request.files.add(_createPartFile(File(path), paramFile));
    } else if (realFiles != null) {
      for (FileByte file in realFiles)
        request.files.add(_createPartFileByte(file, paramFile));
    }

    final streamResponse = await request.send().timeout(Duration(seconds: request.files.isEmpty ? timeout : Constants().timeout));
    final response = await http.Response.fromStream(streamResponse);
    if (showLog != null && showLog!) {
      logDev.log('\n\nadvn-request url: ' + request.url.toString());
      logDev.log('\nadvn-request body: $body');
      logDev.log('\nadvn-response: ${response.body}');
    }
    baseResponse.fromJson(jsonDecode(response.body), data);
  } catch (e) {
    return _responseError(baseResponse, e);
  }
  return baseResponse;
}