requestApi method
dynamic
requestApi(
- RequestData? data,
- CustomRequestData? customData,
- dynamic header,
- RequestApiHelperConfigData config,
- dynamic name,
- RESTAPI type,
- OnUploadProgressCallback? onUploadProgress,
Implementation
requestApi(RequestData? data, CustomRequestData? customData, header, RequestApiHelperConfigData config, name, RESTAPI type, OnUploadProgressCallback? onUploadProgress) async {
Map<String, String> _header = header;
List<String> _file = [];
List<String> _requestNameFile = [];
Uri _http = Uri.parse('');
_isFile = false;
_body = {};
if (config.version != null) {
_body!.addAll({'version': config.version});
}
String? rawJson;
if (type == RESTAPI.get) await bodyCheck(data, customData, config, type);
if (customData != null) {
if (customData.file != null) {
_file = customData.file!.filePath;
_requestNameFile = customData.file!.fileRequestName;
_isFile = true;
}
if (type != RESTAPI.get && customData.body != null) _body = customData.body!;
if (customData.url != null) {
_http = Uri.parse(customData.url! + _request);
}
if (customData.header != null) _header = customData.header!;
if (customData.rawJson != null) rawJson = customData.rawJson!;
} else {
if (data != null) {
if (data.file != null) {
_file = data.file!.filePath;
_requestNameFile = data.file!.fileRequestName;
_isFile = true;
}
_body = data.body;
}
_http = Uri.parse(config.url! + name! + (type != RESTAPI.post ? _request : ''));
_body ??= {};
if (data?.rawJson != null) {
rawJson = data!.rawJson!;
_header.addAll({
'content-type': 'application/json',
});
}
}
if (type == RESTAPI.get) {
_response = await http.get(_http, headers: _header).timeout(config.timeout ?? const Duration(milliseconds: 10000));
} else if (type == RESTAPI.post) {
if (_file.isNotEmpty) {
final httpClients = getHttpClient(
duration: config.timeout,
);
final requests = await httpClients.postUrl(_http);
int byteCount = 0;
var request = http.MultipartRequest("POST", _http);
if (config.version != null) {
request.fields['version'] = config.version!;
}
if (_body != null) {
if (_body is Map) {
_body!.forEach((k, v) {
if (k != 'version') {
request.fields[k] = v;
}
});
}
}
for (int counterfile = 0; counterfile < _file.length; counterfile++) {
if (_file[counterfile] == '' || _file[counterfile] == 'null') {
request.fields[_requestNameFile[counterfile]] = 'null';
} else {
request.files.add(await http.MultipartFile.fromPath(_requestNameFile[counterfile], _file[counterfile], contentType: MediaType('application', _file[counterfile].split('.').last)));
}
}
Stream<List<int>>? streamUpload;
request.headers.addAll(_header);
if (onUploadProgress != null) {
var msStream = request.finalize();
var totalByteLength = request.contentLength;
String decodes = request.headers[HttpHeaders.contentTypeHeader] ?? '';
requests.headers.add(HttpHeaders.authorizationHeader, {request.headers[HttpHeaders.authorizationHeader]});
requests.headers.set(HttpHeaders.contentTypeHeader, {decodes});
streamUpload = msStream.transform(
StreamTransformer.fromHandlers(
handleData: (data, sink) {
sink.add(data);
byteCount += data.length;
onUploadProgress(byteCount, totalByteLength);
},
handleError: (error, stack, sink) {
throw error;
},
handleDone: (sink) {
sink.close();
// UPLOAD DONE;
},
),
);
}
if (streamUpload != null) {
await requests.addStream(streamUpload);
_response = await requests.close();
} else {
_response = await request.send().timeout(config.timeout ?? const Duration(milliseconds: 120000));
}
} else {
final rawJsons = rawJson != null ? utf8.encode(rawJson) : null;
_response = await http.post(_http, body: rawJsons ?? _body, headers: _header).timeout(config.timeout ?? const Duration(milliseconds: 120000));
}
} else if (type == RESTAPI.put) {
final rawJsons = rawJson != null ? utf8.encode(rawJson) : null;
_response = await http.put(_http, headers: _header, body: rawJsons ?? _body).timeout(config.timeout ?? const Duration(milliseconds: 10000));
} else if (type == RESTAPI.delete) {
final rawJsons = rawJson != null ? utf8.encode(rawJson) : null;
_response = await http.delete(_http, headers: _header, body: rawJsons ?? _body).timeout(config.timeout ?? const Duration(milliseconds: 10000));
} else if (type == RESTAPI.path) {
final rawJsons = rawJson != null ? utf8.encode(rawJson) : null;
_response = await http.patch(_http, headers: _header, body: rawJsons ?? _body).timeout(config.timeout ?? const Duration(milliseconds: 10000));
}
}