mirror of
https://github.com/SyncrowIOT/web.git
synced 2026-10-24 13:27:55 +00:00
build main structure and build data layer and space managment bloc
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
|
||||
import 'enums.dart';
|
||||
|
||||
class GenericException implements Exception {
|
||||
final ExceptionType type;
|
||||
final String errorMessage;
|
||||
const GenericException(
|
||||
{required this.type, this.errorMessage = "Unknown Error"});
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return errorMessage;
|
||||
}
|
||||
|
||||
List<Object?> get props => [type, errorMessage];
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../utils/constants/api_const.dart';
|
||||
|
||||
class DioInstance {
|
||||
Dio? _dio;
|
||||
|
||||
Dio get dio => _dio ?? _instantiate();
|
||||
|
||||
String? baseUrl;
|
||||
|
||||
DioInstance({this.baseUrl});
|
||||
|
||||
Dio _instantiate() {
|
||||
Dio dio = Dio(
|
||||
BaseOptions(
|
||||
baseUrl: baseUrl ?? '${ApiEndpoints.baseUrl}/',
|
||||
receiveDataWhenStatusError: true,
|
||||
headers: {
|
||||
'content_Type': 'application/json',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
dio.interceptors.add(
|
||||
LogInterceptor(
|
||||
responseHeader: false,
|
||||
requestHeader: false,
|
||||
requestBody: true,
|
||||
responseBody: true,
|
||||
),
|
||||
);
|
||||
|
||||
return dio;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class EndPoints {
|
||||
static const String fetchCommunities = 'projects/{projectUuid}/communities';
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
enum RequestType {
|
||||
get,
|
||||
post,
|
||||
delete,
|
||||
put,
|
||||
}
|
||||
|
||||
enum ExceptionType {
|
||||
notAuthenticated,
|
||||
connectionError,
|
||||
// related to http status code exceptions
|
||||
notAuthorized,
|
||||
notFound,
|
||||
internalServerException,
|
||||
serviceUnavailableException,
|
||||
pageGone,
|
||||
|
||||
// related to bad request status code
|
||||
// related to auth requests
|
||||
invalidCredentials,
|
||||
solutionAlreadySunmitted,
|
||||
invalidValidation,
|
||||
// other
|
||||
other,
|
||||
}
|
||||
|
||||
enum ExceptionMessage {
|
||||
NOT_AUTHENTICATED,
|
||||
INVALID_CREDENTIALS,
|
||||
The_password_field_must_be_at_least_8_characters,
|
||||
SOLUTION_ALREADY_SUBMITTED,
|
||||
you_are_not_authorized,
|
||||
page_not_found,
|
||||
page_gone,
|
||||
INTERNAL_SERVER_ERROR,
|
||||
service_unavailable,
|
||||
}
|
||||
|
||||
enum NotificationsType {
|
||||
payment,
|
||||
transporation,
|
||||
product,
|
||||
zero,
|
||||
}
|
||||
|
||||
enum ImagesType {
|
||||
assets,
|
||||
svg,
|
||||
network,
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
|
||||
import '../../pages/auth/model/token.dart';
|
||||
import 'custom_exceptions.dart';
|
||||
import 'dio.dart';
|
||||
import 'enums.dart';
|
||||
|
||||
class Request {
|
||||
String endPoint;
|
||||
bool? autherized;
|
||||
bool? isFormData;
|
||||
RequestType? method;
|
||||
Map<String, dynamic>? headers;
|
||||
final Map<String, dynamic>? queryParams;
|
||||
Map<String, dynamic>? body;
|
||||
Duration? receiveTimeout;
|
||||
Request(
|
||||
this.endPoint, {
|
||||
this.autherized,
|
||||
this.isFormData,
|
||||
this.method,
|
||||
this.headers,
|
||||
this.queryParams,
|
||||
this.body,
|
||||
this.receiveTimeout,
|
||||
}) {
|
||||
headers = {
|
||||
'content_Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
};
|
||||
}
|
||||
Future<Map<String, dynamic>> sendRequest() async {
|
||||
Response? response;
|
||||
if (autherized != null && autherized!) {
|
||||
final storage = const FlutterSecureStorage();
|
||||
final token = await storage.read(key: Token.loginAccessTokenKey);
|
||||
if (token != null) {
|
||||
headers!["authorization"] = "Bearer $token";
|
||||
}
|
||||
}
|
||||
try {
|
||||
response = await DioInstance().dio.request(
|
||||
endPoint,
|
||||
queryParameters: queryParams,
|
||||
data: isFormData != null && isFormData == true
|
||||
? FormData.fromMap(body!)
|
||||
: body,
|
||||
options: Options(
|
||||
method: method!.name.toUpperCase(),
|
||||
headers: headers,
|
||||
contentType: 'application/json',
|
||||
receiveTimeout: receiveTimeout,
|
||||
),
|
||||
);
|
||||
if (response.statusCode! >= 200 && response.statusCode! < 300) {
|
||||
return response.data;
|
||||
}
|
||||
} on DioException catch (error) {
|
||||
if (error.type == DioExceptionType.badResponse) {
|
||||
throw badRequestException[error.response!.data["error"]] ??
|
||||
const GenericException(
|
||||
type: ExceptionType.other,
|
||||
);
|
||||
}
|
||||
if (error.type == DioExceptionType.connectionError ||
|
||||
error.type == DioExceptionType.connectionTimeout ||
|
||||
error.type == DioExceptionType.receiveTimeout ||
|
||||
error.type == DioExceptionType.sendTimeout ||
|
||||
error.type == DioExceptionType.unknown) {
|
||||
throw const GenericException(
|
||||
type: ExceptionType.connectionError,
|
||||
errorMessage: 'no_internet_connection',
|
||||
);
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'endPoint': endPoint,
|
||||
'method': method!.name.toUpperCase(),
|
||||
'body': json.encode(body),
|
||||
'headers': headers,
|
||||
'queryParams': queryParams,
|
||||
'autherized': autherized,
|
||||
'isFormData': isFormData,
|
||||
};
|
||||
@override
|
||||
String toString() {
|
||||
return jsonEncode(toJson());
|
||||
}
|
||||
|
||||
List<Object?> get props => [
|
||||
endPoint,
|
||||
autherized,
|
||||
isFormData,
|
||||
method,
|
||||
headers,
|
||||
queryParams,
|
||||
body,
|
||||
receiveTimeout,
|
||||
];
|
||||
}
|
||||
|
||||
Map<String, GenericException> badRequestException = {
|
||||
ExceptionMessage.INVALID_CREDENTIALS.name: const GenericException(
|
||||
type: ExceptionType.invalidCredentials,
|
||||
errorMessage: "Invalid credentials ...",
|
||||
),
|
||||
"The password field must be at least 8 characters.": const GenericException(
|
||||
type: ExceptionType.invalidValidation,
|
||||
errorMessage: 'password must be 8 or more characters',
|
||||
),
|
||||
ExceptionMessage.NOT_AUTHENTICATED.name: const GenericException(
|
||||
type: ExceptionType.notAuthenticated,
|
||||
errorMessage: "not authenticated",
|
||||
),
|
||||
};
|
||||
Reference in New Issue
Block a user