Skip to content

Commit

Permalink
Added documantations
Browse files Browse the repository at this point in the history
  • Loading branch information
Eldar2021 committed Aug 11, 2024
1 parent 54d6d6b commit 2685cf5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 44 deletions.
25 changes: 15 additions & 10 deletions packages/mq_remote_client/lib/src/http_exception.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import 'package:meta/meta.dart';

/// Exception class for remote MQ (Message Queue) errors.
/// [failureType] indicates the type of failure.
/// Optional [error], [message], [stackTrace],
/// and [statusCode] provide additional details.
@immutable
final class CustomException implements Exception {
const CustomException(
final class MqRemoteException implements Exception {
const MqRemoteException(
this.failureType, {
this.error,
this.message,
Expand All @@ -17,29 +21,30 @@ final class CustomException implements Exception {
final int? statusCode;
}

/// Enum representing different failure types.
enum FailureType {
/// Represents http error 400
/// HTTP 400 error.
badRequest('Represents http error '),

/// Represents http error 401
/// HTTP 401 error.
noAuthorization('Authentication error'),

/// Forbidden http error 403
/// HTTP 403 error.
forbidden('Forbidden http error'),

/// Internal server http error 500
/// HTTP 500 error.
internalServer('Internal server http error'),

/// Json decode error
/// JSON decoding error.
decode('Json decode error'),

/// Json deserialization error
/// JSON deserialization error.
deserialization('Json deserialization error'),

/// Internet Connection error
/// No internet connection error.
connection('Device unconected internet'),

/// Unknown error
/// Unknown error.
unknown('Unknown error');

const FailureType(this.message);
Expand Down
24 changes: 12 additions & 12 deletions packages/mq_remote_client/lib/src/mq_remote_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ class MqRemoteClient {
}

/// Makes a GET request to the given [url] and parses the response as type [T]
Future<Either<T, CustomException>> get<T>(String url) {
Future<Either<T, MqRemoteException>> get<T>(String url) {
return _get<T>(url);
}

/// Makes a GET request to the given [url] and parses the response as type [T]
/// from a JSON object.
///
/// The [fromJson] parameter is used to parse the JSON response.
Future<Either<T, CustomException>> getType<T>(
Future<Either<T, MqRemoteException>> getType<T>(
String url, {
required FromJson<T> fromJson,
}) async {
Expand All @@ -85,7 +85,7 @@ class MqRemoteClient {
/// of type [T].
///
/// The [fromJson] parameter is used to parse each JSON object in the list.
Future<Either<List<T>, CustomException>> getListOfType<T>(
Future<Either<List<T>, MqRemoteException>> getListOfType<T>(
String url, {
required FromJson<T> fromJson,
}) async {
Expand All @@ -95,7 +95,7 @@ class MqRemoteClient {

/// Makes a POST request to the given [url] with an optional [body] and parses
/// the response as type [T].
Future<Either<T, CustomException>> post<T>(
Future<Either<T, MqRemoteException>> post<T>(
String url, {
Map<String, dynamic>? body,
}) {
Expand All @@ -106,7 +106,7 @@ class MqRemoteClient {
/// the response as type [T] from a JSON object.
///
/// The [fromJson] parameter is used to parse the JSON response.
Future<Either<T, CustomException>> postType<T>(
Future<Either<T, MqRemoteException>> postType<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
Expand All @@ -119,7 +119,7 @@ class MqRemoteClient {
/// the response as a list of type [T].
///
/// The [fromJson] parameter is used to parse each JSON object in the list.
Future<Either<List<T>, CustomException>> postListOfType<T>(
Future<Either<List<T>, MqRemoteException>> postListOfType<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
Expand All @@ -130,7 +130,7 @@ class MqRemoteClient {

/// Makes a PUT request to the given [url] with an optional [body] and parses
/// the response as type [T].
Future<Either<T, CustomException>> put<T>(
Future<Either<T, MqRemoteException>> put<T>(
String url, {
Map<String, dynamic>? body,
}) {
Expand All @@ -141,7 +141,7 @@ class MqRemoteClient {
/// the response as type [T] from a JSON object.
///
/// The [fromJson] parameter is used to parse the JSON response.
Future<Either<T, CustomException>> putType<T>(
Future<Either<T, MqRemoteException>> putType<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
Expand All @@ -154,7 +154,7 @@ class MqRemoteClient {
/// the response as a list of type [T].
///
/// The [fromJson] parameter is used to parse each JSON object in the list.
Future<Either<List<T>, CustomException>> putListOfType<T>(
Future<Either<List<T>, MqRemoteException>> putListOfType<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
Expand All @@ -167,7 +167,7 @@ class MqRemoteClient {
/// parses the response as type [T].
///
/// The [fromJson] parameter is used to parse the JSON response.
Future<Either<T, CustomException>> patch<T>(
Future<Either<T, MqRemoteException>> patch<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
Expand All @@ -180,7 +180,7 @@ class MqRemoteClient {
/// parses the response as type [T] from a JSON object.
///
/// The [fromJson] parameter is used to parse the JSON response.
Future<Either<T, CustomException>> patchType<T>(
Future<Either<T, MqRemoteException>> patchType<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
Expand All @@ -193,7 +193,7 @@ class MqRemoteClient {
/// parses the response as a list of type [T].
///
/// The [fromJson] parameter is used to parse each JSON object in the list.
Future<Either<List<T>, CustomException>> patchListOfType<T>(
Future<Either<List<T>, MqRemoteException>> patchListOfType<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
part of 'mq_remote_client.dart';

/// Extension on [MqRemoteClient] providing base methods for HTTP operations.
extension MqRemoteClientBaseMehtods on MqRemoteClient {
Future<Either<T, CustomException>> _get<T>(String url) async {
/// Performs an HTTP GET request to the specified [url].
/// Returns either the response data [T] or an [MqRemoteException].
Future<Either<T, MqRemoteException>> _get<T>(String url) async {
try {
if (await network.checkInternetConnection()) {
final response = await dio.get<T>(url);
return Right(response.data as T);
} else {
return const Left(CustomException(FailureType.connection));
return const Left(MqRemoteException(FailureType.connection));
}
} on DioException catch (e) {
return Left(_parseDioException(e));
Expand All @@ -16,7 +19,9 @@ extension MqRemoteClientBaseMehtods on MqRemoteClient {
}
}

Future<Either<T, CustomException>> _post<T>(
/// Performs an HTTP POST request to the specified [url] with an optional [body].
/// Returns either the response data [T] or an [MqRemoteException].
Future<Either<T, MqRemoteException>> _post<T>(
String url, {
Map<String, dynamic>? body,
}) async {
Expand All @@ -25,7 +30,7 @@ extension MqRemoteClientBaseMehtods on MqRemoteClient {
final response = await dio.post<T>(url, data: body);
return Right(response.data as T);
} else {
return const Left(CustomException(FailureType.connection));
return const Left(MqRemoteException(FailureType.connection));
}
} on DioException catch (e) {
return Left(_parseDioException(e));
Expand All @@ -34,7 +39,9 @@ extension MqRemoteClientBaseMehtods on MqRemoteClient {
}
}

Future<Either<T, CustomException>> _put<T>(
/// Performs an HTTP PUT request to the specified [url] with an optional [body].
/// Returns either the response data [T] or an [MqRemoteException].
Future<Either<T, MqRemoteException>> _put<T>(
String url, {
Map<String, dynamic>? body,
}) async {
Expand All @@ -43,7 +50,7 @@ extension MqRemoteClientBaseMehtods on MqRemoteClient {
final response = await dio.put<T>(url, data: body);
return Right(response.data as T);
} else {
return const Left(CustomException(FailureType.connection));
return const Left(MqRemoteException(FailureType.connection));
}
} on DioException catch (e) {
return Left(_parseDioException(e));
Expand All @@ -52,7 +59,9 @@ extension MqRemoteClientBaseMehtods on MqRemoteClient {
}
}

Future<Either<T, CustomException>> _patch<T>(
/// Performs an HTTP PATCH request to the specified [url] with an optional [body].
/// Returns either the response data [T] or an [MqRemoteException].
Future<Either<T, MqRemoteException>> _patch<T>(
String url, {
Map<String, dynamic>? body,
}) async {
Expand All @@ -61,7 +70,7 @@ extension MqRemoteClientBaseMehtods on MqRemoteClient {
final response = await dio.patch<T>(url, data: body);
return Right(response.data as T);
} else {
return const Left(CustomException(FailureType.connection));
return const Left(MqRemoteException(FailureType.connection));
}
} on DioException catch (e) {
return Left(_parseDioException(e));
Expand All @@ -70,8 +79,10 @@ extension MqRemoteClientBaseMehtods on MqRemoteClient {
}
}

Future<Either<T, CustomException>> _convertType<T>({
required Either<Map<String, dynamic>, CustomException> jsonData,
/// Converts JSON data to a specific type [T] using [fromJson] function.
/// Returns either the converted type [T] or an [MqRemoteException].
Future<Either<T, MqRemoteException>> _convertType<T>({
required Either<Map<String, dynamic>, MqRemoteException> jsonData,
required T Function(Map<String, dynamic>) fromJson,
}) async {
try {
Expand All @@ -80,12 +91,14 @@ extension MqRemoteClientBaseMehtods on MqRemoteClient {
(e) => Right(fromJson(e)),
);
} catch (e) {
return Left(CustomException(FailureType.deserialization, message: '$e'));
return Left(MqRemoteException(FailureType.deserialization, message: '$e'));
}
}

Future<Either<List<T>, CustomException>> _convertListOfType<T>({
required Either<List<dynamic>, CustomException> jsonData,
/// Converts a list of JSON data to a list of type [T] using [fromJson] function.
/// Returns either the list of type [T] or an [MqRemoteException].
Future<Either<List<T>, MqRemoteException>> _convertListOfType<T>({
required Either<List<dynamic>, MqRemoteException> jsonData,
required T Function(Map<String, dynamic>) fromJson,
}) async {
try {
Expand All @@ -94,41 +107,43 @@ extension MqRemoteClientBaseMehtods on MqRemoteClient {
(r) => Right(r.map((e) => fromJson(e as Map<String, dynamic>)).toList()),
);
} catch (e) {
return Left(CustomException(FailureType.deserialization, message: '$e'));
return Left(MqRemoteException(FailureType.deserialization, message: '$e'));
}
}

CustomException _unknownExc(Object e, StackTrace? s) {
return CustomException(FailureType.unknown, error: e, stackTrace: s);
/// Creates an [MqRemoteException] for an unknown error.
MqRemoteException _unknownExc(Object e, StackTrace? s) {
return MqRemoteException(FailureType.unknown, error: e, stackTrace: s);
}

CustomException _parseDioException(DioException exception) {
/// Parses a [DioException] and returns an appropriate [MqRemoteException].
MqRemoteException _parseDioException(DioException exception) {
return switch (exception.response?.statusCode) {
400 => CustomException(
400 => MqRemoteException(
FailureType.badRequest,
statusCode: 400,
message: exception.message,
error: exception.error,
),
401 => CustomException(
401 => MqRemoteException(
FailureType.noAuthorization,
message: exception.message,
statusCode: 401,
error: exception.error,
),
403 => CustomException(
403 => MqRemoteException(
FailureType.forbidden,
message: exception.message,
statusCode: 403,
error: exception.error,
),
500 => CustomException(
500 => MqRemoteException(
FailureType.internalServer,
message: exception.message,
statusCode: 500,
error: exception.error,
),
_ => CustomException(
_ => MqRemoteException(
FailureType.unknown,
message: exception.message,
statusCode: exception.response?.statusCode,
Expand Down
5 changes: 5 additions & 0 deletions packages/mq_remote_client/lib/src/network_client.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/foundation.dart';

/// A client for checking the device's internet connection status.
@immutable
final class NetworkClient {
/// Creates a [NetworkClient] with the given [connectivity] instance.
const NetworkClient(this.connectivity);

/// The [Connectivity] instance used to check the connection type.
final Connectivity connectivity;

/// Checks if the device is connected to the internet via mobile, ethernet, or Wi-Fi.
/// Returns `true` if connected, `false` otherwise.
Future<bool> checkInternetConnection() async {
final connectivityResult = await connectivity.checkConnectivity();
return connectivityResult.any(
Expand Down

0 comments on commit 2685cf5

Please sign in to comment.