19
19
from ._generated .models import CreateChatThreadRequest
20
20
from ._models import (
21
21
ChatThread ,
22
- ChatThreadParticipant
22
+ CreateChatThreadResult
23
+ )
24
+ from ._utils import ( # pylint: disable=unused-import
25
+ _to_utc_datetime ,
26
+ return_response ,
27
+ CommunicationErrorResponseConverter
23
28
)
24
- from ._utils import _to_utc_datetime , return_response # pylint: disable=unused-import
25
29
from ._version import SDK_MONIKER
26
30
27
31
if TYPE_CHECKING :
@@ -119,26 +123,24 @@ def get_chat_thread_client(
119
123
@distributed_trace
120
124
def create_chat_thread (
121
125
self , topic , # type: str
122
- thread_participants , # type: list[ChatThreadParticipant]
123
- repeatability_request_id = None , # type: Optional[str]
124
126
** kwargs # type: Any
125
127
):
126
- # type: (...) -> ChatThreadClient
128
+ # type: (...) -> CreateChatThreadResult
127
129
"""Creates a chat thread.
128
130
129
131
:param topic: Required. The thread topic.
130
132
:type topic: str
131
- :param thread_participants: Required . Participants to be added to the thread.
132
- :type thread_participants: list[~azure.communication.chat.ChatThreadParticipant]
133
- :param repeatability_request_id: If specified, the client directs that the request is
133
+ :keyword thread_participants: Optional . Participants to be added to the thread.
134
+ :paramtype thread_participants: list[~azure.communication.chat.ChatThreadParticipant]
135
+ :keyword repeatability_request_id: Optional. If specified, the client directs that the request is
134
136
repeatable; that is, that the client can make the request multiple times with the same
135
137
Repeatability-Request-ID and get back an appropriate response without the server executing the
136
138
request multiple times. The value of the Repeatability-Request-ID is an opaque string
137
139
representing a client-generated, globally unique for all time, identifier for the request. If not
138
140
specified, a new unique id would be generated.
139
- :type repeatability_request_id: str
140
- :return: ChatThreadClient
141
- :rtype: ~azure.communication.chat.ChatThreadClient
141
+ :paramtype repeatability_request_id: str
142
+ :return: CreateChatThreadResult
143
+ :rtype: ~azure.communication.chat.CreateChatThreadResult
142
144
:raises: ~azure.core.exceptions.HttpResponseError, ValueError
143
145
144
146
.. admonition:: Example:
@@ -148,40 +150,45 @@ def create_chat_thread(
148
150
:end-before: [END create_thread]
149
151
:language: python
150
152
:dedent: 8
151
- :caption: Creating ChatThreadClient by creating a new chat thread.
153
+ :caption: Creating ChatThread by creating a new chat thread.
152
154
"""
153
155
if not topic :
154
156
raise ValueError ("topic cannot be None." )
155
- if not thread_participants :
156
- raise ValueError ( "List of ChatThreadParticipant cannot be None." )
157
+
158
+ repeatability_request_id = kwargs . pop ( 'repeatability_request_id' , None )
157
159
if repeatability_request_id is None :
158
160
repeatability_request_id = str (uuid4 ())
159
161
160
- participants = [m ._to_generated () for m in thread_participants ] # pylint:disable=protected-access
161
- create_thread_request = \
162
- CreateChatThreadRequest (topic = topic , participants = participants )
162
+ thread_participants = kwargs .pop ('thread_participants' , None )
163
+ participants = []
164
+ if thread_participants is not None :
165
+ participants = [m ._to_generated () for m in thread_participants ] # pylint:disable=protected-access
166
+
167
+ create_thread_request = CreateChatThreadRequest (topic = topic , participants = participants )
163
168
164
169
create_chat_thread_result = self ._client .chat .create_chat_thread (
165
170
create_chat_thread_request = create_thread_request ,
166
171
repeatability_request_id = repeatability_request_id ,
167
172
** kwargs )
173
+
174
+ errors = None
168
175
if hasattr (create_chat_thread_result , 'errors' ) and \
169
176
create_chat_thread_result .errors is not None :
170
- participants = \
171
- create_chat_thread_result .errors .invalid_participants
172
- errors = []
173
- for participant in participants :
174
- errors .append ('participant ' + participant .target +
175
- ' failed to join thread due to: ' + participant .message )
176
- raise RuntimeError (errors )
177
- thread_id = create_chat_thread_result .chat_thread .id
178
- return ChatThreadClient (
179
- endpoint = self ._endpoint ,
180
- credential = self ._credential ,
181
- thread_id = thread_id ,
182
- ** kwargs
177
+ errors = CommunicationErrorResponseConverter ._convert ( # pylint:disable=protected-access
178
+ participants = [thread_participants ],
179
+ communication_errors = create_chat_thread_result .errors .invalid_participants
180
+ )
181
+
182
+ chat_thread = ChatThread ._from_generated ( # pylint:disable=protected-access
183
+ create_chat_thread_result .chat_thread )
184
+
185
+ create_chat_thread_result = CreateChatThreadResult (
186
+ chat_thread = chat_thread ,
187
+ errors = errors
183
188
)
184
189
190
+ return create_chat_thread_result
191
+
185
192
@distributed_trace
186
193
def get_chat_thread (
187
194
self , thread_id , # type: str
@@ -192,8 +199,7 @@ def get_chat_thread(
192
199
193
200
:param thread_id: Required. Thread id to get.
194
201
:type thread_id: str
195
- :keyword callable cls: A custom type or function that will be passed the direct response
196
- :return: ChatThread, or the result of cls(response)
202
+ :return: ChatThread
197
203
:rtype: ~azure.communication.chat.ChatThread
198
204
:raises: ~azure.core.exceptions.HttpResponseError, ValueError
199
205
@@ -254,8 +260,7 @@ def delete_chat_thread(
254
260
255
261
:param thread_id: Required. Thread id to delete.
256
262
:type thread_id: str
257
- :keyword callable cls: A custom type or function that will be passed the direct response
258
- :return: None, or the result of cls(response)
263
+ :return: None
259
264
:rtype: None
260
265
:raises: ~azure.core.exceptions.HttpResponseError, ValueError
261
266
0 commit comments