Line data Source code
1 : 2 : import 'dart:async'; 3 : 4 : import 'package:chatwoot_client_sdk/data/local/entity/chatwoot_contact.dart'; 5 : import 'package:chatwoot_client_sdk/data/local/entity/chatwoot_conversation.dart'; 6 : import 'package:chatwoot_client_sdk/data/local/entity/chatwoot_user.dart'; 7 : import 'package:chatwoot_client_sdk/data/remote/chatwoot_client_exception.dart'; 8 : import 'package:chatwoot_client_sdk/data/remote/service/chatwoot_client_api_interceptor.dart'; 9 : import 'package:dio/dio.dart'; 10 : import 'package:web_socket_channel/web_socket_channel.dart'; 11 : 12 : abstract class ChatwootClientAuthService{ 13 : 14 : WebSocketChannel? connection; 15 : final Dio dio; 16 : 17 2 : ChatwootClientAuthService(this.dio); 18 : 19 : Future<ChatwootContact> createNewContact(String inboxIdentifier, ChatwootUser? user); 20 : 21 : Future<ChatwootConversation> createNewConversation(String inboxIdentifier, String contactIdentifier); 22 : 23 : 24 : } 25 : 26 : class ChatwootClientAuthServiceImpl extends ChatwootClientAuthService{ 27 : 28 2 : ChatwootClientAuthServiceImpl( 29 : { 30 : required Dio dio 31 : } 32 2 : ) : super(dio); 33 : 34 : 35 : @override 36 1 : Future<ChatwootContact> createNewContact(String inboxIdentifier, ChatwootUser? user) async{ 37 : try{ 38 3 : final createResponse = await dio.post( 39 1 : "public/api/v1/inboxes/$inboxIdentifier/contacts", 40 1 : data: user?.toJson() 41 : ); 42 2 : if((createResponse.statusCode ?? 0).isBetween(199, 300) ){ 43 : //creating contact successful continue with request 44 2 : final contact = ChatwootContact.fromJson(createResponse.data); 45 : return contact; 46 : }else{ 47 1 : throw ChatwootClientException( 48 1 : createResponse.statusMessage ?? "unknown error", 49 : ChatwootClientExceptionType.CREATE_CONTACT_FAILED 50 : ); 51 : } 52 1 : } on DioError catch(e){ 53 2 : throw ChatwootClientException(e.message,ChatwootClientExceptionType.CREATE_CONTACT_FAILED); 54 : } 55 : } 56 : 57 : @override 58 1 : Future<ChatwootConversation> createNewConversation(String inboxIdentifier, String contactIdentifier) async{ 59 : try{ 60 3 : final createResponse = await dio.post( 61 : "public/api/v1/inboxes/$inboxIdentifier/contacts/$contactIdentifier/conversations" 62 1 : ); 63 2 : if((createResponse.statusCode ?? 0).isBetween(199, 300) ){ 64 : //creating contact successful continue with request 65 2 : final newConversation = ChatwootConversation.fromJson(createResponse.data); 66 : return newConversation; 67 : }else{ 68 1 : throw ChatwootClientException( 69 1 : createResponse.statusMessage ?? "unknown error", 70 : ChatwootClientExceptionType.CREATE_CONVERSATION_FAILED 71 : ); 72 : } 73 1 : } on DioError catch(e){ 74 1 : throw ChatwootClientException( 75 1 : e.message,ChatwootClientExceptionType.CREATE_CONVERSATION_FAILED 76 : ); 77 : } 78 : } 79 : 80 : 81 : }