|
| 1 | +""" |
| 2 | +Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +Licensed under the MIT License. |
| 4 | +""" |
| 5 | + |
| 6 | +from typing import Any, Optional, Union |
| 7 | + |
| 8 | +from microsoft.teams.common.http import Client, ClientOptions |
| 9 | + |
| 10 | +from ...models import ConversationResource |
| 11 | +from .activity import ConversationActivityClient |
| 12 | +from .member import ConversationMemberClient |
| 13 | +from .params import ( |
| 14 | + CreateConversationParams, |
| 15 | + GetConversationsParams, |
| 16 | + GetConversationsResponse, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class ConversationOperations: |
| 21 | + """Base class for conversation operations.""" |
| 22 | + |
| 23 | + def __init__(self, client: "ConversationClient", conversation_id: str) -> None: |
| 24 | + self._client = client |
| 25 | + self._conversation_id = conversation_id |
| 26 | + |
| 27 | + |
| 28 | +class ActivityOperations(ConversationOperations): |
| 29 | + """Operations for managing activities in a conversation.""" |
| 30 | + |
| 31 | + async def create(self, activity: Any) -> Any: |
| 32 | + return await self._client._activities.create(self._conversation_id, activity) |
| 33 | + |
| 34 | + async def update(self, activity_id: str, activity: Any) -> Any: |
| 35 | + return await self._client._activities.update(self._conversation_id, activity_id, activity) |
| 36 | + |
| 37 | + async def reply(self, activity_id: str, activity: Any) -> Any: |
| 38 | + return await self._client._activities.reply(self._conversation_id, activity_id, activity) |
| 39 | + |
| 40 | + async def delete(self, activity_id: str) -> None: |
| 41 | + await self._client._activities.delete(self._conversation_id, activity_id) |
| 42 | + |
| 43 | + async def get_members(self, activity_id: str) -> Any: |
| 44 | + return await self._client._activities.get_members(self._conversation_id, activity_id) |
| 45 | + |
| 46 | + |
| 47 | +class MemberOperations(ConversationOperations): |
| 48 | + """Operations for managing members in a conversation.""" |
| 49 | + |
| 50 | + async def get_all(self) -> Any: |
| 51 | + return await self._client._members.get(self._conversation_id) |
| 52 | + |
| 53 | + async def get(self, member_id: str) -> Any: |
| 54 | + return await self._client._members.get_by_id(self._conversation_id, member_id) |
| 55 | + |
| 56 | + async def delete(self, member_id: str) -> None: |
| 57 | + await self._client._members.delete(self._conversation_id, member_id) |
| 58 | + |
| 59 | + |
| 60 | +class ConversationClient: |
| 61 | + """Client for managing Teams conversations.""" |
| 62 | + |
| 63 | + def __init__(self, service_url: str, options: Union[Client, ClientOptions] = None) -> None: |
| 64 | + """Initialize the client. |
| 65 | +
|
| 66 | + Args: |
| 67 | + service_url: The Teams service URL. |
| 68 | + options: Either an HTTP client instance or client options. If None, a default client is created. |
| 69 | + """ |
| 70 | + self.service_url = service_url |
| 71 | + |
| 72 | + if isinstance(options, Client): |
| 73 | + self._http = options |
| 74 | + else: |
| 75 | + self._http = Client(options or ClientOptions()) |
| 76 | + |
| 77 | + self._activities = ConversationActivityClient(service_url, self._http) |
| 78 | + self._members = ConversationMemberClient(service_url, self._http) |
| 79 | + |
| 80 | + @property |
| 81 | + def http(self) -> Client: |
| 82 | + """Get the HTTP client. |
| 83 | +
|
| 84 | + Returns: |
| 85 | + The HTTP client instance. |
| 86 | + """ |
| 87 | + return self._http |
| 88 | + |
| 89 | + @http.setter |
| 90 | + def http(self, client: Client) -> None: |
| 91 | + """Set the HTTP client. |
| 92 | +
|
| 93 | + Args: |
| 94 | + client: The HTTP client to use. |
| 95 | + """ |
| 96 | + self._http = client |
| 97 | + # Update sub-clients with new HTTP client |
| 98 | + self._activities = ConversationActivityClient(self.service_url, client) |
| 99 | + self._members = ConversationMemberClient(self.service_url, client) |
| 100 | + |
| 101 | + def activities(self, conversation_id: str) -> ActivityOperations: |
| 102 | + """Get activity operations for a conversation. |
| 103 | +
|
| 104 | + Args: |
| 105 | + conversation_id: The ID of the conversation. |
| 106 | +
|
| 107 | + Returns: |
| 108 | + An operations object for managing activities in the conversation. |
| 109 | + """ |
| 110 | + return ActivityOperations(self, conversation_id) |
| 111 | + |
| 112 | + def members(self, conversation_id: str) -> MemberOperations: |
| 113 | + """Get member operations for a conversation. |
| 114 | +
|
| 115 | + Args: |
| 116 | + conversation_id: The ID of the conversation. |
| 117 | +
|
| 118 | + Returns: |
| 119 | + An operations object for managing members in the conversation. |
| 120 | + """ |
| 121 | + return MemberOperations(self, conversation_id) |
| 122 | + |
| 123 | + async def get(self, params: Optional[GetConversationsParams] = None) -> GetConversationsResponse: |
| 124 | + """Get a list of conversations. |
| 125 | +
|
| 126 | + Args: |
| 127 | + params: Optional parameters for getting conversations. |
| 128 | +
|
| 129 | + Returns: |
| 130 | + A response containing the list of conversations and a continuation token. |
| 131 | + """ |
| 132 | + query_params = {} |
| 133 | + if params and params.continuation_token: |
| 134 | + query_params["continuationToken"] = params.continuation_token |
| 135 | + |
| 136 | + response = await self._http.get( |
| 137 | + f"{self.service_url}/v3/conversations", |
| 138 | + params=query_params, |
| 139 | + ) |
| 140 | + return GetConversationsResponse.model_validate(response.json()) |
| 141 | + |
| 142 | + async def create(self, params: CreateConversationParams) -> ConversationResource: |
| 143 | + """Create a new conversation. |
| 144 | +
|
| 145 | + Args: |
| 146 | + params: Parameters for creating the conversation. |
| 147 | +
|
| 148 | + Returns: |
| 149 | + The created conversation resource. |
| 150 | + """ |
| 151 | + response = await self._http.post( |
| 152 | + f"{self.service_url}/v3/conversations", |
| 153 | + json=params.model_dump(by_alias=True), |
| 154 | + ) |
| 155 | + return ConversationResource.model_validate(response.json()) |
0 commit comments