initiateRequest static method

Future<RequestResponse<FrappeResponse?>> initiateRequest({
  1. required String url,
  2. required HttpMethod method,
  3. String? contentType,
  4. Map<String, dynamic>? headers,
  5. Map<String, dynamic>? data,
  6. Map<String, dynamic>? queryParams,
  7. bool isFrappeResponse = true,
})

Implementation

static Future<RequestResponse<FrappeResponse?>> initiateRequest(
    {required String url,
    required HttpMethod method,
    String? contentType,
    Map<String, dynamic>? headers,
    Map<String, dynamic>? data,
    Map<String, dynamic>? queryParams,
    bool isFrappeResponse = true}) async {
  Response<dynamic>? response;
  RequestResponse<FrappeResponse?>? r;
  try {
    response = await Request._httpRequest(url, method, headers,
        contentType: contentType, data: data, queryParams: queryParams);

    final frappeResponse = _buildFrappeResponse(response, isFrappeResponse);

    if ((response.statusCode! / 100).floor() == 2) {
      r = RequestResponse.success<FrappeResponse>(frappeResponse);
    } else {
      final info =
          Information(httpCode: response.statusCode, data: response.data);
      final errorDetail = ErrorDetail(info: info);
      r = RequestResponse.fail(errorDetail);
    }
  } on DioError catch (err) {
    if (err.response != null) {
      final frappeResponse =
          _buildFrappeResponse(err.response!, isFrappeResponse);
      final info = Information(
          httpCode: err.response?.statusCode,
          data: frappeResponse,
          rawError: err);
      final errorDetail = ErrorDetail(info: info);
      r = RequestResponse.fail(errorDetail);
    }
  }

  if (r != null) {
    _messageChecker(r);
    _errorChecker(r);
  } else {
    throw RequestException(
        cause: 'No FrappeResponse after initiateRequest',
        contentType: contentType,
        url: url,
        data: data,
        queryParams: queryParams,
        headers: headers);
  }

  r.rawResponse = response as Response<String>?;
  return r;
}