request<T> method

Future<FlintResponse<T>> request<T>(
  1. String method,
  2. String path, {
  3. dynamic body,
  4. Map<String, dynamic>? queryParameters,
  5. Map<String, String>? headers,
  6. JsonParser<T>? parser,
  7. ErrorHandler? onError,
  8. RequestDoneCallback<T>? onDone,
  9. Duration? requestTimeout,
})

Implementation

Future<FlintResponse<T>> request<T>(
  String method,
  String path, {
  dynamic body,
  Map<String, dynamic>? queryParameters,
  Map<String, String>? headers,
  JsonParser<T>? parser,
  ErrorHandler? onError,
  RequestDoneCallback<T>? onDone,
  Duration? requestTimeout,
}) async {
  final url = _url(path, queryParameters);
  final stopwatch = Stopwatch()..start();

  try {
    final response = await _send<T>(
      method.toUpperCase(),
      url,
      body: body,
      headers: headers,
      parser: parser,
      timeout: requestTimeout ?? timeout,
    );
    stopwatch.stop();
    final completed = FlintResponse<T>(
      statusCode: response.statusCode,
      data: response.data,
      type: response.type,
      headers: response.headers,
      url: Uri.parse(url),
      method: method,
      duration: stopwatch.elapsed,
      statusConfig: statusCodeConfig,
    );
    _finish(completed, null, onDone);
    return completed;
  } catch (error) {
    stopwatch.stop();
    final flintError = error is FlintError
        ? error
        : FlintError.fromException(
            error,
            url: Uri.parse(url),
            method: method,
          );
    final errorResponse = FlintResponse<T>.error(
      flintError,
      method: method,
      duration: stopwatch.elapsed,
      statusConfig: statusCodeConfig,
    );
    _finish(errorResponse, flintError, onDone);
    final handler = onError ?? this.onError;
    handler?.call(flintError);
    if (throwIfError) throw flintError;
    return errorResponse;
  }
}