diff --git a/lib/unit/api/HttpService.dart b/lib/unit/api/HttpService.dart deleted file mode 100644 index dd35207..0000000 --- a/lib/unit/api/HttpService.dart +++ /dev/null @@ -1,72 +0,0 @@ -import 'dart:convert'; -import 'package:http/http.dart' as http; - -class HttpService { - String? sessionId; - - Future login(String username, String password) async { - String url = 'http://127.0.0.1:8081/login'; - try { - final response = await http.post( - Uri.parse(url), - headers: { - 'Content-Type': 'application/json', - }, - body: jsonEncode({ - 'username': username, - 'password': password, - }), - ); - - if (response.statusCode == 200) { - final jsonData = jsonDecode(response.body); - if (jsonData['login_status'] == 'pending') { - sessionId = jsonData['id']; - print("OTP sent, session id: $sessionId"); - } else { - print("Unexpected login status: ${jsonData['login_status']}"); - } - } else { - print("Login failed with status code: ${response.statusCode}"); - } - } catch (e) { - print("Error during login request: $e"); - } - } - - Future verifyOtp(int otp) async { - String url = 'http://127.0.0.1:8081/verify_otp'; - if (sessionId == null) { - print("ID not found"); - return false; - } - - try { - final response = await http.post( - Uri.parse(url), - headers: { - 'Content-Type': 'application/json', - }, - body: jsonEncode({ - 'session_id': sessionId!, - 'otp': otp, - }), - ); - - if (response.statusCode == 200) { - final jsonData = jsonDecode(response.body); - if (jsonData['login_status'] == 'success') { - print("login success"); - return true; - } else { - print("Unexpected response: ${jsonData['login_status']}"); - } - } else { - print("OTP verification failed with status code: ${response.statusCode}"); - } - } catch (e) { - print("Error during OTP verification: $e"); - } - return false; - } -} diff --git a/lib/unit/api/WebSocketService.dart b/lib/unit/api/WebSocketService.dart deleted file mode 100644 index f665754..0000000 --- a/lib/unit/api/WebSocketService.dart +++ /dev/null @@ -1,50 +0,0 @@ -import 'package:web_socket_channel/web_socket_channel.dart'; - -class WebSocketService { - WebSocketChannel? channel; - bool _isConnected = false; - - bool get isConnected => _isConnected; - - Future wsConnect(String uri, Function(String) onMessageReceived) async { - try { - channel = WebSocketChannel.connect(Uri.parse(uri)); - channel!.stream.listen( - (message) { - print("Received data: $message"); - onMessageReceived(message); - }, - onError: (error) { - print("WebSocket connection error: $error"); - _isConnected = false; - channel?.sink.close(); - }, - onDone: () { - print("WebSocket connection closed."); - _isConnected = false; - }, - ); - - _isConnected = true; - return true; - } catch (e) { - print("Failed to connect: $e"); - _isConnected = false; - return false; - } - } - - void sendMessage(String message) { - if (_isConnected && channel != null) { - channel!.sink.add(message); - print("Sent message: $message"); - } else { - print('Cannot send message: WebSocket is not connected.'); - } - } - - void closeConnection() { - channel?.sink.close(); - _isConnected = false; - } -} diff --git a/lib/unit/com.dart b/lib/unit/com.dart deleted file mode 100644 index 22add60..0000000 --- a/lib/unit/com.dart +++ /dev/null @@ -1,31 +0,0 @@ -import 'api/HttpService.dart'; -import 'api/WebSocketService.dart'; - -class Com { - final HttpService httpService; - final WebSocketService webSocketService; - - Com({required this.httpService, required this.webSocketService}); - - Future login(String username, String password) async { - await httpService.login(username, password); - } - - Future verifyOtp(int otp) async { - return await httpService.verifyOtp(otp); - } - - Future wsConnect(Function(String) onMessageReceived) async { - return await webSocketService.wsConnect("ws://127.0.0.1:8081/ws", onMessageReceived); - } - - void sendMessageOverSocket(String message) { - webSocketService.sendMessage(message); - } - - void closeWebSocketConnection() { - webSocketService.closeConnection(); - } -} - -