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,
)

Invoke HTTP request We don't use a Map<String, String> for queryParams. If collectionFormat is 'multi' a key might appear multiple times.

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 MultipartRequestPlus) {
    final request = MultipartRequestPlus(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);
    }
  }
}