def setUp(self): super(ChatClientTestAsync, self).setUp() self.recording_processors.extend([ BodyReplacerProcessor(keys=[ "id", "token", "createdBy", "participants", "multipleStatus", "value" ]), URIIdentityReplacer(), ResponseReplacerProcessor(keys=[self._resource_name]), ChatURIReplacer() ]) endpoint, _ = parse_connection_str(self.connection_str) self.endpoint = endpoint self.identity_client = CommunicationIdentityClient.from_connection_string( self.connection_str) # create user self.user = self.identity_client.create_user() token_response = self.identity_client.get_token(self.user, scopes=["chat"]) self.token = token_response.token # create ChatClient self.chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.token))
def setUp(self): super(ChatThreadClientTestAsync, self).setUp() self.recording_processors.extend([ BodyReplacerProcessor(keys=["id", "token", "senderId", "chatMessageId", "nextLink", "participants", "multipleStatus", "value"]), URIIdentityReplacer(), ResponseReplacerProcessor(keys=[self._resource_name]), ChatURIReplacer()]) endpoint, _ = parse_connection_str(self.connection_str) self.endpoint = endpoint self.identity_client = CommunicationIdentityClient.from_connection_string(self.connection_str) self.users = [] self.user_tokens = [] self.chat_clients = [] # create user 1 self.user = self.identity_client.create_user() token_response = self.identity_client.get_token(self.user, scopes=["chat"]) self.token = token_response.token # create user 2 self.new_user = self.identity_client.create_user() token_response = self.identity_client.get_token(self.new_user, scopes=["chat"]) self.token_new_user = token_response.token # create ChatClient refresh_option = CommunicationTokenRefreshOptions(self.token) refresh_option_new_user = CommunicationTokenRefreshOptions(self.token_new_user) self.chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_option)) self.chat_client_new_user = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_option_new_user))
async def create_chat_thread_client_async(self): # [START create_chat_thread_client] from datetime import datetime from azure.communication.chat.aio import ChatClient from azure.communication.chat import ChatThreadParticipant from azure.communication.identity import CommunicationUserIdentifier from azure.communication.identity._shared.user_credential_async import CommunicationTokenCredential from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) async with chat_client: topic = "test topic" participants = [ChatThreadParticipant( user=self.user, display_name='name', share_history_time=datetime.utcnow() )] create_chat_thread_result = await chat_client.create_chat_thread(topic, thread_participants=participants) chat_thread_client = chat_client.get_chat_thread_client(create_chat_thread_result.chat_thread.id) # [END create_chat_thread_client] self._thread_id = create_chat_thread_result.chat_thread.id print("thread created, id: " + self._thread_id) print("create_chat_thread_client_async succeeded")
async def test_list_chat_threads(): thread_id = "19:[email protected]" raised = False async def mock_send(*_, **__): return mock_response(status_code=200, json_payload={"value": [{ "id": thread_id }]}) chat_client = ChatClient("https://endpoint", credential, transport=Mock(send=mock_send)) chat_thread_infos = None try: chat_thread_infos = chat_client.list_chat_threads() except: raised = True assert raised == False items = [] async for item in chat_thread_infos: items.append(item) assert len(items) == 1 assert items[0].id == thread_id
async def create_chat_thread_client_async(self): token = self.token endpoint = self.endpoint user = self.user # [START create_chat_thread_client] from datetime import datetime from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential from azure.communication.chat import ChatParticipant, CommunicationUserIdentifier # set `endpoint` to an existing ACS endpoint chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) async with chat_client: topic = "test topic" participants = [ ChatParticipant(identifier=user, display_name='name', share_history_time=datetime.utcnow()) ] create_chat_thread_result = await chat_client.create_chat_thread( topic, thread_participants=participants) chat_thread_client = chat_client.get_chat_thread_client( create_chat_thread_result.chat_thread.id) # [END create_chat_thread_client] self._thread_id = create_chat_thread_result.chat_thread.id print("thread created, id: " + self._thread_id) print("create_chat_thread_client_async succeeded")
def get_chat_thread_client(self): # [START get_chat_thread_client] from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential, CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) chat_thread_client = chat_client.get_chat_thread_client(self._thread_id) # [END get_chat_thread_client] print("chat_thread_client created with thread id: ", chat_thread_client.thread_id)
def get_chat_thread_client(self): # [START get_chat_thread_client] from azure.communication.chat.aio import ChatClient, CommunicationUserCredential chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token)) chat_thread_client = chat_client.get_chat_thread_client( self._thread_id) # [END get_chat_thread_client] print("chat_thread_client created with thread id: ", chat_thread_client.thread_id)
async def list_threads_async(self): from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential, CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) async with chat_client: # [START list_threads] from datetime import datetime, timedelta import pytz start_time = datetime.utcnow() - timedelta(days=2) start_time = start_time.replace(tzinfo=pytz.utc) chat_thread_infos = chat_client.list_chat_threads(results_per_page=5, start_time=start_time) print("list_threads succeeded with results_per_page is 5, and were created since 2 days ago.") async for info in chat_thread_infos: print("thread id: ", info.id)
async def test_create_chat_thread(): thread_id = "19:[email protected]" async def mock_send(*_, **__): return mock_response( status_code=201, json_payload={ "chatThread": { "id": thread_id, "topic": "test topic", "createdOn": "2020-12-03T21:09:17Z", "createdBy": "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041" } }) chat_client = ChatClient("https://endpoint", credential, transport=Mock(send=mock_send)) topic = "test topic" user = CommunicationUserIdentifier( "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041") participants = [ ChatParticipant(identifier=user, display_name='name', share_history_time=datetime.utcnow()) ] create_chat_thread_result = await chat_client.create_chat_thread( topic, thread_participants=participants) assert create_chat_thread_result.chat_thread.id == thread_id
async def test_get_chat_thread(): thread_id = "19:[email protected]" raised = False async def mock_send(*_, **__): return mock_response(status_code=200, json_payload={ "id": thread_id, "topic": "Lunch Chat thread", "createdOn": "2020-10-30T10:50:50Z", "deletedOn": "2020-10-30T10:50:50Z", "createdByCommunicationIdentifier": { "rawId": "string", "communicationUser": { "id": "string" } } }) chat_client = ChatClient("https://endpoint", credential, transport=Mock(send=mock_send)) get_thread_result = None try: get_thread_result = await chat_client.get_chat_thread(thread_id) except: raised = True assert raised == False assert get_thread_result.id == thread_id
async def create_thread_async(self): from datetime import datetime from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential, CommunicationTokenRefreshOptions from azure.communication.chat import ChatThreadParticipant, CommunicationUserIdentifier refresh_options = CommunicationTokenRefreshOptions(self.token) chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) async with chat_client: # [START create_thread] topic = "test topic" participants = [ChatThreadParticipant( user=self.user, display_name='name', share_history_time=datetime.utcnow() )] # creates a new chat_thread everytime chat_thread_client = await chat_client.create_chat_thread(topic, participants) # creates a new chat_thread if not exists repeatability_request_id = 'b66d6031-fdcc-41df-8306-e524c9f226b8' # unique identifier chat_thread_client_w_repeatability_id = await chat_client.create_chat_thread(topic, participants, repeatability_request_id) # [END create_thread] self._thread_id = chat_thread_client.thread_id print("thread created, id: " + self._thread_id)
def create_chat_client(self): # [START create_chat_client] from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.token)) # [END create_chat_client] print("chat_client created")
async def test_create_chat_thread_raises_error(): async def mock_send(*_, **__): return mock_response(status_code=400, json_payload={"msg": "some error"}) chat_client = ChatClient("https://endpoint", credential, transport=Mock(send=mock_send)) topic = "test topic", user = CommunicationUser( "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041") members = [ ChatThreadMember(user=user, display_name='name', share_history_time=datetime.utcnow()) ] raised = False try: await chat_client.create_chat_thread(topic=topic, thread_members=members) except: raised = True assert raised == True
async def test_create_chat_thread(): thread_id = "19:[email protected]" async def mock_send(*_, **__): return mock_response(status_code=207, json_payload={ "multipleStatus": [{ "id": thread_id, "statusCode": 201, "type": "Thread" }] }) chat_client = ChatClient("https://endpoint", credential, transport=Mock(send=mock_send)) topic = "test topic" user = CommunicationUser( "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041") members = [ ChatThreadMember(user=user, display_name='name', share_history_time=datetime.utcnow()) ] chat_thread_client = await chat_client.create_chat_thread(topic, members) assert chat_thread_client.thread_id == thread_id
async def create_thread_async(self): token = self.token endpoint = self.endpoint thread_id = self._thread_id # [START create_thread] from datetime import datetime from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential from azure.communication.chat import ChatParticipant # set `endpoint` to an existing ACS endpoint chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) async with chat_client: topic = "test topic" participants = [ChatParticipant( identifier=self.user, display_name='name', share_history_time=datetime.utcnow() )] # creates a new chat_thread everytime create_chat_thread_result = await chat_client.create_chat_thread(topic, thread_participants=participants) # creates a new chat_thread if not exists idempotency_token = 'b66d6031-fdcc-41df-8306-e524c9f226b8' # unique identifier create_chat_thread_result_w_repeatability_id = await chat_client.create_chat_thread( topic, thread_participants=participants, idempotency_token=idempotency_token) # [END create_thread] self._thread_id = create_chat_thread_result.chat_thread.id print("thread created, id: " + self._thread_id)
async def test_get_chat_thread(): thread_id = "19:[email protected]" raised = False async def mock_send(*_, **__): return mock_response(status_code=200, json_payload={ "id": thread_id, "created_by": "8:acs:resource_user", "members": [{ "id": "", "display_name": "name", "share_history_time": "1970-01-01T00:00:00Z" }] }) chat_client = ChatClient("https://endpoint", credential, transport=Mock(send=mock_send)) get_thread_result = None try: get_thread_result = await chat_client.get_chat_thread(thread_id) except: raised = True assert raised == False assert get_thread_result.id == thread_id
def setUp(self): super(ChatThreadClientTestAsync, self).setUp() self.recording_processors.extend([ BodyReplacerProcessor(keys=[ "id", "token", "senderId", "chatMessageId", "nextLink", "members", "multipleStatus", "value" ]), URIIdentityReplacer(), ResponseReplacerProcessor(keys=[self._resource_name]), ChatURIReplacer() ]) endpoint, _ = parse_connection_str(self.connection_str) self.endpoint = endpoint self.identity_client = CommunicationIdentityClient.from_connection_string( self.connection_str) # create user self.user = self.identity_client.create_user() token_response = self.identity_client.issue_token(self.user, scopes=["chat"]) self.token = token_response.token # create another user self.new_user = self.identity_client.create_user() # create ChatClient self.chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token))
def get_chat_thread_client(self): token = self.token endpoint = self.endpoint thread_id = self._thread_id # [START get_chat_thread_client] from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential # set `endpoint` to an existing ACS endpoint chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) # set `thread_id` to an existing chat thread id chat_thread_client = chat_client.get_chat_thread_client(thread_id) # [END get_chat_thread_client] print("chat_thread_client created with thread id: ", chat_thread_client.thread_id)
def create_chat_client(self): # [START create_chat_client] from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential, CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) # [END create_chat_client] print("chat_client created")
async def get_chat_thread_properties_async(self): thread_id = self._thread_id token = self.token endpoint = self.endpoint # [START get_thread] from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential # set `endpoint` to an existing ACS endpoint chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) async with chat_client: chat_thread_client = chat_client.get_chat_thread_client(thread_id) async with chat_thread_client: chat_thread_properties = chat_thread_client.get_properties() print('Expected Thread Id: ', thread_id, ' Actual Value: ', chat_thread_properties.id) # [END get_thread] print("get_chat_thread_properties_async succeeded, thread id: " + chat_thread.id + ", thread topic: " + chat_thread.topic)
async def delete_thread_async(self): from azure.communication.chat.aio import ChatClient, CommunicationUserCredential chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token)) async with chat_client: # [START delete_thread] await chat_client.delete_chat_thread(self._thread_id) # [END delete_thread] print("delete_thread succeeded")
async def list_threads_async(self): token = self.token endpoint = self.endpoint thread_id = self._thread_id # [START list_threads] from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential # set `endpoint` to an existing ACS endpoint chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) async with chat_client: from datetime import datetime, timedelta start_time = datetime.utcnow() - timedelta(days=2) chat_threads = chat_client.list_chat_threads(results_per_page=5, start_time=start_time) print("list_threads succeeded with results_per_page is 5, and were created since 2 days ago.") async for chat_thread_item_page in chat_threads.by_page(): async for chat_thread_item in chat_thread_item_page: print("thread id: ", chat_thread_item.id)
async def get_thread_async(self): from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential, CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) async with chat_client: # [START get_thread] chat_thread = await chat_client.get_chat_thread(self._thread_id) # [END get_thread] print("get_thread succeeded, thread id: " + chat_thread.id + ", thread topic: " + chat_thread.topic)
async def delete_thread_async(self): from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential, CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) async with chat_client: # [START delete_thread] await chat_client.delete_chat_thread(self._thread_id) # [END delete_thread] print("delete_thread succeeded")
def create_chat_client(self): # [START create_chat_client] from azure.communication.chat.aio import ChatClient from azure.communication.identity._shared.user_credential_async import CommunicationTokenCredential from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) # [END create_chat_client] print("chat_client created")
async def get_chat_thread_properties_async(self): thread_id = self._thread_id # [START get_thread] from azure.communication.chat.aio import ChatClient from azure.communication.identity._shared.user_credential_async import CommunicationTokenCredential from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) async with chat_client: chat_thread_client = chat_client.get_chat_thread_client(thread_id) async with chat_thread_client: chat_thread_properties = chat_thread_client.get_properties() print('Expected Thread Id: ', thread_id, ' Actual Value: ', chat_thread_properties.id) # [END get_thread] print("get_chat_thread_properties_async succeeded, thread id: " + chat_thread.id + ", thread topic: " + chat_thread.topic)
def create_chat_client(self): token = self.token endpoint = self.endpoint thread_id = self._thread_id # [START create_chat_client] from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential # set `endpoint` to an existing ACS endpoint chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) # [END create_chat_client] print("chat_client created")
async def delete_thread_async(self): token = self.token endpoint = self.endpoint thread_id = self._thread_id # [START delete_thread] from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential # set `endpoint` to an existing ACS endpoint chat_client = ChatClient(endpoint, CommunicationTokenCredential(token)) async with chat_client: # set `thread_id` to an existing chat thread id await chat_client.delete_chat_thread(thread_id) # [END delete_thread] print("delete_thread succeeded")
async def test_delete_chat_thread(): thread_id = "19:[email protected]" raised = False async def mock_send(*_, **__): return mock_response(status_code=204) chat_client = ChatClient("https://endpoint", credential, transport=Mock(send=mock_send)) raised = False try: await chat_client.delete_chat_thread(thread_id) except: raised = True assert raised == False
async def create_chat_thread_client_async(self): # [START create_chat_thread_client] from datetime import datetime from azure.communication.chat.aio import ChatClient, CommunicationUserCredential from azure.communication.chat import ChatThreadMember, CommunicationUserIdentifier chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token)) async with chat_client: topic = "test topic" members = [ChatThreadMember( user=self.user, display_name='name', share_history_time=datetime.utcnow() )] chat_thread_client = await chat_client.create_chat_thread(topic, members) # [END create_chat_thread_client] self._thread_id = chat_thread_client.thread_id print("thread created, id: " + self._thread_id)