Skip to content

Commit 2528b34

Browse files
committed
feat: add new token based pagination support
1 parent 3ff18f3 commit 2528b34

File tree

4 files changed

+44
-17
lines changed

4 files changed

+44
-17
lines changed

tests/unit/base/test_token_pagination.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import unittest
2-
from unittest.mock import Mock, AsyncMock, MagicMock
2+
from unittest.mock import Mock, AsyncMock
33
from tests import IntegrationTestCase
44
from tests.holodeck import Request
5+
from twilio.base.exceptions import TwilioException
56
from twilio.base.token_pagination import TokenPagination
67
from twilio.http.response import Response
78

@@ -45,7 +46,10 @@ def setUp(self):
4546
"""
4647
self.response.status_code = 200
4748

48-
self.solution = {"account_sid": "ACxxxx", "uri": "/Accounts/ACxxxx/Resources.json"}
49+
self.solution = {
50+
"account_sid": "ACxxxx",
51+
"uri": "/Accounts/ACxxxx/Resources.json",
52+
}
4953
self.page = TestTokenPaginationPage(self.version, self.response, self.solution)
5054

5155
def test_key_property(self):
@@ -282,7 +286,7 @@ def test_next_page_without_uri_in_solution(self):
282286

283287
page = TestTokenPaginationPage(version, response, solution)
284288

285-
with self.assertRaises(ValueError) as context:
289+
with self.assertRaises(TwilioException) as context:
286290
page.next_page()
287291

288292
self.assertIn("URI must be provided", str(context.exception))
@@ -308,7 +312,7 @@ def test_previous_page_without_uri_in_solution(self):
308312

309313
page = TestTokenPaginationPage(version, response, solution)
310314

311-
with self.assertRaises(ValueError) as context:
315+
with self.assertRaises(TwilioException) as context:
312316
page.previous_page()
313317

314318
self.assertIn("URI must be provided", str(context.exception))
@@ -481,7 +485,9 @@ def setUp(self):
481485
)
482486

483487
self.version = self.client.api.v2010
484-
self.response = self.version.page(method="GET", uri="/Accounts/ACaaaa/Records.json")
488+
self.response = self.version.page(
489+
method="GET", uri="/Accounts/ACaaaa/Records.json"
490+
)
485491

486492
self.solution = {
487493
"account_sid": "ACaaaa",

tests/unit/rest/test_client.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,32 @@ def test_periods_in_query(self):
8181
def test_edge_deprecation_warning_when_only_edge_is_set(self):
8282
with warnings.catch_warnings(record=True) as w:
8383
warnings.simplefilter("always") # Ensure all warnings are caught
84-
Client(username="username", password="password", edge="edge") # Trigger the warning
84+
Client(
85+
username="username", password="password", edge="edge"
86+
) # Trigger the warning
8587

8688
# Check if a warning was raised
8789
self.assertGreater(len(w), 0)
8890
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
89-
self.assertIn("For regional processing, DNS is of format product.<edge>.<region>.twilio.com; otherwise use product.twilio.com.", str(w[-1].message))
91+
self.assertIn(
92+
"For regional processing, DNS is of format product.<edge>.<region>.twilio.com; otherwise use product.twilio.com.",
93+
str(w[-1].message),
94+
)
9095

9196
def test_edge_deprecation_warning_when_only_region_is_set(self):
9297
with warnings.catch_warnings(record=True) as w:
9398
warnings.simplefilter("always") # Ensure all warnings are caught
94-
Client(username="username", password="password", region="us1") # Trigger the warning
99+
Client(
100+
username="username", password="password", region="us1"
101+
) # Trigger the warning
95102

96103
# Check if a warning was raised
97104
self.assertGreater(len(w), 0)
98105
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
99-
self.assertIn("For regional processing, DNS is of format product.<edge>.<region>.twilio.com; otherwise use product.twilio.com.", str(w[-1].message))
100-
106+
self.assertIn(
107+
"For regional processing, DNS is of format product.<edge>.<region>.twilio.com; otherwise use product.twilio.com.",
108+
str(w[-1].message),
109+
)
101110

102111

103112
class TestUserAgentClients(unittest.TestCase):

twilio/base/client_base.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
class ClientBase(object):
1515
"""A client for accessing the Twilio API."""
16+
1617
region_mappings = {
1718
"au1": "sydney",
1819
"br1": "sao-paulo",
@@ -22,8 +23,9 @@ class ClientBase(object):
2223
"jp2": "osaka",
2324
"sg1": "singapore",
2425
"us1": "ashburn",
25-
"us2": "umatilla"
26+
"us2": "umatilla",
2627
}
28+
2729
def __init__(
2830
self,
2931
username: Optional[str] = None,
@@ -56,13 +58,19 @@ def __init__(
5658
""" :type : str """
5759
self.password = password or environment.get("TWILIO_AUTH_TOKEN")
5860
""" :type : str """
59-
if (edge is not None and region is None) or (region is not None and edge is None):
61+
if (edge is not None and region is None) or (
62+
region is not None and edge is None
63+
):
6064
warnings.warn(
6165
"For regional processing, DNS is of format product.<edge>.<region>.twilio.com; otherwise use product.twilio.com.",
6266
DeprecationWarning,
63-
stacklevel=2
67+
stacklevel=2,
6468
)
65-
self.edge = edge or environment.get("TWILIO_EDGE") or (self.region_mappings[region] if region is not None else "")
69+
self.edge = (
70+
edge
71+
or environment.get("TWILIO_EDGE")
72+
or (self.region_mappings[region] if region is not None else "")
73+
)
6674
""" :type : str """
6775
self.region = region or environment.get("TWILIO_REGION")
6876
""" :type : str """

twilio/base/token_pagination.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, Optional
1+
from typing import Optional
22

33
from twilio.base.exceptions import TwilioException
44
from twilio.base.page import Page
@@ -85,7 +85,9 @@ def _get_page(self, token: Optional[str]) -> Optional["TokenPagination"]:
8585
cls = type(self)
8686
return cls(self._version, response, self._solution)
8787

88-
async def _get_page_async(self, token: Optional[str]) -> Optional["TokenPagination"]:
88+
async def _get_page_async(
89+
self, token: Optional[str]
90+
) -> Optional["TokenPagination"]:
8991
"""
9092
Internal async helper to fetch a page using a given token.
9193
@@ -104,7 +106,9 @@ async def _get_page_async(self, token: Optional[str]) -> Optional["TokenPaginati
104106
raise TwilioException("URI must be provided for token pagination")
105107

106108
url = self._version.domain.absolute_url(uri)
107-
response = await self._version.domain.twilio.request_async("GET", url, params=params)
109+
response = await self._version.domain.twilio.request_async(
110+
"GET", url, params=params
111+
)
108112
cls = type(self)
109113
return cls(self._version, response, self._solution)
110114

0 commit comments

Comments
 (0)