multipart method

Future<Response?> multipart(
  1. String method,
  2. String apiPath, {
  3. Map<String, String>? fields,
  4. List<MultipartFile>? files,
})

Executes a HTTP Multipart request for the given apiPath and method.

Supports an optional fields and files.

Implementation

Future<Response?> multipart(
  String method,
  String apiPath, {
  Map<String, String>? fields,
  List<MultipartFile>? files,
}) async {
  Uri apiUri = _generateApiUri(apiPath);

  return _processApiCall(
    httpApiConfig.maxRetryAttempts,
    apiPath: apiPath,
    apiCall: () async {
      final request = MultipartRequest(method, apiUri);

      request.headers.addAll(httpApiConfig.headers);

      if (fields != null) {
        request.fields.addAll(fields);
      }
      if (files != null) {
        request.files.addAll(files);
      }

      final streamedResponse = await httpClient
          .send(request)
          .timeout(
            Duration(seconds: httpApiConfig.timeout),
            onTimeout: () => throw Exception("API Timeout exception, no response from the server for: $apiPath"),
          );
      return await Response.fromStream(streamedResponse);
    },
  );
}