Semi-implemented getting spaces feature

This commit is contained in:
Mohammad Salameh
2024-03-12 11:14:31 +03:00
parent 661d535960
commit 0f3cc453ce
25 changed files with 311 additions and 294 deletions

View File

@ -1,6 +1,7 @@
abstract class ApiEndpoints {
static const String baseUrl = 'http://100.107.182.63:4001';
static const String baseUrl = 'https://syncrow.azurewebsites.net';
// Authentication
static const String signUp = '$baseUrl/authentication/user/signup';
static const String login = '$baseUrl/authentication/user/login';
static const String deleteUser = '$baseUrl/authentication/user/delete/{id}';
@ -8,4 +9,7 @@ abstract class ApiEndpoints {
static const String verifyOtp = '$baseUrl/authentication/user/verify-otp';
static const String forgetPassword =
'$baseUrl/authentication/user/forget-password';
// Spaces
static const String spaces = '$baseUrl/home';
}

View File

@ -24,10 +24,7 @@ class AuthenticationAPI {
path: ApiEndpoints.login,
body: model.toJson(),
showServerMessage: false,
expectedResponseModel: (json) {
Token token = Token.fromJson(json['data']);
return token;
});
expectedResponseModel: (json) => Token.fromJson(json['data']));
debugPrint("response: $response");
return response;
}

View File

@ -1,4 +1,5 @@
import 'package:dio/dio.dart';
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
class HTTPInterceptor extends InterceptorsWrapper {
// @override
@ -7,36 +8,38 @@ class HTTPInterceptor extends InterceptorsWrapper {
// return handler.next(response);
// }
//
// @override
// void onRequest(RequestOptions options,
// RequestInterceptorHandler handler) async {
// // TODO: Implement logic for adding headers to requests.
// // This method is called before a request is sent.
// super.onRequest(options, handler);
// }
//
// @override
// void onError(DioException err, ErrorInterceptorHandler handler) async {
// // TODO: Implement error handling logic.
// // This method is called when an error occurs during a request.
// super.onError(err, handler);
// }
//
// /// Validates the response and returns true if it is successful (status code 2xx).
// Future<bool> validateResponse(Response response) async {
// if (response.statusCode != null) {
// if (response.statusCode! >= 200 && response.statusCode! < 300) {
// // If the response status code is within the successful range (2xx),
// // return true indicating a successful response.
// return true;
// } else {
// // If the response status code is not within the successful range (2xx),
// // return false indicating an unsuccessful response.
// return false;
// }
// } else {
// // If the response status code is null, return false indicating an unsuccessful response.
// return false;
// }
// }
@override
void onRequest(
RequestOptions options, RequestInterceptorHandler handler) async {
//pass the token from the flutter secure storage to the request header
options.headers['Authorization'] = 'Bearer ${AuthCubit.token.accessToken}';
super.onRequest(options, handler);
}
//
// @override
// void onError(DioException err, ErrorInterceptorHandler handler) async {
// // TODO: Implement error handling logic.
// // This method is called when an error occurs during a request.
// super.onError(err, handler);
// }
//
// /// Validates the response and returns true if it is successful (status code 2xx).
// Future<bool> validateResponse(Response response) async {
// if (response.statusCode != null) {
// if (response.statusCode! >= 200 && response.statusCode! < 300) {
// // If the response status code is within the successful range (2xx),
// // return true indicating a successful response.
// return true;
// } else {
// // If the response status code is not within the successful range (2xx),
// // return false indicating an unsuccessful response.
// return false;
// }
// } else {
// // If the response status code is null, return false indicating an unsuccessful response.
// return false;
// }
// }
}

View File

@ -17,8 +17,8 @@ class HTTPService {
baseUrl: ApiEndpoints.baseUrl,
receiveDataWhenStatusError: true,
followRedirects: false,
connectTimeout: const Duration(seconds: 60),
receiveTimeout: const Duration(seconds: 60),
connectTimeout: const Duration(seconds: 5),
receiveTimeout: const Duration(seconds: 5),
),
);
@ -37,6 +37,9 @@ class HTTPService {
path,
queryParameters: queryParameters,
);
debugPrint("status code is ${response.statusCode}");
debugPrint("response data is ${response.data}");
return expectedResponseModel(response.data);
} catch (error) {
debugPrint("******* Error");

View File

@ -38,6 +38,9 @@ class ServerFailure extends Failure {
case DioExceptionType.unknown:
return ServerFailure("Unexpected Error, Please try again!");
default:
return ServerFailure("Unexpected Error, Please try again!");
}
}

View File

@ -0,0 +1,35 @@
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
import 'package:syncrow_app/services/api/http_service.dart';
class SpacesAPI {
// static Future<Token> loginWithEmail(
// {required LoginWithEmailModel model}) async {
// final response = await HTTPService().post(
// path: ApiEndpoints.login,
// body: model.toJson(),
// showServerMessage: false,
// expectedResponseModel: (json) {
// Token token = Token.fromJson(json['data']);
// return token;
// });
// debugPrint("response: $response");
// return response;
// }
static Future<List<SpaceModel>> getSpaces() async {
final response = await HTTPService().get(
path: "${ApiEndpoints.spaces}/${AuthCubit.user!.uuid}",
showServerMessage: false,
expectedResponseModel: (json) {
List<SpaceModel> spaces = [];
for (var space in json) {
spaces.add(SpaceModel.fromJson(space));
}
return spaces;
},
);
return response;
}
}