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
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@
# Licensed under the MIT License.

import logging
from typing import Callable

logger = logging.getLogger(__name__)


class _DeferredString:
def __init__(self, func, *args, **kwargs):
"""A wrapper around a function to allow for deferred evaluation.

The result of the function is converted to a string with str().
If an error occurs during evaluation, an error is logged and a default
string is returned.
"""

def __init__(self, func: Callable, *args, **kwargs):
"""Initializes a DeferredString instance.

:param func: The function to be called to get the string value.
:param args: Positional arguments to pass to the function.
:param kwargs: Keyword arguments to pass to the function.
"""
self.func = func
self.args = args
self.kwargs = kwargs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async def save(
data = self.copy()
del data["__key__"]

logger.info(f"Saving state {self.__key__}")
logger.info("Saving state %s", self.__key__)
await storage.delete(self.__deleted__)
await storage.write(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ async def get_attachment_info(self, attachment_id: str) -> AttachmentInfo:

url = f"v3/attachments/{attachment_id}"

logger.info(f"Getting attachment info for ID: {attachment_id}")
logger.info("Getting attachment info for ID: %s", attachment_id)
async with self.client.get(url) as response:
if response.status >= 400:
if response.status >= 300:
logger.error(
f"Error getting attachment info: {response.status}", stack_info=True
"Error getting attachment info: %s",
response.status,
stack_info=True,
)
response.raise_for_status()

Expand Down Expand Up @@ -108,11 +110,13 @@ async def get_attachment(self, attachment_id: str, view_id: str) -> BytesIO:

url = f"v3/attachments/{attachment_id}/views/{view_id}"

logger.info(f"Getting attachment for ID: {attachment_id}, View ID: {view_id}")
logger.info(
"Getting attachment for ID: %s, View ID: %s", attachment_id, view_id
)
async with self.client.get(url) as response:
if response.status >= 400:
if response.status >= 300:
logger.error(
f"Error getting attachment: {response.status}", stack_info=True
"Error getting attachment: %s", response.status, stack_info=True
)
response.raise_for_status()

Expand Down Expand Up @@ -143,12 +147,12 @@ async def get_conversations(
)

logger.info(
f"Getting conversations with continuation token: {continuation_token}"
"Getting conversations with continuation token: %s", continuation_token
)
async with self.client.get("v3/conversations", params=params) as response:
if response.status >= 400:
if response.status >= 300:
logger.error(
f"Error getting conversations: {response.status}", stack_info=True
"Error getting conversations: %s", response.status, stack_info=True
)
response.raise_for_status()

Expand All @@ -170,9 +174,9 @@ async def create_conversation(
"v3/conversations",
json=body.model_dump(by_alias=True, exclude_unset=True, mode="json"),
) as response:
if response.status >= 400:
if response.status >= 300:
logger.error(
f"Error creating conversation: {response.status}", stack_info=True
"Error creating conversation: %s", response.status, stack_info=True
)
response.raise_for_status()

Expand Down Expand Up @@ -201,7 +205,10 @@ async def reply_to_activity(
url = f"v3/conversations/{conversation_id}/activities/{activity_id}"

logger.info(
f"Replying to activity: {activity_id} in conversation: {conversation_id}. Activity type is {body.type}"
"Replying to activity: %s in conversation: %s. Activity type is %s",
activity_id,
conversation_id,
body.type,
)

async with self.client.post(
Expand All @@ -212,15 +219,16 @@ async def reply_to_activity(
) as response:
result = await response.json() if response.content_length else {}

if response.status >= 400:
if response.status >= 300:
logger.error(
f"Error replying to activity: {result or response.status}",
"Error replying to activity: %s",
result or response.status,
stack_info=True,
)
response.raise_for_status()

logger.info(
f"Reply to conversation/activity: {result.get('id')}, {activity_id}"
"Reply to conversation/activity: %s, %s", result.get("id"), activity_id
)

return ResourceResponse.model_validate(result)
Expand All @@ -246,15 +254,19 @@ async def send_to_conversation(
url = f"v3/conversations/{conversation_id}/activities"

logger.info(
f"Sending to conversation: {conversation_id}. Activity type is {body.type}"
"Sending to conversation: %s. Activity type is %s",
conversation_id,
body.type,
)
async with self.client.post(
url,
json=body.model_dump(by_alias=True, exclude_unset=True, mode="json"),
) as response:
if response.status >= 400:
if response.status >= 300:
logger.error(
f"Error sending to conversation: {response.status}", stack_info=True
"Error sending to conversation: %s",
response.status,
stack_info=True,
)
response.raise_for_status()

Expand Down Expand Up @@ -283,15 +295,18 @@ async def update_activity(
url = f"v3/conversations/{conversation_id}/activities/{activity_id}"

logger.info(
f"Updating activity: {activity_id} in conversation: {conversation_id}. Activity type is {body.type}"
"Updating activity: %s in conversation: %s. Activity type is %s",
activity_id,
conversation_id,
body.type,
)
async with self.client.put(
url,
json=body.model_dump(by_alias=True, exclude_unset=True),
) as response:
if response.status >= 400:
if response.status >= 300:
logger.error(
f"Error updating activity: {response.status}", stack_info=True
"Error updating activity: %s", response.status, stack_info=True
)
response.raise_for_status()

Expand All @@ -316,12 +331,14 @@ async def delete_activity(self, conversation_id: str, activity_id: str) -> None:
url = f"v3/conversations/{conversation_id}/activities/{activity_id}"

logger.info(
f"Deleting activity: {activity_id} from conversation: {conversation_id}"
"Deleting activity: %s from conversation: %s",
activity_id,
conversation_id,
)
async with self.client.delete(url) as response:
if response.status >= 400:
if response.status >= 300:
logger.error(
f"Error deleting activity: {response.status}", stack_info=True
"Error deleting activity: %s", response.status, stack_info=True
)
response.raise_for_status()

Expand Down Expand Up @@ -354,12 +371,14 @@ async def upload_attachment(
}

logger.info(
f"Uploading attachment to conversation: {conversation_id}, Attachment name: {body.name}"
"Uploading attachment to conversation: %s, Attachment name: %s",
conversation_id,
body.name,
)
async with self.client.post(url, json=attachment_dict) as response:
if response.status >= 400:
if response.status >= 300:
logger.error(
f"Error uploading attachment: {response.status}", stack_info=True
"Error uploading attachment: %s", response.status, stack_info=True
)
response.raise_for_status()

Expand All @@ -385,11 +404,14 @@ async def get_conversation_members(
conversation_id = self._normalize_conversation_id(conversation_id)
url = f"v3/conversations/{conversation_id}/members"

logger.info(f"Getting conversation members for conversation: {conversation_id}")
logger.info(
"Getting conversation members for conversation: %s", conversation_id
)
async with self.client.get(url) as response:
if response.status >= 400:
if response.status >= 300:
logger.error(
f"Error getting conversation members: {response.status}",
"Error getting conversation members: %s",
response.status,
stack_info=True,
)
response.raise_for_status()
Expand Down Expand Up @@ -418,12 +440,15 @@ async def get_conversation_member(
url = f"v3/conversations/{conversation_id}/members/{member_id}"

logger.info(
f"Getting conversation member: {member_id} from conversation: {conversation_id}"
"Getting conversation member: %s from conversation: %s",
member_id,
conversation_id,
)
async with self.client.get(url) as response:
if response.status >= 400:
if response.status >= 300:
logger.error(
f"Error getting conversation member: {response.status}",
"Error getting conversation member: %s",
response.status,
stack_info=True,
)
response.raise_for_status()
Expand Down Expand Up @@ -451,12 +476,15 @@ async def delete_conversation_member(
url = f"v3/conversations/{conversation_id}/members/{member_id}"

logger.info(
f"Deleting conversation member: {member_id} from conversation: {conversation_id}"
"Deleting conversation member: %s from conversation: %s",
member_id,
conversation_id,
)
async with self.client.delete(url) as response:
if response.status >= 400 and response.status != 204:
if response.status >= 300:
logger.error(
f"Error deleting conversation member: {response.status}",
"Error deleting conversation member: %s",
response.status,
stack_info=True,
)
response.raise_for_status()
Expand All @@ -482,12 +510,15 @@ async def get_activity_members(
url = f"v3/conversations/{conversation_id}/activities/{activity_id}/members"

logger.info(
f"Getting activity members for conversation: {conversation_id}, Activity ID: {activity_id}"
"Getting activity members for conversation: %s, Activity ID: %s",
conversation_id,
activity_id,
)
async with self.client.get(url) as response:
if response.status >= 400:
if response.status >= 300:
logger.error(
f"Error getting activity members: {response.status}",
"Error getting activity members: %s",
response.status,
stack_info=True,
)
response.raise_for_status()
Expand Down Expand Up @@ -526,12 +557,16 @@ async def get_conversation_paged_members(
url = f"v3/conversations/{conversation_id}/pagedmembers"

logger.info(
f"Getting paged members for conversation: {conversation_id}, Page Size: {page_size}, Continuation Token: {continuation_token}"
"Getting paged members for conversation: %s, Page Size: %s, Continuation Token: %s",
conversation_id,
page_size,
continuation_token,
)
async with self.client.get(url, params=params) as response:
if response.status >= 400:
if response.status >= 300:
logger.error(
f"Error getting conversation paged members: {response.status}",
"Error getting conversation paged members: %s",
response.status,
stack_info=True,
)
response.raise_for_status()
Expand Down Expand Up @@ -559,15 +594,12 @@ async def send_conversation_history(
conversation_id = self._normalize_conversation_id(conversation_id)
url = f"v3/conversations/{conversation_id}/activities/history"

logger.info(f"Sending conversation history to conversation: {conversation_id}")
logger.info("Sending conversation history to conversation: %s", conversation_id)
async with self.client.post(url, json=body) as response:
if (
response.status >= 400
and response.status != 201
and response.status != 202
):
if response.status >= 300:
logger.error(
f"Error sending conversation history: {response.status}",
"Error sending conversation history: %s",
response.status,
stack_info=True,
)
response.raise_for_status()
Expand Down Expand Up @@ -602,7 +634,9 @@ def __init__(self, endpoint: str, token: str, *, session: ClientSession = None):
headers=headers,
)
logger.debug(
f"ConnectorClient initialized with endpoint: {endpoint} and headers: {headers}"
"ConnectorClient initialized with endpoint: %s and headers: %s",
endpoint,
headers,
)

if len(token) > 1:
Expand Down
Loading