-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from Eldar2021/el/implement-dio
Implement dio
- Loading branch information
Showing
26 changed files
with
438 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.