Skip to content

Commit

Permalink
[Communication]Rename CommunicationUserCredential to CommunicationTok…
Browse files Browse the repository at this point in the history
…enCredential (#16177)

* Rename CommunicationUserCredential to CommunicationTokenCredential

* Fix casing in method name
  • Loading branch information
sacheun authored Jan 15, 2021
1 parent 82f13b8 commit f7e0d77
Show file tree
Hide file tree
Showing 25 changed files with 127 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from msrest.serialization import TZ_UTC
from .utils import create_access_token

class CommunicationUserCredential(object):
class CommunicationTokenCredential(object):
"""Credential type used for authenticating to an Azure Communication service.
:param str token: The token used to authenticate to an Azure Communication service
:raises: TypeError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from msrest.serialization import TZ_UTC
from .utils import create_access_token

class CommunicationUserCredential(object):
class CommunicationTokenCredential(object):
"""Credential type used for authenticating to an Azure Communication service.
:param str token: The token used to authenticate to an Azure Communication service
:raises: TypeError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,47 @@
# --------------------------------------------------------------------------
from unittest import TestCase
from unittest.mock import MagicMock
from azure.communication.administration._shared.user_credential import CommunicationUserCredential
from azure.communication.administration._shared.user_credential import CommunicationTokenCredential
from azure.communication.administration._shared.utils import create_access_token


class TestCommunicationUserCredential(TestCase):
class TestCommunicationTokenCredential(TestCase):
sample_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."+\
"eyJleHAiOjMyNTAzNjgwMDAwfQ.9i7FNNHHJT8cOzo-yrAUJyBSfJ-tPPk2emcHavOEpWc"
sample_token_expiry = 32503680000
expired_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."+\
"eyJleHAiOjEwMH0.1h_scYkNp-G98-O4cW6KvfJZwiz54uJMyeDACE4nypg"


def test_communicationusercredential_decodes_token(self):
credential = CommunicationUserCredential(self.sample_token)
def test_communicationtokencredential_decodes_token(self):
credential = CommunicationTokenCredential(self.sample_token)
access_token = credential.get_token()

self.assertEqual(access_token.token, self.sample_token)

def test_communicationusercredential_throws_if_invalid_token(self):
self.assertRaises(ValueError, lambda: CommunicationUserCredential("foo.bar.tar"))
def test_communicationtokencredential_throws_if_invalid_token(self):
self.assertRaises(ValueError, lambda: CommunicationTokenCredential("foo.bar.tar"))

def test_communicationusercredential_throws_if_nonstring_token(self):
self.assertRaises(TypeError, lambda: CommunicationUserCredential(454))
def test_communicationtokencredential_throws_if_nonstring_token(self):
self.assertRaises(TypeError, lambda: CommunicationTokenCredential(454))

def test_communicationusercredential_static_token_returns_expired_token(self):
credential = CommunicationUserCredential(self.expired_token)
def test_communicationtokencredential_static_token_returns_expired_token(self):
credential = CommunicationTokenCredential(self.expired_token)

self.assertEqual(credential.get_token().token, self.expired_token)

def test_communicationusercredential_token_expired_refresh_called(self):
def test_communicationtokencredential_token_expired_refresh_called(self):
refresher = MagicMock(return_value=self.sample_token)
access_token = CommunicationUserCredential(
access_token = CommunicationTokenCredential(
self.expired_token,
token_refresher=refresher).get_token()
refresher.assert_called_once()
self.assertEqual(access_token, self.sample_token)


def test_communicationusercredential_token_expired_refresh_called_asnecessary(self):
def test_communicationtokencredential_token_expired_refresh_called_asnecessary(self):
refresher = MagicMock(return_value=create_access_token(self.expired_token))
credential = CommunicationUserCredential(
credential = CommunicationTokenCredential(
self.expired_token,
token_refresher=refresher)

Expand Down
4 changes: 2 additions & 2 deletions sdk/communication/azure-communication-chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +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, CommunicationUserCredential
from azure.communication.chat import ChatClient, CommunicationTokenCredential
# Your unique Azure Communication service endpoint
endpoint = "https://<RESOURCE_NAME>.communcationservices.azure.com"
token = "<token>"
chat_client = ChatClient(endpoint, CommunicationUserCredential(token))
chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
```

## Create Chat Thread Client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
SendChatMessageResult,
ChatThreadInfo,
)
from ._shared.user_credential import CommunicationUserCredential
from ._shared.user_credential import CommunicationTokenCredential
from ._models import (
ChatThreadMember,
ChatMessage,
Expand All @@ -24,7 +24,7 @@
'SendChatMessageResult',
'ChatThread',
'ChatThreadInfo',
'CommunicationUserCredential',
'CommunicationTokenCredential',
'ChatThreadMember',
'CommunicationUserIdentifier',
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from azure.core.pipeline.policies import BearerTokenCredentialPolicy

from ._chat_thread_client import ChatThreadClient
from ._shared.user_credential import CommunicationUserCredential
from ._shared.user_credential import CommunicationTokenCredential
from ._generated import AzureCommunicationChatService
from ._generated.models import CreateChatThreadRequest
from ._models import ChatThread
Expand All @@ -37,7 +37,7 @@ class ChatClient(object):
:param str endpoint:
The endpoint of the Azure Communication resource.
:param CommunicationUserCredential credential:
:param CommunicationTokenCredential credential:
The credentials with which to authenticate.
.. admonition:: Example:
Expand All @@ -53,7 +53,7 @@ class ChatClient(object):
def __init__(
self,
endpoint, # type: str
credential, # type: CommunicationUserCredential
credential, # type: CommunicationTokenCredential
**kwargs # type: Any
):
# type: (...) -> None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from azure.core.tracing.decorator import distributed_trace
from azure.core.pipeline.policies import BearerTokenCredentialPolicy

from ._shared.user_credential import CommunicationUserCredential
from ._shared.user_credential import CommunicationTokenCredential
from ._generated import AzureCommunicationChatService
from ._generated.models import (
AddChatThreadMembersRequest,
Expand Down Expand Up @@ -51,7 +51,7 @@ class ChatThreadClient(object):
:param str endpoint:
The endpoint of the Azure Communication resource.
:param CommunicationUserCredential credential:
:param CommunicationTokenCredential credential:
The credentials with which to authenticate. The value contains a User
Access Token
:param str thread_id:
Expand All @@ -70,7 +70,7 @@ class ChatThreadClient(object):
def __init__(
self,
endpoint, # type: str
credential, # type: CommunicationUserCredential
credential, # type: CommunicationTokenCredential
thread_id, # type: str
**kwargs # type: Any
):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from msrest.serialization import TZ_UTC
from .utils import create_access_token

class CommunicationUserCredential(object):
class CommunicationTokenCredential(object):
"""Credential type used for authenticating to an Azure Communication service.
:param str token: The token used to authenticate to an Azure Communication service
:raises: TypeError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from msrest.serialization import TZ_UTC
from .utils import create_access_token

class CommunicationUserCredential(object):
class CommunicationTokenCredential(object):
"""Credential type used for authenticating to an Azure Communication service.
:param str token: The token used to authenticate to an Azure Communication service
:raises: TypeError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
# ------------------------------------
from ._chat_client_async import ChatClient
from ._chat_thread_client_async import ChatThreadClient
from .._shared.user_credential_async import CommunicationUserCredential
from .._shared.user_credential_async import CommunicationTokenCredential

__all__ = [
"ChatClient",
"ChatThreadClient",
"CommunicationUserCredential"
"CommunicationTokenCredential"
]
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from azure.core.async_paging import AsyncItemPaged

from ._chat_thread_client_async import ChatThreadClient
from .._shared.user_credential_async import CommunicationUserCredential
from .._shared.user_credential_async import CommunicationTokenCredential
from .._generated.aio import AzureCommunicationChatService
from .._generated.models import (
CreateChatThreadRequest,
Expand All @@ -42,7 +42,7 @@ class ChatClient(object):
:param str endpoint:
The endpoint of the Azure Communication resource.
:param CommunicationUserCredential credential:
:param CommunicationTokenCredential credential:
The credentials with which to authenticate.
.. admonition:: Example:
Expand All @@ -57,7 +57,7 @@ class ChatClient(object):

def __init__(
self, endpoint: str,
credential: CommunicationUserCredential,
credential: CommunicationTokenCredential,
**kwargs
) -> None:
if not credential:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from azure.core.pipeline.policies import BearerTokenCredentialPolicy
from azure.core.async_paging import AsyncItemPaged

from .._shared.user_credential_async import CommunicationUserCredential
from .._shared.user_credential_async import CommunicationTokenCredential
from .._generated.aio import AzureCommunicationChatService
from .._generated.models import (
AddChatThreadMembersRequest,
Expand Down Expand Up @@ -51,7 +51,7 @@ class ChatThreadClient(object):
:param str endpoint:
The endpoint of the Azure Communication resource.
:param CommunicationUserCredential credential:
:param CommunicationTokenCredential credential:
The credentials with which to authenticate. The value contains a User
Access Token
:param str thread_id:
Expand All @@ -70,7 +70,7 @@ class ChatThreadClient(object):
def __init__(
self,
endpoint: str,
credential: CommunicationUserCredential,
credential: CommunicationTokenCredential,
thread_id: str,
**kwargs
) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class ChatClientSamples(object):

def create_chat_client(self):
# [START create_chat_client]
from azure.communication.chat import ChatClient, CommunicationUserCredential
chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token))
from azure.communication.chat import ChatClient, CommunicationTokenCredential
chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.token))
# [END create_chat_client]

def create_thread(self):
Expand All @@ -56,10 +56,10 @@ def create_thread(self):
ChatClient,
ChatThreadMember,
CommunicationUserIdentifier,
CommunicationUserCredential
CommunicationTokenCredential
)

chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token))
chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.token))

topic = "test topic"
members = [ChatThreadMember(
Expand All @@ -75,31 +75,31 @@ def create_thread(self):

def get_chat_thread_client(self):
# [START get_chat_thread_client]
from azure.communication.chat import ChatClient, CommunicationUserCredential
from azure.communication.chat import ChatClient, CommunicationTokenCredential

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

def get_thread(self):
# [START get_thread]
from azure.communication.chat import ChatClient, CommunicationUserCredential
from azure.communication.chat import ChatClient, CommunicationTokenCredential

chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token))
chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.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):
# [START list_threads]
from azure.communication.chat import ChatClient, CommunicationUserCredential
from azure.communication.chat import ChatClient, CommunicationTokenCredential
from datetime import datetime, timedelta
import pytz

chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token))
chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.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 @@ -111,9 +111,9 @@ def list_threads(self):

def delete_thread(self):
# [START delete_thread]
from azure.communication.chat import ChatClient, CommunicationUserCredential
from azure.communication.chat import ChatClient, CommunicationTokenCredential

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ class ChatClientSamplesAsync(object):

def create_chat_client(self):
# [START create_chat_client]
from azure.communication.chat.aio import ChatClient, CommunicationUserCredential
chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token))
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 create_thread_async(self):
from datetime import datetime
from azure.communication.chat.aio import ChatClient, CommunicationUserCredential
from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential
from azure.communication.chat import ChatThreadMember, CommunicationUser

chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token))
chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.token))
async with chat_client:
# [START create_thread]
topic = "test topic"
Expand All @@ -72,28 +72,28 @@ async def create_thread_async(self):

def get_chat_thread_client(self):
# [START get_chat_thread_client]
from azure.communication.chat.aio import ChatClient, CommunicationUserCredential
from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential

chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token))
chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(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 get_thread_async(self):
from azure.communication.chat.aio import ChatClient, CommunicationUserCredential
from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential

chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token))
chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.token))
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 list_threads_async(self):
from azure.communication.chat.aio import ChatClient, CommunicationUserCredential
from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential

chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token))
chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.token))
async with chat_client:
# [START list_threads]
from datetime import datetime, timedelta
Expand All @@ -107,9 +107,9 @@ async def list_threads_async(self):
# [END list_threads]

async def delete_thread_async(self):
from azure.communication.chat.aio import ChatClient, CommunicationUserCredential
from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential

chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token))
chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(self.token))
async with chat_client:
# [START delete_thread]
await chat_client.delete_chat_thread(self._thread_id)
Expand Down
Loading

0 comments on commit f7e0d77

Please sign in to comment.