invokeAPI method

Future<Response> invokeAPI(
  1. String path,
  2. String method,
  3. List<QueryParam> queryParams,
  4. Object? body,
  5. Map<String, String> headerParams,
  6. Map<String, String> formParams,
  7. String contentType,
  8. List<String> authNames,
)

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);
    }
  }
}