invokeAPI method
Implementation
Future<Http.Response> invokeAPI(
String path,
String method,
List<QueryParam> queryParams,
Object? body,
Map<String, String> headerParams,
Map<String, String> formParams,
String contentType,
List<String> authNames) async {
await _updateParamsForAuth(queryParams, headerParams);
final ps = queryParams.map((p) => '${p.name}=${p.value}');
final String queryString = ps.isNotEmpty ? '?' + ps.join('&') : '';
final String url = basePath + path + queryString;
headerParams.addAll(_defaultHeaderMap);
headerParams['Content-Type'] = contentType;
if (body is Http.MultipartRequest) {
final request = Http.MultipartRequest(method, Uri.parse(url));
request.fields.addAll(body.fields);
request.files.addAll(body.files);
request.headers.addAll(body.headers);
request.headers.addAll(headerParams);
final response = await httpClient.send(request);
return Http.Response.fromStream(response);
} else {
final msgBody = contentType == "application/x-www-form-urlencoded"
? formParams
: serialize(body);
switch (method) {
case "POST":
return httpClient.post(Uri.parse(url),
headers: headerParams, body: msgBody);
case "PUT":
return httpClient.put(Uri.parse(url),
headers: headerParams, body: msgBody);
case "DELETE":
return httpClient.delete(Uri.parse(url), headers: headerParams);
case "PATCH":
return httpClient.patch(Uri.parse(url),
headers: headerParams, body: msgBody);
default:
return httpClient.get(Uri.parse(url), headers: headerParams);
}
}
}