Advertisement
McFamous

Untitled

Jul 24th, 2023
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.36 KB | None | 0 0
  1.   Future<Result<List<T>>> requestPayload<T>({
  2.     required HttpMethod method,
  3.     required String path,
  4.     required T Function(Object? json) fromJsonT,
  5.     Map<String, dynamic>? data,
  6.     String? contentType,
  7.     Map<String, dynamic>? parameters,
  8.     String? language,
  9.   }) async {
  10.     try {
  11.       final Map<String, String> headers = language == null ? {} : {"Content-Language": language};
  12.       final Response response = await request(
  13.         path,
  14.         data: data,
  15.         queryParameters: parameters,
  16.         options: Options(method: method.name, contentType: contentType, headers: headers.isEmpty ? null : headers),
  17.       );
  18.  
  19.       final ApiResponse<T> apiResponse = ApiResponse.fromJson(response.data, (data) => fromJsonT(data));
  20.       final Payload<T> result = apiResponse.result!;
  21.  
  22.       return Result.success(result.items);
  23.     } on DioException catch (e) {
  24.       final Response? errorResponse = e.response;
  25.       ApiError? error;
  26.  
  27.       if (errorResponse != null) {
  28.         final ApiResponse<ApiError> apiResponse = ApiResponse.fromJson(
  29.           errorResponse.data,
  30.           (data) => ApiError.fromJson(data as Map<String, dynamic>),
  31.         );
  32.         final Payload<ApiError>? errors = apiResponse.errors;
  33.         error = errors?.items[0];
  34.       }
  35.  
  36.       return Result.error(error?.toNetworkException() ?? const NetworkException());
  37.     }
  38.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement