You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.0 KiB
77 lines
2.0 KiB
import 'dart:io'; |
|
|
|
import 'package:dio/dio.dart'; |
|
|
|
abstract class BaseRepository { |
|
final Dio dio; |
|
BaseRepository({required this.dio}); |
|
|
|
Future<dynamic> get({ |
|
required String service, |
|
CancelToken? cancelToken, |
|
}) async { |
|
try { |
|
final response = await dio.get( |
|
service, |
|
cancelToken: cancelToken, |
|
options: Options( |
|
headers: { |
|
HttpHeaders.contentTypeHeader: "application/json", |
|
}, |
|
contentType: "application/json", |
|
), |
|
); |
|
if (response.statusCode != 200) { |
|
throw BaseRepositoryException( |
|
message: "Invalid Http Response ${response.statusCode}", |
|
); |
|
} |
|
return response.data; |
|
} on DioError catch (e) { |
|
throw BaseRepositoryException(message: e.message); |
|
} on SocketException catch (e) { |
|
throw BaseRepositoryException(message: e.message); |
|
} on BaseRepositoryException catch (e) { |
|
throw BaseRepositoryException(message: e.message); |
|
} |
|
} |
|
|
|
Future<dynamic> post({ |
|
required String service, |
|
required Map<String, dynamic> jsonParam, |
|
CancelToken? cancelToken, |
|
}) async { |
|
try { |
|
final response = await dio.post( |
|
service, |
|
data: jsonParam, |
|
cancelToken: cancelToken, |
|
options: Options( |
|
headers: { |
|
HttpHeaders.contentTypeHeader: "application/json", |
|
}, |
|
contentType: "application/json", |
|
), |
|
); |
|
if (response.statusCode != 200) { |
|
throw BaseRepositoryException( |
|
message: "Invalid Http Response ${response.statusCode}", |
|
); |
|
} |
|
return response.data; |
|
} on DioError catch (e) { |
|
throw BaseRepositoryException(message: e.message); |
|
} on SocketException catch (e) { |
|
throw BaseRepositoryException(message: e.message); |
|
} on BaseRepositoryException catch (e) { |
|
throw BaseRepositoryException(message: e.message); |
|
} |
|
} |
|
} |
|
|
|
class BaseRepositoryException implements Exception { |
|
final String message; |
|
BaseRepositoryException({ |
|
required this.message, |
|
}); |
|
}
|
|
|