Init core module
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
class Constant {
|
||||
static const userId = '_id';
|
||||
static const firstName = 'firstname';
|
||||
static const lastName = 'lastname';
|
||||
static const acceptHeaderDefault = 'application/json';
|
||||
static const contentTypeHeaderDefault = 'application/json';
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
class AuthorizationInterceptors extends InterceptorsWrapper {
|
||||
String? _authorization;
|
||||
|
||||
void changeAuthorization(String? userName, String? password) {
|
||||
_authorization = base64Encode(utf8.encode('$userName:$password'));
|
||||
}
|
||||
|
||||
@override
|
||||
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
|
||||
if (_authorization != null) {
|
||||
options.headers['Authorization'] = 'Basic $_authorization';
|
||||
}
|
||||
super.onRequest(options, handler);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
class DynamicUrlInterceptors extends InterceptorsWrapper {
|
||||
String? _baseUrl;
|
||||
|
||||
void changeBaseUrl(String? url) {
|
||||
_baseUrl = url;
|
||||
}
|
||||
|
||||
@override
|
||||
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
|
||||
if (_baseUrl != null) {
|
||||
options.baseUrl = _baseUrl!;
|
||||
}
|
||||
super.onRequest(options, handler);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import 'package:core/data/network/config/service_path.dart';
|
||||
|
||||
class Endpoint {
|
||||
static final ServicePath authentication = ServicePath('/api/login');
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
class ServicePath {
|
||||
final String path;
|
||||
|
||||
ServicePath(this.path);
|
||||
}
|
||||
|
||||
extension ServicePathExtension on ServicePath {
|
||||
String generateEndpointPath() {
|
||||
return '$path';
|
||||
}
|
||||
|
||||
String generateAuthenticationUrl(Uri? baseUrl) {
|
||||
if (baseUrl == null) {
|
||||
return generateEndpointPath();
|
||||
} else {
|
||||
return baseUrl.origin + generateEndpointPath();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
class DioClient {
|
||||
final Dio _dio;
|
||||
|
||||
DioClient(this._dio);
|
||||
|
||||
Map<String, dynamic> getHeaders() => Map.from(_dio.options.headers);
|
||||
|
||||
Future<dynamic> get(
|
||||
String path, {
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
CancelToken? cancelToken,
|
||||
ProgressCallback? onReceiveProgress,
|
||||
}) async {
|
||||
return await _dio.get(
|
||||
path,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
cancelToken: cancelToken,
|
||||
onReceiveProgress: onReceiveProgress)
|
||||
.then((value) => value.data)
|
||||
.catchError((error) => throw error);
|
||||
}
|
||||
|
||||
Future<dynamic> post(
|
||||
String path, {
|
||||
data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
CancelToken? cancelToken,
|
||||
ProgressCallback? onSendProgress,
|
||||
ProgressCallback? onReceiveProgress,
|
||||
}) async {
|
||||
return await _dio.post(path,
|
||||
data: data,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
cancelToken: cancelToken,
|
||||
onSendProgress: onSendProgress,
|
||||
onReceiveProgress: onReceiveProgress)
|
||||
.then((value) => value.data)
|
||||
.catchError((error) => throw error);
|
||||
}
|
||||
|
||||
Future<dynamic> delete(
|
||||
String path, {
|
||||
data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
CancelToken? cancelToken,
|
||||
}) async {
|
||||
return await _dio.delete(
|
||||
path,
|
||||
data: data,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
cancelToken: cancelToken)
|
||||
.then((value) => value.data)
|
||||
.catchError((error) => throw(error));
|
||||
}
|
||||
|
||||
Future<dynamic> put(
|
||||
String path, {
|
||||
data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
CancelToken? cancelToken,
|
||||
ProgressCallback? onSendProgress,
|
||||
ProgressCallback? onReceiveProgress
|
||||
}) async {
|
||||
return await _dio.put(
|
||||
path,
|
||||
data: data,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
cancelToken: cancelToken,
|
||||
onSendProgress: onSendProgress,
|
||||
onReceiveProgress: onReceiveProgress)
|
||||
.then((value) => value.data)
|
||||
.catchError((error) => throw(error));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user