Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions sdk/communication/azure-communication-chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@ it with this token. It is because the initiator of the create request must be in
This will allow you to create, get, list or delete chat threads.

```python
from azure.communication.chat import ChatClient
from azure.communication.identity._shared.user_credential import CommunicationTokenCredential
from azure.communication.identity._shared.user_token_refresh_options import CommunicationTokenRefreshOptions
from azure.communication.chat import ChatClient, CommunicationTokenCredential

# Your unique Azure Communication service endpoint
endpoint = "https://<RESOURCE_NAME>.communcationservices.azure.com"
refresh_options = CommunicationTokenRefreshOptions(token)
chat_client = ChatClient(endpoint, CommunicationTokenCredential(refresh_options))
chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
```

## Create Chat Thread Client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
CreateChatThreadResult
)

from ._shared.user_credential import CommunicationTokenCredential

__all__ = [
'ChatClient',
'ChatThreadClient',
Expand All @@ -29,6 +31,7 @@
'ChatThreadParticipant',
'ChatMessageType',
'CreateChatThreadResult',
'CommunicationError'
'CommunicationError',
'CommunicationTokenCredential'
]
__version__ = VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ class ChatMessage(object):
:ivar created_on: The timestamp when the chat message arrived at the server. The timestamp is
in RFC3339 format: ``yyyy-MM-ddTHH:mm:ssZ``.
:type created_on: ~datetime.datetime
:ivar sender_communication_identifier: The chat message sender.
:type sender_communication_identifier: CommunicationUserIdentifier
:ivar sender: The chat message sender.
:type sender: CommunicationUserIdentifier
:ivar deleted_on: The timestamp when the chat message was deleted. The timestamp is in RFC3339
format: ``yyyy-MM-ddTHH:mm:ssZ``.
:type deleted_on: ~datetime.datetime
Expand All @@ -97,7 +97,7 @@ def __init__(
self.content = kwargs['content']
self.sender_display_name = kwargs['sender_display_name']
self.created_on = kwargs['created_on']
self.sender_communication_identifier = kwargs['sender_communication_identifier']
self.sender = kwargs['sender']
self.deleted_on = kwargs['deleted_on']
self.edited_on = kwargs['edited_on']

Expand Down Expand Up @@ -125,7 +125,7 @@ def _from_generated(cls, chat_message):
content=ChatMessageContent._from_generated(chat_message.content), # pylint:disable=protected-access
sender_display_name=chat_message.sender_display_name,
created_on=chat_message.created_on,
sender_communication_identifier=sender_communication_identifier,
sender=sender_communication_identifier,
deleted_on=chat_message.deleted_on,
edited_on=chat_message.edited_on
)
Expand All @@ -141,9 +141,9 @@ class ChatMessageContent(object):
:ivar participants: Chat message content for messages of types participantAdded or
participantRemoved.
:type participants: list[~azure.communication.chat.models.ChatParticipant]
:ivar initiator_communication_identifier: Chat message content for messages of types participantAdded or
:ivar initiator: Chat message content for messages of types participantAdded or
participantRemoved.
:type initiator_communication_identifier: CommunicationUserIdentifier
:type initiator: Union[CommunicationUserIdentifier, MicrosoftTeamsUserIdentifier]
"""

def __init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@

from msrest.serialization import TZ_UTC

from .user_token_refresh_options import CommunicationTokenRefreshOptions

class CommunicationTokenCredential(object):
"""Credential type used for authenticating to an Azure Communication service.
:param communication_token_refresh_options: The token used to authenticate to an Azure Communication service
:type communication_token_refresh_options: ~azure.communication.chat.CommunicationTokenRefreshOptions
:param str token: The token used to authenticate to an Azure Communication service
:param token_refresher: The token refresher to provide capacity to fetch fresh token
:raises: TypeError
"""

ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2

def __init__(self,
communication_token_refresh_options
):
token, # type: str
token_refresher=None
):
communication_token_refresh_options = CommunicationTokenRefreshOptions(token=token,
token_refresher=token_refresher)
self._token = communication_token_refresh_options.get_token()
self._token_refresher = communication_token_refresh_options.get_token_refresher()
self._lock = Condition(Lock())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@

from msrest.serialization import TZ_UTC

from .user_token_refresh_options import CommunicationTokenRefreshOptions

class CommunicationTokenCredential(object):
"""Credential type used for authenticating to an Azure Communication service.
:param communication_token_refresh_options: The token used to authenticate to an Azure Communication service
:type communication_token_refresh_options: ~azure.communication.chat.aio.CommunicationTokenRefreshOptions
:param str token: The token used to authenticate to an Azure Communication service
:param token_refresher: The token refresher to provide capacity to fetch fresh token
:raises: TypeError
"""

ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2

def __init__(self,
communication_token_refresh_options
):
token, # type: str
token_refresher=None
):
communication_token_refresh_options = CommunicationTokenRefreshOptions(token=token,
token_refresher=token_refresher)
self._token = communication_token_refresh_options.get_token()
self._token_refresher = communication_token_refresh_options.get_token_refresher()
self._lock = Condition(Lock())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from .communication_identifier_serializer import CommunicationUserIdentifierSerializer
from ._communication_identifier_serializer import CommunicationUserIdentifierSerializer

def _to_utc_datetime(value):
return value.strftime('%Y-%m-%dT%H:%M:%SZ')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
# ------------------------------------
from ._chat_client_async import ChatClient
from ._chat_thread_client_async import ChatThreadClient

from .._shared.user_credential_async import CommunicationTokenCredential
__all__ = [
"ChatClient",
"ChatThreadClient"
"ChatThreadClient",
"CommunicationTokenCredential"
]
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,34 @@ class ChatClientSamples(object):
_thread_id = None

def create_chat_client(self):
token = self.token
endpoint = self.endpoint
# [START create_chat_client]
from azure.communication.chat import ChatClient
from azure.communication.identity._shared.user_credential 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))
from azure.communication.chat import ChatClient, CommunicationTokenCredential

chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
# [END create_chat_client]

def create_thread(self):
token = self.token
endpoint = self.endpoint
user = self.user
# [START create_thread]
from datetime import datetime

from azure.communication.identity import CommunicationUserIdentifier
from azure.communication.identity._shared.user_credential import CommunicationTokenCredential
from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions

from azure.communication.chat import(
ChatClient,
ChatThreadParticipant
ChatThreadParticipant,
CommunicationTokenCredential
)

refresh_options = CommunicationTokenRefreshOptions(self.token)
chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options))
chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))

topic = "test topic"
participants = [ChatThreadParticipant(
user=self.user,
user=user,
display_name='name',
share_history_time=datetime.utcnow()
)]
Expand All @@ -91,41 +92,42 @@ def create_thread(self):
print("thread created, id: " + self._thread_id)

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 import ChatClient
from azure.communication.identity._shared.user_credential import CommunicationTokenCredential
from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions
from azure.communication.chat import ChatClient, CommunicationTokenCredential

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)
chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
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)
print("get_chat_thread_client succeeded with thread id: ", chat_thread_client.thread_id)

def get_thread(self):
token = self.token
endpoint = self.endpoint

# [START get_thread]
from azure.communication.chat import ChatClient
from azure.communication.identity._shared.user_credential import CommunicationTokenCredential
from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions
from azure.communication.chat import ChatClient, CommunicationTokenCredential

refresh_options = CommunicationTokenRefreshOptions(self.token)
chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options))
chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
chat_thread = 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)

def list_threads(self):
token = self.token
endpoint = self.endpoint

# [START list_threads]
from azure.communication.chat import ChatClient
from azure.communication.identity._shared.user_credential import CommunicationTokenCredential
from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions
from azure.communication.chat import ChatClient, CommunicationTokenCredential
from datetime import datetime, timedelta
import pytz

refresh_options = CommunicationTokenRefreshOptions(self.token)
chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options))
chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
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)
Expand All @@ -137,14 +139,14 @@ def list_threads(self):
# [END list_threads]

def delete_thread(self):
token = self.token
endpoint = self.endpoint
thread_id = self._thread_id
# [START delete_thread]
from azure.communication.chat import ChatClient
from azure.communication.identity._shared.user_credential import CommunicationTokenCredential
from azure.communication.chat._shared.user_token_refresh_options import CommunicationTokenRefreshOptions
from azure.communication.chat import ChatClient, CommunicationTokenCredential

refresh_options = CommunicationTokenRefreshOptions(self.token)
chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options))
chat_client.delete_chat_thread(self._thread_id)
chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
chat_client.delete_chat_thread(thread_id)
# [END delete_thread]

print("delete_thread succeeded")
Expand Down
Loading