invokeAPI method

Future<Response> invokeAPI(
  1. String path,
  2. String method,
  3. List<QueryParam> queryParams,
  4. Map<String, String> headerParams,
  5. String? contentType,
)

Implementation

Future<Response> invokeAPI(
  String path,
  String method,
  List<QueryParam> queryParams,
  // Object? body,
  Map<String, String> headerParams,
  // Map<String, String> formParams,
  String? contentType, //todo check
) async {
  headerParams.addAll(_defaultHeaderMap);
  if (contentType != null) {
    headerParams['Content-Type'] = contentType;
  }

  String basePath = (env == Environment.PRODUCTION)
      ? "https://core.dxpapi.com/api/v1/core"
      : "https://staging-core.dxpapi.com/api/v1/core/";

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

  try {
    final nullableHeaderParams = headerParams.isEmpty ? null : headerParams;

    switch (method) {
      case 'GET':
        return await _client.get(
          uri,
          headers: nullableHeaderParams,
        );
    }
  } on SocketException catch (error, trace) {
    throw ApiException.withInner(
      HttpStatus.badRequest,
      'Socket operation failed: $method $path',
      error,
      trace,
    );
  } on TlsException catch (error, trace) {
    throw ApiException.withInner(
      HttpStatus.badRequest,
      'TLS/SSL communication failed: $method $path',
      error,
      trace,
    );
  } on IOException catch (error, trace) {
    throw ApiException.withInner(
      HttpStatus.badRequest,
      'I/O operation failed: $method $path',
      error,
      trace,
    );
  } on ClientException catch (error, trace) {
    throw ApiException.withInner(
      HttpStatus.badRequest,
      'HTTP connection failed: $method $path',
      error,
      trace,
    );
  } on Exception catch (error, trace) {
    throw ApiException.withInner(
      HttpStatus.badRequest,
      'Exception occurred: $method $path',
      error,
      trace,
    );
  }

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