Skip to content

Commit

Permalink
Merge pull request #230 from Eldar2021/el/implement-dio
Browse files Browse the repository at this point in the history
Implement dio
  • Loading branch information
Eldar2021 authored Jul 13, 2024
2 parents 59f9e4b + 8db656e commit 0053d25
Show file tree
Hide file tree
Showing 26 changed files with 438 additions and 203 deletions.
15 changes: 6 additions & 9 deletions app/lib/app/data/source/auth_remote_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class AuthRemoteDataSource {
required this.isIntegrationTest,
});

final RemoteClient client;
final MqDio client;
final PreferencesStorage storage;
final SoccialAuth soccialAuth;
final bool isIntegrationTest;
Expand All @@ -25,7 +25,7 @@ final class AuthRemoteDataSource {
) async {
final googleAuth = await _getGoogleAuth();

final token = await client.post(
final token = await client.postType(
apiConst.loginWithGoogle,
fromJson: TokenResponse.fromJson,
body: {'access_token': googleAuth.accessToken},
Expand Down Expand Up @@ -68,7 +68,7 @@ final class AuthRemoteDataSource {
) async {
final appleAuth = await _getAppleAuth();

final token = await client.post(
final token = await client.postType(
apiConst.loginWithApple,
fromJson: TokenResponse.fromJson,
body: {'access_token': appleAuth.accessToken},
Expand Down Expand Up @@ -106,38 +106,35 @@ final class AuthRemoteDataSource {
}

Future<Either<UserDataResponse, Exception>> saveUserData(UserEntity userEntity) {
return client.put(
return client.putType(
apiConst.putProfile(userEntity.accessToken),
fromJson: UserDataResponse.fromJson,
body: {
'gender': userEntity.gender.name.toUpperCase(),
'language': userEntity.localeCode.toUpperCase(),
},
token: userEntity.accessToken,
);
}

Future<Either<UserDataResponse, Exception>> pathGender({
required String userId,
required Gender gender,
}) {
return client.patch(
return client.patchType(
apiConst.putProfile(userId),
fromJson: UserDataResponse.fromJson,
body: {'gender': gender.name.toUpperCase()},
token: userId,
);
}

Future<Either<UserDataResponse, Exception>> pathLocaleCode({
required String userId,
required String localeCode,
}) {
return client.patch(
return client.patchType(
apiConst.putProfile(userId),
fromJson: UserDataResponse.fromJson,
body: {'language': localeCode.toUpperCase()},
token: userId,
);
}

Expand Down
4 changes: 2 additions & 2 deletions app/lib/app/presentation/view/app_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MyApp extends StatelessWidget {
context.read<PreferencesStorage>(),
),
remoteDataSource: AuthRemoteDataSource(
client: context.read<RemoteClient>(),
client: context.read<MqDio>(),
storage: context.read<PreferencesStorage>(),
soccialAuth: context.read<SoccialAuth>(),
isIntegrationTest: context.read<AppConfig>().isIntegrationTest,
Expand All @@ -68,7 +68,7 @@ class MyApp extends StatelessWidget {
GetHomeDataUseCase(
HomeRepositoryImpl(
HomeLocalDataSource(context.read<PreferencesStorage>()),
HomeRemoteDataSource(context.read<RemoteClient>()),
HomeRemoteDataSource(context.read<MqDio>()),
),
),
),
Expand Down
7 changes: 5 additions & 2 deletions app/lib/constants/keys/storage_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ final class StorageKeys {

static String get localeKey => apiConst.isDevmode ? _localeKeyDev : _localeKey;
static String get tokenKey => apiConst.isDevmode ? _tokenKeyDev : _tokenKey;
static String get oldTokenKey => apiConst.isDevmode ? _oldTokenKeyDev : _oldTokenKey;
static String get genderKey => apiConst.isDevmode ? _genderKeyDev : _genderKey;
static String get usernameKey => apiConst.isDevmode ? _usernameKeyDev : _usernameKey;
static String get modeKey => apiConst.isDevmode ? _modeKeyDev : _modeKey;
static String get readThemeKey => apiConst.isDevmode ? _readThemeKeyDev : _readThemeKey;
static String get colorKey => apiConst.isDevmode ? _colorKeyDev : _colorKey;

static const _localeKey = 'locale';
static const _tokenKey = 'token';
static const _tokenKey = 'token-new';
static const _oldTokenKey = 'token';
static const _genderKey = 'gender';
static const _usernameKey = 'username';
static const _modeKey = 'mode';
static const _readThemeKey = 'readThemeKey';
static const _colorKey = 'color';

static const _localeKeyDev = 'locale-dev';
static const _tokenKeyDev = 'token-dev';
static const _tokenKeyDev = 'token-dev-new';
static const _oldTokenKeyDev = 'token-dev';
static const _genderKeyDev = 'gender-dev';
static const _usernameKeyDev = 'username-dev';
static const _modeKeyDev = 'mode-dev';
Expand Down
48 changes: 48 additions & 0 deletions app/lib/core/client/http_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:meta/meta.dart';

@immutable
final class CustomException implements Exception {
const CustomException(
this.failureType, {
this.error,
this.message,
this.stackTrace,
this.statusCode,
});

final dynamic error;
final FailureType failureType;
final StackTrace? stackTrace;
final String? message;
final int? statusCode;
}

enum FailureType {
/// Represents http error 400
badRequest('Represents http error '),

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

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

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

/// Json decode error
decode('Json decode error'),

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

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

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

const FailureType(this.message);

final String message;
}
151 changes: 151 additions & 0 deletions app/lib/core/client/mq_dio.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:my_quran/core/core.dart';

part 'mq_dio_base_extension.dart';

typedef FromJson<T> = T Function(Map<String, dynamic>);
typedef ResolveValue = String? Function();

@immutable
class MqDio {
const MqDio({
required this.dio,
required this.network,
this.language,
this.token,
this.oldToken,
});

final Dio dio;
final NetworkClient network;
final ResolveValue? language;
final ResolveValue? token;
final ResolveValue? oldToken;

void initilize() {
dio.interceptors.add(
InterceptorsWrapper(
onRequest: (options, handler) {
final tokenValue = token?.call();
final languageValue = language?.call();
final oldTokenValue = oldToken?.call();
options.headers = {
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json',
'X-CSRFTOKEN': '9KDITf4aeXMyyQffH5TMtuuUtSfOSLtnZIeF2JZBXJziDfbP0wLo7xrWsUVeL2wO',
if (tokenValue != null) 'Authorization': 'Token $tokenValue',
if (languageValue != null) 'Accept-Language': languageValue,
if (oldTokenValue != null) 'old-token': 'Old Token $oldTokenValue',
};
return handler.next(options);
},
),
);
}

Future<Either<T, CustomException>> get<T>(
String url, {
required FromJson<T> fromJson,
}) async {
final response = await _get<T>(url);
return response;
}

Future<Either<T, CustomException>> getType<T>(
String url, {
required FromJson<T> fromJson,
}) async {
final data = await _get<Map<String, dynamic>>(url);
return _convertType<T>(jsonData: data, fromJson: fromJson);
}

Future<Either<List<T>, CustomException>> getListOfType<T>(
String url, {
required FromJson<T> fromJson,
}) async {
final data = await _get<List<dynamic>>(url);
return _convertListOfType(jsonData: data, fromJson: fromJson);
}

Future<Either<T, CustomException>> post<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
}) async {
final response = await _post<T>(url, body: body);
return response;
}

Future<Either<T, CustomException>> postType<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
}) async {
final data = await _post<Map<String, dynamic>>(url, body: body);
return _convertType<T>(jsonData: data, fromJson: fromJson);
}

Future<Either<List<T>, CustomException>> postListOfType<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
}) async {
final data = await _post<List<dynamic>>(url, body: body);
return _convertListOfType(jsonData: data, fromJson: fromJson);
}

Future<Either<T, CustomException>> put<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
}) async {
final response = await _put<T>(url, body: body);
return response;
}

Future<Either<T, CustomException>> putType<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
}) async {
final data = await _put<Map<String, dynamic>>(url, body: body);
return _convertType<T>(jsonData: data, fromJson: fromJson);
}

Future<Either<List<T>, CustomException>> putListOfType<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
}) async {
final data = await _put<List<dynamic>>(url, body: body);
return _convertListOfType(jsonData: data, fromJson: fromJson);
}

Future<Either<T, CustomException>> patch<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
}) async {
final response = await _patch<T>(url, body: body);
return response;
}

Future<Either<T, CustomException>> patchType<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
}) async {
final data = await _patch<Map<String, dynamic>>(url, body: body);
return _convertType<T>(jsonData: data, fromJson: fromJson);
}

Future<Either<List<T>, CustomException>> patchListOfType<T>(
String url, {
required FromJson<T> fromJson,
Map<String, dynamic>? body,
}) async {
final data = await _patch<List<dynamic>>(url, body: body);
return _convertListOfType(jsonData: data, fromJson: fromJson);
}
}
Loading

0 comments on commit 0053d25

Please sign in to comment.