invokeAPI method

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

Implementation

Future<Response> invokeAPI(
  String path,
  String method,
  List<QueryParam> queryParams,
  Map<String, String> headerParams,
  String? nullableContentType,
) async {
  headerParams.addAll(_defaultHeaderMap);

  final urlEncodedQueryParams = queryParams.map((param) => '$param');

  final queryString = urlEncodedQueryParams.isNotEmpty
      ? '?${urlEncodedQueryParams.join('&')}'
      : '';

  final uri = Uri.parse('$basePath$path$queryString');

  if (nullableContentType != null) {
    headerParams['Content-Type'] = nullableContentType;
  }

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

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

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