mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-24 21:37:55 +00:00
getting spaces and rooms from api {null checks}
This commit is contained in:
@@ -12,4 +12,5 @@ abstract class ApiEndpoints {
|
||||
|
||||
// Spaces
|
||||
static const String spaces = '$baseUrl/home';
|
||||
static const String rooms = '$baseUrl/room';
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:syncrow_app/features/auth/model/login_with_email_model.dart';
|
||||
import 'package:syncrow_app/features/auth/model/token.dart';
|
||||
import 'package:syncrow_app/features/auth/model/verify_code.dart';
|
||||
@@ -11,21 +10,22 @@ class AuthenticationAPI {
|
||||
path: ApiEndpoints.verifyOtp,
|
||||
body: data.toJson(),
|
||||
showServerMessage: false,
|
||||
expectedResponseModel: (json) {
|
||||
Token token = Token.fromJson(json);
|
||||
return token;
|
||||
});
|
||||
expectedResponseModel: (json) => Token.fromJson(json));
|
||||
return response;
|
||||
}
|
||||
|
||||
static Future<Token> loginWithEmail(
|
||||
{required LoginWithEmailModel model}) async {
|
||||
final response = await HTTPService().post(
|
||||
path: ApiEndpoints.login,
|
||||
body: model.toJson(),
|
||||
showServerMessage: false,
|
||||
expectedResponseModel: (json) => Token.fromJson(json['data']));
|
||||
debugPrint("response: $response");
|
||||
return response;
|
||||
try {
|
||||
final response = await HTTPService().post(
|
||||
path: ApiEndpoints.login,
|
||||
body: model.toJson(),
|
||||
showServerMessage: false,
|
||||
expectedResponseModel: (json) => Token.fromJson(json['data']));
|
||||
// debugPrint("response: $response");
|
||||
return response;
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:syncrow_app/features/auth/model/token.dart';
|
||||
|
||||
class HTTPInterceptor extends InterceptorsWrapper {
|
||||
// @override
|
||||
@@ -11,9 +12,9 @@ class HTTPInterceptor extends InterceptorsWrapper {
|
||||
@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}';
|
||||
var storage = FlutterSecureStorage();
|
||||
var token = await storage.read(key: Token.loginAccessTokenKey);
|
||||
options.headers['Authorization'] = 'Bearer $token';
|
||||
|
||||
super.onRequest(options, handler);
|
||||
}
|
||||
|
||||
@@ -17,8 +17,8 @@ class HTTPService {
|
||||
baseUrl: ApiEndpoints.baseUrl,
|
||||
receiveDataWhenStatusError: true,
|
||||
followRedirects: false,
|
||||
connectTimeout: const Duration(seconds: 5),
|
||||
receiveTimeout: const Duration(seconds: 5),
|
||||
connectTimeout: const Duration(seconds: 30),
|
||||
receiveTimeout: const Duration(seconds: 30),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -62,8 +62,8 @@ class HTTPService {
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
);
|
||||
debugPrint("status code is ${response.statusCode}");
|
||||
debugPrint("response data is ${response.data}");
|
||||
// debugPrint("status code is ${response.statusCode}");
|
||||
// debugPrint("response data is ${response.data}");
|
||||
return expectedResponseModel(response.data);
|
||||
} catch (error) {
|
||||
rethrow;
|
||||
|
||||
@@ -1,26 +1,19 @@
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
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/features/auth/model/user_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/room_model.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 {
|
||||
var uuid =
|
||||
await const FlutterSecureStorage().read(key: UserModel.userUuidKey);
|
||||
final response = await HTTPService().get(
|
||||
path: "${ApiEndpoints.spaces}/${AuthCubit.user!.uuid}",
|
||||
path: ApiEndpoints.spaces,
|
||||
queryParameters: {
|
||||
"userUuid": uuid,
|
||||
},
|
||||
showServerMessage: false,
|
||||
expectedResponseModel: (json) {
|
||||
List<SpaceModel> spaces = [];
|
||||
@@ -32,4 +25,21 @@ class SpacesAPI {
|
||||
);
|
||||
return response;
|
||||
}
|
||||
|
||||
//get rooms by space id
|
||||
static Future<List<RoomModel>> getRooms(int spaceId) async {
|
||||
final response = await HTTPService().get(
|
||||
path: ApiEndpoints.rooms,
|
||||
queryParameters: {"homeId": spaceId},
|
||||
showServerMessage: false,
|
||||
expectedResponseModel: (json) {
|
||||
List<RoomModel> rooms = [];
|
||||
for (var room in json) {
|
||||
rooms.add(RoomModel.fromJson(room));
|
||||
}
|
||||
return rooms;
|
||||
},
|
||||
);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user