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