invokeAPI method

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

Actually make an API request and return the HTTP Response asynchronously. This function will wrap all errors with MmApiException

Implementation

Future<Response> invokeAPI(
  String path,
  String method,
  List<MmQueryParam> queryParams,
  Object? body,
  Map<String, String> headerParams,
  Map<String, String> formParams,
  String? contentType,
) async {
  _updateParamsForAuth(queryParams, headerParams);

  headerParams.addAll(_defaultHeaderMap);
  if (contentType != null) {
    headerParams['Content-Type'] = contentType;
  }

  final urlEncodedMmQueryParams = queryParams.map((param) => '$param');
  final queryString = urlEncodedMmQueryParams.isNotEmpty ? '?${urlEncodedMmQueryParams.join('&')}' : '';
  final uri = Uri.parse('$basePath$path$queryString');

  try {
    // Special case for uploading a single file which isn't a 'multipart/form-data'.
    if (body is MultipartFile &&
        (contentType == null || !contentType.toLowerCase().startsWith('multipart/form-data'))) {
      final request = StreamedRequest(method, uri);
      request.headers.addAll(headerParams);
      request.contentLength = body.length;
      body.finalize().listen(
            request.sink.add,
            onDone: request.sink.close,
            // ignore: avoid_types_on_closure_parameters
            onError: (Object error, StackTrace trace) => request.sink.close(),
            cancelOnError: true,
          );
      final response = await _client.send(request);
      return Response.fromStream(response);
    }

    if (body is MultipartRequest) {
      final request = MultipartRequest(method, uri);
      request.fields.addAll(body.fields);
      request.files.addAll(body.files);
      request.headers.addAll(body.headers);
      request.headers.addAll(headerParams);
      final response = await _client.send(request);
      return Response.fromStream(response);
    }

    final msgBody = contentType == 'application/x-www-form-urlencoded' ? formParams : await serializeAsync(body);
    final nullableHeaderParams = headerParams.isEmpty ? null : headerParams;

    switch (method) {
      case 'POST':
        return await _client.post(
          uri,
          headers: nullableHeaderParams,
          body: msgBody,
        );
      case 'PUT':
        return await _client.put(
          uri,
          headers: nullableHeaderParams,
          body: msgBody,
        );
      case 'DELETE':
        return await _client.delete(
          uri,
          headers: nullableHeaderParams,
          body: msgBody,
        );
      case 'PATCH':
        return await _client.patch(
          uri,
          headers: nullableHeaderParams,
          body: msgBody,
        );
      case 'HEAD':
        return await _client.head(
          uri,
          headers: nullableHeaderParams,
        );
      case 'GET':
        return await _client.get(
          uri,
          headers: nullableHeaderParams,
        );
    }
  } on SocketException catch (error, trace) {
    throw MmApiException.withInner(
      HttpStatus.badRequest,
      'Socket operation failed: $method $path',
      error,
      trace,
    );
  } on TlsException catch (error, trace) {
    throw MmApiException.withInner(
      HttpStatus.badRequest,
      'TLS/SSL communication failed: $method $path',
      error,
      trace,
    );
  } on IOException catch (error, trace) {
    throw MmApiException.withInner(
      HttpStatus.badRequest,
      'I/O operation failed: $method $path',
      error,
      trace,
    );
  } on ClientException catch (error, trace) {
    throw MmApiException.withInner(
      HttpStatus.badRequest,
      'HTTP connection failed: $method $path',
      error,
      trace,
    );
  } on Exception catch (error, trace) {
    throw MmApiException.withInner(
      HttpStatus.badRequest,
      'Exception occurred: $method $path',
      error,
      trace,
    );
  }

  throw MmApiException(
    HttpStatus.badRequest,
    'Invalid HTTP operation: $method $path',
  );
}