Skip to content
Open
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
70 changes: 37 additions & 33 deletions backend/python/app/sources/external/microsoft/teams/teams.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


import json
import logging
from dataclasses import asdict
Expand Down Expand Up @@ -86,30 +84,32 @@ def _handle_teams_response(self, response: object) -> TeamsResponse:
if response is None:
return TeamsResponse(success=False, error="Empty response from Teams API")

success = True
error_msg = None
# Fast-path all the common paths by reorganizing conditions
# 1. dict with 'error'
# 2. hasattr 'error'
# 3. hasattr 'code' and 'message'
# Only check attributes once as needed.

# Enhanced error response handling for Teams operations
if hasattr(response, 'error'):
success = False
error_msg = str(response.error)
elif isinstance(response, dict) and 'error' in response:
success = False
# Avoid redundant isinstance/hasattr checks where feasible
if isinstance(response, dict) and 'error' in response:
error_info = response['error']
if isinstance(error_info, dict):
error_code = error_info.get('code', 'Unknown')
error_message = error_info.get('message', 'No message')
error_msg = f"{error_code}: {error_message}"
else:
error_msg = str(error_info)
elif hasattr(response, 'code') and hasattr(response, 'message'):
success = False
return TeamsResponse(success=False, data=response, error=error_msg)

if hasattr(response, 'error'):
return TeamsResponse(success=False, data=response, error=str(response.error))

if hasattr(response, 'code') and hasattr(response, 'message'):
error_msg = f"{response.code}: {response.message}"
return TeamsResponse(
success=success,
data=response,
error=error_msg,
)
return TeamsResponse(success=False, data=response, error=error_msg)

# Normal success response, fast path
return TeamsResponse(success=True, data=response, error=None)
except Exception as e:
logger.error(f"Error handling Teams response: {e}")
return TeamsResponse(success=False, error=str(e))
Expand Down Expand Up @@ -224,22 +224,26 @@ async def teams_get_group(self, team_id: str, select: Optional[List[str]] = None
try:
# Build query parameters
query_params = TeamsRequestBuilder.TeamsRequestBuilderGetQueryParameters()
if select:
query_params.select = select
if expand:
query_params.expand = expand
if filter:
query_params.filter = filter
if orderby:
query_params.orderby = orderby
if search:
query_params.search = search
if top:
query_params.top = top
if skip:
query_params.skip = skip
config = TeamsRequestBuilder.TeamsRequestBuilderGetRequestConfiguration(query_parameters=query_params)
response = await self.client.teams.by_team_id(team_id).group.get(request_configuration=config)
params = (
('select', select),
('expand', expand),
('filter', filter),
('orderby', orderby),
('search', search),
('top', top),
('skip', skip),
)
# Avoid many if statements: set only the not-None fields
for attr, value in params:
if value is not None:
setattr(query_params, attr, value)
config = TeamsRequestBuilder.TeamsRequestBuilderGetRequestConfiguration(
query_parameters=query_params
)
# Reuse local for chained attribute: slightly reduces method-call time
teams = self.client.teams
group_req = teams.by_team_id(team_id).group
response = await group_req.get(request_configuration=config)
return self._handle_teams_response(response)
except Exception as e:
logger.error(f"Error in teams_get_group: {e}")
Expand Down