Skip to content

Commit 9257630

Browse files
Fix SessionResource to use flat /AgentSession endpoints matching backend API (#3)
The backend uses account-scoped /v1/AgentSession routes, not nested /Agent/{uuid}/Session. Align the SDK: list() no longer requires agent_uuid (use agent_id filter param instead), get() takes only session_id, and all backend filters (phone_number, call_uuid, date/duration ranges) are documented. Fixes SER-4434
1 parent 0be79be commit 9257630

2 files changed

Lines changed: 54 additions & 14 deletions

File tree

src/plivo_agentstack/agent/client.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,25 +220,37 @@ async def unassign(self, agent_uuid: str, number: str) -> None:
220220

221221

222222
class SessionResource:
223-
"""Session history -- list and get agent sessions."""
223+
"""Session history -- list and get agent sessions.
224+
225+
Sessions are account-scoped (not nested under an agent).
226+
Use ``agent_id`` query param to filter by agent.
227+
228+
Session IDs accept both raw UUIDs and ``as_<uuid>`` prefix format.
229+
"""
224230

225231
def __init__(self, http: HttpTransport, prefix: str) -> None:
226232
self._http = http
227233
self._prefix = prefix
228234

229-
async def list(self, agent_uuid: str, **params: Any) -> dict:
230-
"""GET /Agent/{agent_uuid}/Session -- list sessions.
235+
async def list(self, **params: Any) -> dict:
236+
"""GET /AgentSession -- paginated list.
237+
238+
Optional query params: limit, offset, sort_by, sort_order,
239+
agent_id, agent_mode, call_uuid, phone_number,
240+
ended_at__gte, ended_at__lte, created_at__gte, created_at__lte,
241+
duration__gte, duration__lte.
231242
232-
Optional query params: limit, offset, sort_by, sort_order, agent_mode.
243+
Returns ``{"api_id": "...", "objects": [...],
244+
"meta": {"limit", "offset", "total_count", "previous", "next"}}``.
233245
"""
234246
return await self._http.request(
235-
"GET", f"{self._prefix}/Agent/{agent_uuid}/Session", params=params
247+
"GET", f"{self._prefix}/AgentSession", params=params
236248
)
237249

238-
async def get(self, agent_uuid: str, session_id: str) -> dict:
239-
"""GET /Agent/{agent_uuid}/Session/{session_id} -- get session details."""
250+
async def get(self, session_id: str) -> dict:
251+
"""GET /AgentSession/{session_id} -- get session details."""
240252
return await self._http.request(
241-
"GET", f"{self._prefix}/Agent/{agent_uuid}/Session/{session_id}"
253+
"GET", f"{self._prefix}/AgentSession/{session_id}"
242254
)
243255

244256

tests/test_agent/test_client.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ async def test_number_unassign(mock_api, http_transport):
172172

173173

174174
async def test_session_list(mock_api, http_transport):
175-
"""GET /v1/Account/TESTAUTH123/Agent/{uuid}/Session lists sessions."""
176-
mock_api.get(f"/v1/Account/TESTAUTH123/Agent/{AGENT_UUID}/Session").mock(
175+
"""GET /v1/Account/TESTAUTH123/AgentSession lists sessions."""
176+
mock_api.get("/v1/Account/TESTAUTH123/AgentSession").mock(
177177
return_value=httpx.Response(
178178
200,
179179
json={
@@ -192,14 +192,42 @@ async def test_session_list(mock_api, http_transport):
192192
)
193193
)
194194
client = AgentClient(http_transport)
195-
result = await client.sessions.list(AGENT_UUID, limit=10, offset=0)
195+
result = await client.sessions.list(limit=10, offset=0)
196196
assert result["meta"]["total_count"] == 1
197197
assert result["objects"][0]["agent_session_id"] == SESSION_ID
198198

199199

200+
async def test_session_list_with_filters(mock_api, http_transport):
201+
"""GET /v1/Account/TESTAUTH123/AgentSession passes filter params."""
202+
mock_api.get("/v1/Account/TESTAUTH123/AgentSession").mock(
203+
return_value=httpx.Response(
204+
200,
205+
json={
206+
"api_id": "abc-123",
207+
"objects": [],
208+
"meta": {
209+
"limit": 20,
210+
"offset": 0,
211+
"total_count": 0,
212+
"previous": None,
213+
"next": None,
214+
},
215+
},
216+
)
217+
)
218+
client = AgentClient(http_transport)
219+
result = await client.sessions.list(
220+
agent_id=AGENT_UUID, phone_number="+14155551234"
221+
)
222+
assert result["meta"]["total_count"] == 0
223+
request = mock_api.calls[0].request
224+
assert request.url.params["agent_id"] == AGENT_UUID
225+
assert request.url.params["phone_number"] == "+14155551234"
226+
227+
200228
async def test_session_get(mock_api, http_transport):
201-
"""GET /v1/Account/TESTAUTH123/Agent/{uuid}/Session/{session_id} gets session details."""
202-
mock_api.get(f"/v1/Account/TESTAUTH123/Agent/{AGENT_UUID}/Session/{SESSION_ID}").mock(
229+
"""GET /v1/Account/TESTAUTH123/AgentSession/{session_id} gets session details."""
230+
mock_api.get(f"/v1/Account/TESTAUTH123/AgentSession/{SESSION_ID}").mock(
203231
return_value=httpx.Response(
204232
200,
205233
json={
@@ -212,7 +240,7 @@ async def test_session_get(mock_api, http_transport):
212240
)
213241
)
214242
client = AgentClient(http_transport)
215-
result = await client.sessions.get(AGENT_UUID, SESSION_ID)
243+
result = await client.sessions.get(SESSION_ID)
216244
assert result["agent_session_id"] == SESSION_ID
217245
assert result["duration_seconds"] == 120
218246
assert result["turn_count"] == 5

0 commit comments

Comments
 (0)